Artifact c3850715605e3332014d6ae3dcaec7b0fca910c35a1fade1ea85dc70ccedeec6:
- File
bench/pic_bench.c
— part of check-in
[0e4ade46bb]
at
2026-11-19 01:15:51
on branch stage-4
— Benchmark each decoder and parse a chat chunk in one cached query
make bench-pic now decodes one realistic reply (500 text deltas and a 50-piece tool call) per API. It showed the Chat Completions decoder at 9.8ms against 1.3ms for the Messages API decoder, because it compiled about ten statements per chunk; one cached query per chunk and per tool delta brings it to 1.9ms. The baseline gains the decoder rows. (user: dmestas, size: 16150) [annotate] [blame] [check-ins using] [more...]
/* pic benchmarks: what pic itself adds per tool call, on the release build. * tool/... the tool alone (no journal), by input size * journal/... one durable commit, as each tool call makes two * turn/... one scripted turn with the tool, journal and all * decode/... one streamed reply (500 text deltas, a 50-piece tool call) per API * process/... pic start-up: open, recover and check an existing journal * Prints "name<TAB>median_us<TAB>p95_us<TAB>runs" per measurement. * Usage: pic_bench PIC_BINARY [SCALE] (SCALE multiplies the run counts) */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/wait.h> #include <time.h> #include <unistd.h> #include "anthropic.h" #include "openai_chat.h" #include "openai_responses.h" #include "pic.h" #include "sse.h" static char dir[256]; static int scale = 1; static double now_us(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (double)ts.tv_sec * 1e6 + (double)ts.tv_nsec / 1e3; } static int cmp_double(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; } static void report(const char *name, double *t, int n) { qsort(t, (size_t)n, sizeof *t, cmp_double); printf("%s\t%.0f\t%.0f\t%d\n", name, t[n / 2], t[(n * 95) / 100 < n ? (n * 95) / 100 : n - 1], n); fflush(stdout); } static int no_journal(void *ctx, const char *plan, size_t len) { (void)ctx, (void)plan, (void)len; return 0; } static void make_file(const char *name, size_t bytes) { char path[512]; FILE *f; (void)snprintf(path, sizeof path, "%s/%s", dir, name); f = fopen(path, "w"); for (size_t i = 0; f && i < bytes; i++) { fputc(i % 80 == 79 ? '\n' : 'a' + (int)(i % 26), f); } if (f) { fclose(f); } } /* Runs one tool call `runs` times with no journal and reports it. */ static void bench_tool(const char *name, const char *tool, const pic_tool_args *a, int runs, void (*before)(void)) { pic_tool_hooks h = {30, no_journal, NULL, NULL, 0}; double *t = calloc((size_t)runs, sizeof *t); for (int i = 0; t && i < runs; i++) { int is_error = 0; double t0; char *out; if (before) { before(); } t0 = now_us(); out = pic_tool_run(dir, tool, a, &h, &is_error); t[i] = now_us() - t0; if (!out || is_error) { fprintf(stderr, "%s failed: %s\n", name, out ? out : "(null)"); exit(1); } free(out); } report(name, t, runs); free(t); } /* Edits flip a marker back and forth, so every run changes the file. */ static int flip; static const char *old_text[1], *new_text[1]; static void flip_edit(void) { old_text[0] = flip ? "MARKER-B" : "MARKER-A"; new_text[0] = flip ? "MARKER-A" : "MARKER-B"; flip = !flip; } static void bench_tools(void) { static const struct { const char *name, *file; size_t bytes; int runs; } sizes[] = {{"1KB", "s.txt", 1024, 200}, {"100KB", "m.txt", 100 * 1024, 100}, {"10MB", "l.txt", 10u << 20, 30}}; char content[100 * 1024 + 1]; memset(content, 'w', sizeof content - 1); content[sizeof content - 1] = '\0'; for (size_t i = 0; i < sizeof sizes / sizeof sizes[0]; i++) { char name[64], path[512]; pic_tool_args a; FILE *f; make_file(sizes[i].file, sizes[i].bytes); (void)snprintf(path, sizeof path, "%s/%s", dir, sizes[i].file); f = fopen(path, "a"); if (f) { fputs("\nMARKER-A\n", f); fclose(f); } memset(&a, 0, sizeof a); a.path = sizes[i].file; (void)snprintf(name, sizeof name, "tool/read/%s", sizes[i].name); bench_tool(name, "read", &a, sizes[i].runs * scale, NULL); a.nedits = 1; a.old_text = old_text; a.new_text = new_text; flip = 0; (void)snprintf(name, sizeof name, "tool/edit/%s", sizes[i].name); bench_tool(name, "edit", &a, sizes[i].runs * scale, flip_edit); } { pic_tool_args a; memset(&a, 0, sizeof a); a.path = "w.txt"; content[1024] = '\0'; a.content = content; bench_tool("tool/write/1KB", "write", &a, 200 * scale, NULL); content[1024] = 'w'; a.content = content; bench_tool("tool/write/100KB", "write", &a, 100 * scale, NULL); memset(&a, 0, sizeof a); a.command = "true"; bench_tool("tool/bash/true", "bash", &a, 100 * scale, NULL); } } static void bench_journal(void) { pic_store s; char path[512]; int runs = 200 * scale; double *t = calloc((size_t)runs, sizeof *t); (void)snprintf(path, sizeof path, "%s/journal.sqlite", dir); if (!t || pic_open(&s, path) != 0) { fprintf(stderr, "journal: %s\n", s.err); exit(1); } for (int i = 0; i < runs; i++) { double t0 = now_us(); if (pic_begin(&s) != 0 || pic_exec(&s, "INSERT OR REPLACE INTO pic_meta(key,value) VALUES('bench', hex(randomblob(16)))") != 0 || pic_commit(&s) != 0) { fprintf(stderr, "journal: %s\n", s.err); exit(1); } t[i] = now_us() - t0; } report("journal/commit", t, runs); pic_close(&s); free(t); } /* Runs the pic binary and waits; returns microseconds or -1. */ static double run_pic(char *const argv[]) { double t0 = now_us(); int status = 0; pid_t pid = fork(); if (pid == 0) { (void)freopen("/dev/null", "w", stdout); execv(argv[0], argv); _exit(127); } if (pid < 0 || waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { return -1; } return now_us() - t0; } /* One scripted turn per call: submit, then run a one-call script, timed as * the run only. Each run uses a fresh journal so turns do not accumulate. */ static void bench_turns(char *pic) { static const struct { const char *name, *line; } turns[] = { {"turn/read", "tool raw read {\"path\":\"s.txt\"}"}, {"turn/write", "tool write t.txt hello"}, {"turn/edit", "tool raw edit {\"path\":\"e.txt\",\"edits\":[{\"oldText\":\"one\",\"newText\":\"two\"}]}"}, {"turn/bash", "tool bash true"}, {"turn/answer-only", "text done"}, }; for (size_t k = 0; k < sizeof turns / sizeof turns[0]; k++) { int runs = 30 * scale; double *t = calloc((size_t)runs, sizeof *t); char script[512], db[512], ep[512]; FILE *f; (void)snprintf(script, sizeof script, "%s/turn.script", dir); f = fopen(script, "w"); if (!f || !t) { exit(1); } fprintf(f, "%s\ntext done\n", turns[k].line); fclose(f); for (int i = 0; i < runs; i++) { char *submit[] = {pic, "--workspace", dir, "--db", db, "submit", "k", "go", NULL}; char *run[] = {pic, "--workspace", dir, "--db", db, "run", "--script", script, NULL}; (void)snprintf(db, sizeof db, "%s/turn-%zu-%d.sqlite", dir, k, i); (void)snprintf(ep, sizeof ep, "%s/e.txt", dir); f = fopen(ep, "w"); if (f) { fputs("one\n", f); fclose(f); } if (run_pic(submit) < 0 || (t[i] = run_pic(run)) < 0) { fprintf(stderr, "%s failed\n", turns[k].name); exit(1); } } report(turns[k].name, t, runs); free(t); } } /* ---- decoders: one realistic streamed reply, decoded end to end -------- */ #define DELTAS 500 #define ARG_PIECES 50 static void put(sqlite3_str *s, const char *event, const char *data) { if (event) { sqlite3_str_appendf(s, "event: %s\n", event); } sqlite3_str_appendf(s, "data: %s\n\n", data); } /* 500 text deltas, then one tool call whose arguments arrive in 50 pieces. */ static char *stream_anthropic(void) { sqlite3_str *s = sqlite3_str_new(NULL); put(s, "message_start", "{\"type\":\"message_start\",\"message\":{\"id\":\"m\",\"model\":\"x\",\"usage\":{\"input_tokens\":1}}}"); put(s, "content_block_start", "{\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}"); for (int i = 0; i < DELTAS; i++) { put(s, "content_block_delta", "{\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"word \"}}"); } put(s, "content_block_stop", "{\"type\":\"content_block_stop\",\"index\":0}"); put(s, "content_block_start", "{\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"t\",\"name\":\"write\",\"input\":{}}}"); put(s, "content_block_delta", "{\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"path\\\":\\\"a\\\",\\\"content\\\":\\\"\"}}"); for (int i = 0; i < ARG_PIECES; i++) { put(s, "content_block_delta", "{\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"abcdefgh\"}}"); } put(s, "content_block_delta", "{\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"\\\"}\"}}"); put(s, "content_block_stop", "{\"type\":\"content_block_stop\",\"index\":1}"); put(s, "message_delta", "{\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"output_tokens\":9}}"); put(s, "message_stop", "{\"type\":\"message_stop\"}"); return sqlite3_str_finish(s); } static char *stream_chat(void) { sqlite3_str *s = sqlite3_str_new(NULL); for (int i = 0; i < DELTAS; i++) { put(s, NULL, "{\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"word \"},\"finish_reason\":null}]}"); } put(s, NULL, "{\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\"," "\"function\":{\"name\":\"write\",\"arguments\":\"{\\\"path\\\":\\\"a\\\",\\\"content\\\":\\\"\"}}]},\"finish_reason\":null}]}"); for (int i = 0; i < ARG_PIECES; i++) { put(s, NULL, "{\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"abcdefgh\"}}]}," "\"finish_reason\":null}]}"); } put(s, NULL, "{\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"}\"}}]}," "\"finish_reason\":\"tool_calls\"}]}"); put(s, NULL, "[DONE]"); return sqlite3_str_finish(s); } static char *stream_responses(void) { sqlite3_str *s = sqlite3_str_new(NULL); sqlite3_str *text = sqlite3_str_new(NULL); char *joined; put(s, "response.created", "{\"type\":\"response.created\",\"response\":{\"id\":\"r\",\"status\":\"in_progress\",\"output\":[]}}"); for (int i = 0; i < DELTAS; i++) { put(s, "response.output_text.delta", "{\"type\":\"response.output_text.delta\",\"output_index\":0,\"delta\":\"word \"}"); sqlite3_str_appendall(text, "word "); } joined = sqlite3_str_finish(text); sqlite3_str_appendf(s, "event: response.output_item.done\ndata: {\"type\":\"response.output_item.done\",\"output_index\":0," "\"item\":{\"type\":\"message\",\"id\":\"msg\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\"," "\"text\":\"%s\"}]}}\n\n", joined); for (int i = 0; i < ARG_PIECES; i++) { put(s, "response.function_call_arguments.delta", "{\"type\":\"response.function_call_arguments.delta\",\"output_index\":1,\"delta\":\"abcdefgh\"}"); } put(s, "response.output_item.done", "{\"type\":\"response.output_item.done\",\"output_index\":1,\"item\":{\"type\":\"function_call\",\"id\":\"fc\"," "\"call_id\":\"call_1\",\"name\":\"write\",\"arguments\":\"{\\\"path\\\":\\\"a\\\"}\"}}"); put(s, "response.completed", "{\"type\":\"response.completed\",\"response\":{\"id\":\"r\",\"status\":\"completed\"}}"); sqlite3_free(joined); return sqlite3_str_finish(s); } /* Decodes STREAM `runs` times, fed in 512-byte reads as a network would. */ static void bench_decoder(sqlite3 *db, const char *name, const char *stream, int which, int runs) { double *t = calloc((size_t)runs, sizeof *t); size_t n = strlen(stream); for (int i = 0; t && i < runs; i++) { pic_anthropic a; pic_chat c; pic_responses r; pic_sse p; int rc = 0; double t0 = now_us(); if (which == 0) { rc = pic_anthropic_init(&a, db) || pic_sse_init(&p, 1u << 20, 16u << 20, pic_anthropic_event, &a); } else if (which == 1) { rc = pic_chat_init(&c, db) || pic_sse_init(&p, 1u << 20, 16u << 20, pic_chat_event, &c); } else { rc = pic_responses_init(&r, db) || pic_sse_init(&p, 1u << 20, 16u << 20, pic_responses_event, &r); } for (size_t off = 0; rc == 0 && off < n; off += 512) { rc = pic_sse_feed(&p, stream + off, n - off < 512 ? n - off : 512); } if (rc == 0) { char *x = NULL, *y = NULL; if (which == 0) { rc = pic_anthropic_finish(&a) || !(x = pic_neutral_from_anthropic(db, a.msg.content)); } else if (which == 1) { rc = pic_chat_finish(&c) || !(x = pic_chat_native(&c)) || !(y = pic_chat_neutral(&c)); } else { rc = pic_responses_finish(&r) || !(x = pic_responses_native(&r)) || !(y = pic_responses_neutral(&r)); } free(x); free(y); } t[i] = now_us() - t0; pic_sse_free(&p); if (which == 0) { pic_anthropic_free(&a); } else if (which == 1) { pic_chat_free(&c); } else { pic_responses_free(&r); } if (rc != 0) { fprintf(stderr, "%s failed\n", name); exit(1); } } report(name, t, runs); free(t); } static void bench_decoders(void) { sqlite3 *db; char *a = stream_anthropic(), *c = stream_chat(), *r = stream_responses(); if (sqlite3_open(":memory:", &db) != SQLITE_OK || pic_transform_register(db) != 0 || !a || !c || !r) { exit(1); } bench_decoder(db, "decode/anthropic-550", a, 0, 50 * scale); bench_decoder(db, "decode/chat-550", c, 1, 50 * scale); bench_decoder(db, "decode/responses-550", r, 2, 50 * scale); sqlite3_free(a); sqlite3_free(c); sqlite3_free(r); sqlite3_close(db); } static void bench_process(char *pic) { char db[512]; int runs = 50 * scale; double *t = calloc((size_t)runs, sizeof *t); char *submit[] = {pic, "--workspace", dir, "--db", db, "submit", "k", "go", NULL}; char *check[] = {pic, "--workspace", dir, "--db", db, "check", NULL}; (void)snprintf(db, sizeof db, "%s/process.sqlite", dir); if (!t || run_pic(submit) < 0) { exit(1); } for (int i = 0; i < runs; i++) { if ((t[i] = run_pic(check)) < 0) { fprintf(stderr, "process/check failed\n"); exit(1); } } report("process/check", t, runs); free(t); } int main(int argc, char **argv) { const char *tmp = getenv("TMPDIR"); char cmd[600]; if (argc < 2) { fprintf(stderr, "usage: pic_bench PIC_BINARY [SCALE]\n"); return 2; } if (argc > 2) { scale = atoi(argv[2]) > 0 ? atoi(argv[2]) : 1; } (void)snprintf(dir, sizeof dir, "%s/pic-bench.XXXXXX", tmp && *tmp ? tmp : "/tmp"); if (!mkdtemp(dir)) { perror("mkdtemp"); return 1; } bench_tools(); bench_journal(); bench_turns(argv[1]); bench_decoders(); bench_process(argv[1]); (void)snprintf(cmd, sizeof cmd, "rm -rf -- '%s'", dir); return system(cmd) == 0 ? 0 : 1; }