Skip to content
pi-c Artifact [52abd9e408]
Login

Artifact [52abd9e408]

Artifact 52abd9e4086b9ef21c47feb1b4d8d42d33dd369a268a93985af1063fb4db6283:


/* 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
 *   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 "pic.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);
    }
}

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_process(argv[1]);
    (void)snprintf(cmd, sizeof cmd, "rm -rf -- '%s'", dir);
    return system(cmd) == 0 ? 0 : 1;
}