File size: 3,022 Bytes
0358431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node
// OpenJev CPU smoke-bench:  warmup, 1-question latency x N, 10-question batch, ops.
// Usage:  node bench.mjs http://localhost:7860 [--batch 1 --n 5 --token x]

const BASE = process.argv[1] ?? "http://localhost:7860";
const args = process.argv.slice(2);
const opt = (k, d) => { const i = args.indexOf(k); return i >= 0 ? Number(args[i + 1] ?? d) : d; };
const N   = opt("--n", 5);
const B   = opt("--batch", 10);

const token = (() => { const i = args.indexOf("--token"); return i >= 0 ? args[i + 1] : ""; })();
const headers = { "Content-Type": "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}) };
const baseQ = (suffix) => ({
  state: `Customer message: I was charged twice for my order last week${suffix}`,
  questions: {
    route:   { type: "choice", instructions: "Which team should handle this?",
               criteria: { billing: null, shipping: null, technical: null } },
    angry:   { type: "noul",   instructions: "Is the customer angry?" },
    urgency: { type: "score",  instructions: "How urgent is this?",
               criteria: ["can wait", "this week", "today", "right now"] },
  },
});

async function post(path, body) {
  const t0 = Date.now();
  const r = await fetch(BASE + path, { method: "POST", headers, body: JSON.stringify(body) });
  const ms = Date.now() - t0;
  const json = await r.json();
  if (!r.ok) { console.error(`HTTP ${r.status} ${path}:`, JSON.stringify(json).slice(0, 300)); process.exit(1); }
  return { ms, json };
}
const mean = (a) => a.reduce((x, y) => x + y, 0) / a.length;
const sleep = (s) => new Promise((r) => setTimeout(r, s * 1000));

const { ms } = await post("/v1/healthz", {});
console.log(`healthz ${ms} ms`);
const v = await fetch(BASE + "/v1/version").then((r) => r.json());
console.log(`version: backend=${v?.backend} model=${v?.model_dir} profile=${v?.profile} T=${v?.T}`);

// warmup (first call pays cold model load if the Space just woke)
const w = await post("/v1/systemone", baseQ(" and nobody replied."));
console.log(`warmup: ${w.ms} ms -> ${w.json?.answers?.route?.choice}`);

// single-question latency
const lat = [];
for (let i = 0; i < N; i++) {
  const r = await post("/v1/systemone", baseQ(` and nobody replied (${i}).`));
  lat.push(r.ms); const a = r.json.answers;
  console.log(`q${i}: ${r.ms} ms  route=${a.route.choice} angry=${a.angry.noul.toFixed(2)} score=${a.urgency.score.toFixed(2)} tok=${r.json.usage.input_tokens}`);
  await sleep(0.2);
}

// batched (one pass, many questions)
const multi = { state: baseQ(".").state, questions: {} };
for (let i = 0; i < B; i++) multi.questions[`q${i}`] = { type: "choice", instructions: `Pick a team for case ${i}.`, criteria: { billing: null, shipping: null, technical: null } };
const b = await post("/v1/systemone", multi);
console.log(`\nbatch ${B} questions: ${b.ms} ms  (${(b.ms - w.ms).toFixed(0)} ms extra vs warmup single)`);

console.log(`\nsummary: single-question mean ${Math.round(mean(lat))} ms over ${N}  |  batch(${B}) ${b.ms} ms`);