llama-cpp: xmrig + grafana hooks
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
@@ -8,49 +7,7 @@ let
|
||||
script = ../services/llama-cpp-annotations.py;
|
||||
python = pkgs.python3;
|
||||
|
||||
mockLlamaCpp = pkgs.writeText "mock-llama-cpp-server.py" ''
|
||||
import http.server, json, sys, os
|
||||
|
||||
PORT = int(sys.argv[1])
|
||||
STATE_FILE = sys.argv[2]
|
||||
|
||||
if not os.path.exists(STATE_FILE):
|
||||
with open(STATE_FILE, "w") as f:
|
||||
json.dump([{"id": 0, "is_processing": False, "next_token": {"n_decoded": 0}}], f)
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
def log_message(self, fmt, *args):
|
||||
pass
|
||||
|
||||
def _json(self, code, body):
|
||||
data = json.dumps(body).encode()
|
||||
self.send_response(code)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == "/slots":
|
||||
with open(STATE_FILE) as f:
|
||||
slots = json.load(f)
|
||||
self._json(200, slots)
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
def do_POST(self):
|
||||
if self.path == "/test/set-slots":
|
||||
length = int(self.headers.get("Content-Length", 0))
|
||||
body = json.loads(self.rfile.read(length)) if length else []
|
||||
with open(STATE_FILE, "w") as f:
|
||||
json.dump(body, f)
|
||||
self._json(200, {"ok": True})
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
http.server.HTTPServer(("127.0.0.1", PORT), Handler).serve_forever()
|
||||
'';
|
||||
mockLlamaProcess = ./mock-llama-server-proc.py;
|
||||
in
|
||||
pkgs.testers.runNixOSTest {
|
||||
name = "llama-cpp-annotations";
|
||||
@@ -61,6 +18,7 @@ pkgs.testers.runNixOSTest {
|
||||
environment.systemPackages = [
|
||||
pkgs.python3
|
||||
pkgs.curl
|
||||
pkgs.procps
|
||||
];
|
||||
};
|
||||
|
||||
@@ -69,25 +27,23 @@ pkgs.testers.runNixOSTest {
|
||||
import time
|
||||
|
||||
GRAFANA_PORT = 13000
|
||||
LLAMA_PORT = 16688
|
||||
ANNOTS_FILE = "/tmp/annotations.json"
|
||||
SLOTS_FILE = "/tmp/llama-slots.json"
|
||||
LLAMA_STATE = "/tmp/llama-state.txt"
|
||||
STATE_FILE = "/tmp/llama-annot-state.json"
|
||||
PYTHON = "${python}/bin/python3"
|
||||
MOCK_GRAFANA = "${mockGrafana}"
|
||||
MOCK_LLAMA = "${mockLlamaCpp}"
|
||||
MOCK_LLAMA = "${mockLlamaProcess}"
|
||||
SCRIPT = "${script}"
|
||||
|
||||
def read_annotations():
|
||||
out = machine.succeed(f"cat {ANNOTS_FILE} 2>/dev/null || echo '[]'")
|
||||
return json.loads(out.strip())
|
||||
|
||||
def set_slots(slots):
|
||||
machine.succeed(
|
||||
f"curl -sf -X POST http://127.0.0.1:{LLAMA_PORT}/test/set-slots "
|
||||
f"-H 'Content-Type: application/json' "
|
||||
f"-d '{json.dumps(slots)}'"
|
||||
)
|
||||
def set_busy():
|
||||
machine.succeed(f"echo busy > {LLAMA_STATE}")
|
||||
|
||||
def set_idle():
|
||||
machine.succeed(f"echo idle > {LLAMA_STATE}")
|
||||
|
||||
start_all()
|
||||
machine.wait_for_unit("multi-user.target")
|
||||
@@ -98,10 +54,7 @@ pkgs.testers.runNixOSTest {
|
||||
f"systemd-run --unit=mock-grafana {PYTHON} {MOCK_GRAFANA} {GRAFANA_PORT} {ANNOTS_FILE}"
|
||||
)
|
||||
machine.succeed(
|
||||
f"echo '[{{\"id\": 0, \"is_processing\": false, \"next_token\": {{\"n_decoded\": 0}}}}]' > {SLOTS_FILE}"
|
||||
)
|
||||
machine.succeed(
|
||||
f"systemd-run --unit=mock-llama {PYTHON} {MOCK_LLAMA} {LLAMA_PORT} {SLOTS_FILE}"
|
||||
f"systemd-run --unit=mock-llama {PYTHON} {MOCK_LLAMA} {LLAMA_STATE}"
|
||||
)
|
||||
machine.wait_until_succeeds(
|
||||
f"curl -sf http://127.0.0.1:{GRAFANA_PORT}/api/annotations -X POST "
|
||||
@@ -109,7 +62,7 @@ pkgs.testers.runNixOSTest {
|
||||
timeout=10,
|
||||
)
|
||||
machine.wait_until_succeeds(
|
||||
f"curl -sf http://127.0.0.1:{LLAMA_PORT}/slots | grep -q is_processing",
|
||||
"pgrep -x llama-server",
|
||||
timeout=10,
|
||||
)
|
||||
machine.succeed(f"echo '[]' > {ANNOTS_FILE}")
|
||||
@@ -117,62 +70,62 @@ pkgs.testers.runNixOSTest {
|
||||
with subtest("Start annotation service"):
|
||||
machine.succeed(
|
||||
f"systemd-run --unit=llama-annot "
|
||||
f"--setenv=LLAMA_CPP_URL=http://127.0.0.1:{LLAMA_PORT} "
|
||||
f"--setenv=GRAFANA_URL=http://127.0.0.1:{GRAFANA_PORT} "
|
||||
f"--setenv=STATE_FILE={STATE_FILE} "
|
||||
f"--setenv=POLL_INTERVAL=2 "
|
||||
f"--setenv=CPU_THRESHOLD=10 "
|
||||
f"{PYTHON} {SCRIPT}"
|
||||
)
|
||||
time.sleep(3)
|
||||
time.sleep(5)
|
||||
|
||||
with subtest("No annotations when slots are idle"):
|
||||
with subtest("No annotations when idle"):
|
||||
annots = read_annotations()
|
||||
assert annots == [], f"Expected no annotations, got: {annots}"
|
||||
|
||||
with subtest("Annotation created when slot starts processing"):
|
||||
set_slots([{"id": 0, "is_processing": True, "next_token": {"n_decoded": 0}}])
|
||||
with subtest("Annotation created when llama-server becomes busy"):
|
||||
set_busy()
|
||||
machine.wait_until_succeeds(
|
||||
f"cat {ANNOTS_FILE} | {PYTHON} -c "
|
||||
f"\"import sys,json; a=json.load(sys.stdin); exit(0 if a else 1)\"",
|
||||
timeout=15,
|
||||
timeout=20,
|
||||
)
|
||||
annots = read_annotations()
|
||||
assert len(annots) == 1, f"Expected 1 annotation, got: {annots}"
|
||||
assert "llama-cpp" in annots[0].get("tags", []), f"Missing tag: {annots[0]}"
|
||||
assert "slot 0" in annots[0]["text"], f"Missing slot info: {annots[0]['text']}"
|
||||
assert "LLM request" in annots[0]["text"], f"Missing text: {annots[0]['text']}"
|
||||
assert "timeEnd" not in annots[0], f"timeEnd should not be set: {annots[0]}"
|
||||
|
||||
with subtest("Annotation closed when slot stops processing"):
|
||||
set_slots([{"id": 0, "is_processing": False, "next_token": {"n_decoded": 42}}])
|
||||
with subtest("Annotation closed when llama-server becomes idle"):
|
||||
set_idle()
|
||||
machine.wait_until_succeeds(
|
||||
f"cat {ANNOTS_FILE} | {PYTHON} -c "
|
||||
f"\"import sys,json; a=json.load(sys.stdin); exit(0 if a and 'timeEnd' in a[0] else 1)\"",
|
||||
timeout=15,
|
||||
timeout=20,
|
||||
)
|
||||
annots = read_annotations()
|
||||
assert len(annots) == 1, f"Expected 1, got: {annots}"
|
||||
assert "timeEnd" in annots[0], f"timeEnd missing: {annots[0]}"
|
||||
assert annots[0]["timeEnd"] > annots[0]["time"], "timeEnd should be after time"
|
||||
assert "42 tokens" in annots[0].get("text", ""), f"Token count missing: {annots[0]}"
|
||||
assert "s)" in annots[0].get("text", ""), f"Duration missing: {annots[0]}"
|
||||
|
||||
with subtest("State survives restart"):
|
||||
set_slots([{"id": 0, "is_processing": True, "next_token": {"n_decoded": 0}}])
|
||||
set_busy()
|
||||
machine.wait_until_succeeds(
|
||||
f"cat {ANNOTS_FILE} | {PYTHON} -c "
|
||||
f"\"import sys,json; a=json.load(sys.stdin); exit(0 if len(a)==2 else 1)\"",
|
||||
timeout=15,
|
||||
timeout=20,
|
||||
)
|
||||
machine.succeed("systemctl stop llama-annot || true")
|
||||
time.sleep(1)
|
||||
machine.succeed(
|
||||
f"systemd-run --unit=llama-annot-2 "
|
||||
f"--setenv=LLAMA_CPP_URL=http://127.0.0.1:{LLAMA_PORT} "
|
||||
f"--setenv=GRAFANA_URL=http://127.0.0.1:{GRAFANA_PORT} "
|
||||
f"--setenv=STATE_FILE={STATE_FILE} "
|
||||
f"--setenv=POLL_INTERVAL=2 "
|
||||
f"--setenv=CPU_THRESHOLD=10 "
|
||||
f"{PYTHON} {SCRIPT}"
|
||||
)
|
||||
time.sleep(4)
|
||||
time.sleep(6)
|
||||
annots = read_annotations()
|
||||
assert len(annots) == 2, f"Restart should not duplicate, got: {annots}"
|
||||
'';
|
||||
|
||||
Reference in New Issue
Block a user