/* 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
* context/... Pi's context estimate on a long conversation (~200 entries, 400 KB)
* request/... one full request on that conversation, per API, at a local server
* 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 <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "compact.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 = {.exec_seconds = 30, .started = no_journal}; /* designated: new hook fields default to off */
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) {
/* A failed redirect would let the child's output into the benchmark's
* own, so fail the child instead of running it unredirected. */
if (freopen("/dev/null", "w", stdout) == NULL) {
_exit(127);
}
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_anthropic_neutral(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);
}
/* ---- long conversations: what grows with the history ---------------- */
/* A short text answer per API, so a run makes exactly one request. */
static const char long_anthropic[] =
"event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"m\",\"model\":\"x\",\"usage\":{\"input_tokens\":1}}}\n\n"
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n"
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"done\"}}\n\n"
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
"event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":1}}\n\n"
"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n";
static const char long_chat[] =
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"done\"},\"finish_reason\":null}]}\n\n"
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n"
"data: [DONE]\n\n";
static const char long_responses[] =
"event: response.output_item.done\ndata: {\"type\":\"response.output_item.done\",\"output_index\":0,\"item\":{\"type\":\"message\","
"\"id\":\"m\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"done\",\"annotations\":[]}]}}\n\n"
"event: response.completed\ndata: {\"type\":\"response.completed\",\"response\":{\"id\":\"r\",\"status\":\"completed\",\"output\":"
"[{\"type\":\"message\",\"id\":\"m\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"done\","
"\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}\n\n";
/* A local HTTP server answering every request with BODY as an event stream;
* it exits on its own after ten minutes. Returns its pid; *port is set. */
static pid_t serve(const char *body, int *port) {
struct sockaddr_in a;
socklen_t len = sizeof a;
int fd = socket(AF_INET, SOCK_STREAM, 0), one = 1;
pid_t pid;
memset(&a, 0, sizeof a);
a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
if (fd < 0 || bind(fd, (struct sockaddr *)&a, sizeof a) != 0 || listen(fd, 16) != 0 ||
getsockname(fd, (struct sockaddr *)&a, &len) != 0) {
perror("serve");
exit(1);
}
*port = ntohs(a.sin_port);
pid = fork();
if (pid != 0) {
close(fd);
return pid;
}
(void)alarm(600);
for (;;) {
int c = accept(fd, NULL, NULL);
char *req = NULL, head[256];
size_t used = 0, cap = 0, want = 0;
if (c < 0) {
continue;
}
for (;;) { /* the headers, then Content-Length bytes of body */
char *end;
ssize_t n;
if (used + 65536 > cap) {
char *grown = realloc(req, cap = cap ? cap * 2 : 1 << 20);
if (!grown) {
_exit(1);
}
req = grown;
}
n = read(c, req + used, cap - used - 1);
if (n <= 0) {
break;
}
used += (size_t)n;
req[used] = '\0';
end = strstr(req, "\r\n\r\n");
if (end && !want) {
char *cl = strcasestr(req, "content-length:");
want = (size_t)(end + 4 - req) + (cl && cl < end ? strtoul(cl + 15, NULL, 10) : 0);
}
if (want && used >= want) {
break;
}
}
(void)snprintf(head, sizeof head,
"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n",
strlen(body));
if (write(c, head, strlen(head)) < 0 || write(c, body, strlen(body)) < 0) {
/* the client went away; nothing to do */
}
close(c);
free(req);
}
}
/* A conversation of 100 read calls on a 4 KB file (about 200 entries,
* 400 KB of tool output), then: Pi's context estimate in-process, and one
* full request (path, request body, stream, commit) per API. */
static void bench_long(char *pic) {
char db[512], script[512], file[512], url[128], cw[] = "--context-window", zero[] = "0";
static const struct {
const char *name, *model, *body, *path;
} apis[] = {
{"request/anthropic-long", "anthropic/bench", long_anthropic, "/v1/messages"},
{"request/chat-long", "openrouter/bench/model", long_chat, "/v1/chat/completions"},
{"request/responses-long", "openai/bench-model", long_responses, "/v1/responses"},
};
FILE *f;
(void)snprintf(db, sizeof db, "%s/long.sqlite", dir);
(void)snprintf(script, sizeof script, "%s/long.script", dir);
(void)snprintf(file, sizeof file, "%s/r.txt", dir);
make_file("r.txt", 4096);
f = fopen(script, "w");
if (!f) {
exit(1);
}
for (int i = 0; i < 100; i++) {
fputs("tool raw read {\"path\":\"r.txt\"}\n", f);
}
fputs("text done\n", f);
fclose(f);
{
char *submit[] = {pic, "--workspace", dir, "--db", db, "submit", "k0", "start", NULL};
char *run[] = {pic, "--workspace", dir, "--db", db, "run", "--script", script, NULL};
if (run_pic(submit) < 0 || run_pic(run) < 0) {
fprintf(stderr, "long: could not build the conversation\n");
exit(1);
}
}
{
pic_store s;
sqlite3_int64 sub = 0;
sqlite3_stmt *q = NULL;
int runs = 30 * scale;
double *t = calloc((size_t)runs, sizeof *t);
char *submit[] = {pic, "--workspace", dir, "--db", db, "submit", "k1", "go", "--session", "latest", NULL};
if (!t || run_pic(submit) < 0 || pic_open(&s, db) != 0 ||
sqlite3_prepare_v2(s.db, "SELECT max(id) FROM pic_submission", -1, &q, NULL) != SQLITE_OK ||
sqlite3_step(q) != SQLITE_ROW) {
fprintf(stderr, "long: estimate setup\n");
exit(1);
}
sub = sqlite3_column_int64(q, 0);
sqlite3_finalize(q);
for (int i = 0; i < runs; i++) {
long tokens = 0;
double t0 = now_us();
if (pic_context_tokens(s.db, sub, &tokens) != 0 || tokens < 100000) {
fprintf(stderr, "long: estimate %ld\n", tokens);
exit(1);
}
t[i] = now_us() - t0;
}
report("context/estimate-long", t, runs);
pic_close(&s);
free(t);
{
char *cancel[] = {pic, "--workspace", dir, "--db", db, "cancel", "k1", NULL};
if (run_pic(cancel) < 0) {
exit(1);
}
}
}
(void)setenv("ANTHROPIC_API_KEY", "bench", 1);
(void)setenv("OPENROUTER_API_KEY", "bench", 1);
(void)setenv("OPENAI_API_KEY", "bench", 1);
(void)setenv("PIC_PI_EXPORT", "0", 1);
for (size_t k = 0; k < sizeof apis / sizeof apis[0]; k++) {
int port = 0, runs = 20 * scale;
double *t = calloc((size_t)runs, sizeof *t);
pid_t server = serve(apis[k].body, &port);
char model[64];
(void)snprintf(url, sizeof url, "http://127.0.0.1:%d%s", port, apis[k].path);
(void)snprintf(model, sizeof model, "%s", apis[k].model);
for (int i = 0; t && i < runs; i++) {
char key[32];
char *submit[] = {pic, "--workspace", dir, "--db", db, "submit", key, "go", "--session", "latest", NULL};
char *run[] = {pic, "--workspace", dir, "--db", db, cw, zero, "run", "--model", model, "--api-url", url, NULL};
(void)snprintf(key, sizeof key, "%s-%d", apis[k].name, i);
if (run_pic(submit) < 0 || (t[i] = run_pic(run)) < 0) {
fprintf(stderr, "%s failed\n", apis[k].name);
kill(server, SIGKILL);
exit(1);
}
}
kill(server, SIGKILL);
(void)waitpid(server, NULL, 0);
report(apis[k].name, t, runs);
free(t);
}
}
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_long(argv[1]);
bench_process(argv[1]);
(void)snprintf(cmd, sizeof cmd, "rm -rf -- '%s'", dir);
return system(cmd) == 0 ? 0 : 1;
}