feat: add Prometheus metrics parser for llama-swap GPU series
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { test } from "node:test";
|
||||
import { gpuInfos, parseGpuMetrics, parsePrometheus } from "../src/lib/metrics-parser";
|
||||
|
||||
const FIXTURE = readFileSync(new URL("./fixtures/metrics.txt", import.meta.url), "utf8");
|
||||
|
||||
test("parsePrometheus parses a gauge with labels", () => {
|
||||
const samples = parsePrometheus(`# TYPE llamaswap_gpu_util_percent gauge\nllamaswap_gpu_util_percent{id="0",name="RTX 5090"} 42.5\n`);
|
||||
assert.equal(samples.length, 1);
|
||||
assert.equal(samples[0].metric, "llamaswap_gpu_util_percent");
|
||||
assert.deepEqual(samples[0].labels, { id: "0", name: "RTX 5090" });
|
||||
assert.equal(samples[0].value, 42.5);
|
||||
});
|
||||
|
||||
test("parsePrometheus ignores comments and blank lines", () => {
|
||||
const samples = parsePrometheus("# HELP x y\n# TYPE x gauge\n\n");
|
||||
assert.equal(samples.length, 0);
|
||||
});
|
||||
|
||||
test("parseGpuMetrics extracts all five kinds for three GPUs from the fixture", () => {
|
||||
const samples = parseGpuMetrics(FIXTURE);
|
||||
assert.equal(samples.length, 15);
|
||||
const kinds = new Set(samples.map((s) => s.kind));
|
||||
assert.deepEqual([...kinds].sort(), ["fan", "memory_util_percent", "power", "temperature", "util_percent"]);
|
||||
const power0 = samples.find((s) => s.kind === "power" && s.id === "0");
|
||||
assert.ok(power0);
|
||||
assert.equal(power0.value, 316.56);
|
||||
});
|
||||
|
||||
test("gpuInfos dedupes, preserves names, sorts numerically by id", () => {
|
||||
const infos = gpuInfos(parseGpuMetrics(FIXTURE));
|
||||
assert.deepEqual(infos.map((i) => i.id), ["0", "1", "2"]);
|
||||
assert.equal(infos[2].name, "NVIDIA GeForce RTX 5090");
|
||||
});
|
||||
Reference in New Issue
Block a user