aboutsummaryrefslogtreecommitdiff
path: root/bench
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-13 20:03:39 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-13 20:13:28 +0200
commit64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch)
tree85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /bench
downloadpyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz
pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip
Init
Diffstat (limited to 'bench')
-rw-r--r--bench/bench.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/bench/bench.c b/bench/bench.c
new file mode 100644
index 0000000..a33d2d4
--- /dev/null
+++ b/bench/bench.c
@@ -0,0 +1,158 @@
+/* Benchmark for one window and for a whole recording.
+ *
+ * The protocol matches tools/baseline.py so the numbers sit next to PyTorch's
+ * on the same machine: three warm-up windows, then twenty timed ones on real
+ * speech taken from the parity fixture, reporting mean, median and the slowest
+ * of the twenty.
+ *
+ * Real speech rather than noise matters here. The engine's cost is fixed by
+ * the shapes, but denormal cell states on near-silent input are the one thing
+ * that can change it, and noise would hide that. */
+
+#include "pyseg.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <time.h>
+
+#define WARMUP 3
+#define ITERS 20
+
+static double now_ms(void) {
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec * 1e3 + ts.tv_nsec / 1e6;
+}
+
+static int cmp_double(const void *a, const void *b) {
+ const double x = *(const double *)a, y = *(const double *)b;
+ return x < y ? -1 : x > y;
+}
+
+static float *load_windows(const char *path, unsigned *count) {
+ FILE *fp = fopen(path, "rb");
+ if (!fp)
+ return NULL;
+ char magic[4];
+ unsigned n, samples, frames;
+ if (fread(magic, 1, 4, fp) != 4 || memcmp(magic, "PSGG", 4) != 0 || fread(&n, 4, 1, fp) != 1 ||
+ fread(&samples, 4, 1, fp) != 1 || fread(&frames, 4, 1, fp) != 1 ||
+ samples != PYSEG_WINDOW_SAMPLES) {
+ fclose(fp);
+ return NULL;
+ }
+ const size_t len = (size_t)n * PYSEG_WINDOW_SAMPLES;
+ float *data = malloc(len * sizeof(float));
+ if (data && fread(data, sizeof(float), len, fp) != len) {
+ free(data);
+ data = NULL;
+ }
+ fclose(fp);
+ *count = n;
+ return data;
+}
+
+/* Times whole batches and reports per window, so batch sizes compare directly.
+ * The fixture holds fewer windows than some batches, so windows are reused;
+ * that is fine because the cost does not depend on the audio. */
+static void bench_backend(unsigned flags, int batch, const float *windows, unsigned count) {
+ const double t_init = now_ms();
+ pyseg_t *v = pyseg_create_batch(NULL, flags, batch);
+ const double init_ms = now_ms() - t_init;
+ if (!v) {
+ fprintf(stderr, "batch %d: create failed\n", batch);
+ return;
+ }
+
+ float *input = malloc((size_t)batch * PYSEG_WINDOW_SAMPLES * sizeof(float));
+ float *probs = malloc((size_t)batch * PYSEG_FRAMES * PYSEG_CLASSES * sizeof(float));
+ for (int b = 0; b < batch; ++b)
+ memcpy(input + (size_t)b * PYSEG_WINDOW_SAMPLES,
+ windows + (size_t)(b % count) * PYSEG_WINDOW_SAMPLES,
+ PYSEG_WINDOW_SAMPLES * sizeof(float));
+
+ for (int i = 0; i < WARMUP; ++i)
+ pyseg_process_batch_f32(v, input, batch, probs);
+
+ double times[ITERS];
+ for (int i = 0; i < ITERS; ++i) {
+ const double t = now_ms();
+ pyseg_process_batch_f32(v, input, batch, probs);
+ times[i] = (now_ms() - t) / batch;
+ }
+ qsort(times, ITERS, sizeof(double), cmp_double);
+
+ double sum = 0.0;
+ for (int i = 0; i < ITERS; ++i)
+ sum += times[i];
+ const double mean = sum / ITERS;
+
+ printf("window %-6s batch %2d init %5.2f ms mean/p50/max %6.2f / %6.2f / %6.2f ms"
+ " %6.1fx realtime %5.1f MB\n",
+ pyseg_backend_name(v), batch, init_ms, mean, times[ITERS / 2], times[ITERS - 1],
+ 10.0 / (mean / 1e3), pyseg_memory_bytes(v) / 1e6);
+
+ free(probs);
+ free(input);
+ pyseg_destroy(v);
+}
+
+int main(int argc, char **argv) {
+ if (argc < 2) {
+ fprintf(stderr, "usage: bench <golden.bin>\n");
+ return 2;
+ }
+ unsigned count = 0;
+ float *windows = load_windows(argv[1], &count);
+ if (!windows || count == 0) {
+ fprintf(stderr, "cannot read %s\n", argv[1]);
+ return 2;
+ }
+
+ for (int batch = 1; batch <= 8; batch *= 2)
+ bench_backend(0, batch, windows, count);
+ bench_backend(PYSEG_SCALAR_KERNELS, 1, windows, count);
+
+ /* The whole-recording path, which is what a server actually runs: the
+ * fixture's windows are one second apart, so concatenating their first
+ * seconds reconstructs the recording they came from. */
+ const size_t step_samples = PYSEG_SAMPLE_RATE;
+ const size_t num_samples = PYSEG_WINDOW_SAMPLES + (count - 1) * step_samples;
+ float *audio = malloc(num_samples * sizeof(float));
+ memcpy(audio, windows, PYSEG_WINDOW_SAMPLES * sizeof(float));
+ for (unsigned c = 1; c < count; ++c)
+ memcpy(audio + PYSEG_WINDOW_SAMPLES + (size_t)(c - 1) * step_samples,
+ windows + (size_t)c * PYSEG_WINDOW_SAMPLES + PYSEG_WINDOW_SAMPLES - step_samples,
+ step_samples * sizeof(float));
+
+ const size_t frames = pyseg_slide_frames(num_samples, 1.0);
+ float *tracks = malloc(frames * PYSEG_TRACKS * sizeof(float));
+
+ for (int threads = 1; threads <= 16; threads *= 2) {
+ pyseg_slide_params p;
+ pyseg_slide_params_default(&p);
+ p.threads = threads;
+ const double t = now_ms();
+ const size_t got = pyseg_slide(audio, num_samples, &p, tracks);
+ const double ms = now_ms() - t;
+ if (got == 0) {
+ fprintf(stderr, "slide failed at %d threads\n", threads);
+ break;
+ }
+ printf("slide %2d thr %6.1f s of audio in %8.1f ms %6.1fx realtime\n", threads,
+ (double)num_samples / PYSEG_SAMPLE_RATE, ms,
+ (double)num_samples / PYSEG_SAMPLE_RATE / (ms / 1e3));
+ }
+
+ struct rusage ru;
+ getrusage(RUSAGE_SELF, &ru);
+ printf("max rss %.1f MB\n", (double)ru.ru_maxrss / 1e6);
+
+ free(tracks);
+ free(audio);
+ free(windows);
+ return 0;
+}