public snapshot · system/tools/nib/bench/xlang/ · system@main · commit a92f56ab098e · shared 2026-11-22T22:43:57Z· frozen: this page does not change when the repository does
all files
  1#!/usr/bin/env bash
  2# Cross-language benchmarks: nib (native), Go, Rust. Linux, 16 hardware threads (cores 0-7 serve, 8-15 load).
  3#
  4#   bash run.sh            # build, check, time the eight programs, the parallel one, load the servers
  5#   bash run.sh cpu        # only the eight programs
  6#   bash run.sh par        # only the parallel mandelbrot (cores 0-7)
  7#   bash run.sh state      # only the accounts servers (shared per-key state)
  8#   bash run.sh http       # only the servers
  9#
 10# Needs: nibc built (dune build) with NIB_CLANG set, go, cargo, hyperfine, wrk,
 11# GNU time, taskset — run inside `nix-shell -p go hyperfine wrk time` from the
 12# nib package shell. Results go to results/<UTC>/ (git-ignored); summarize.py
 13# turns them into the report's tables.
 14set -euo pipefail
 15here=$(cd "$(dirname "$0")" && pwd)
 16cd "$here"
 17nibc=${NIBC:-$here/../../_build/default/lib/nibc.exe}
 18export NIBC_STD=${NIBC_STD:-$here/../../std}
 19what=${1:-all}
 20stamp=$(date -u +%Y%m%dT%H%M%SZ)
 21out=results/$stamp
 22mkdir -p bin input "$out"
 23progs="binarytrees fannkuchredux spectralnorm nbody mandelbrot fasta knucleotide revcomp"
 24
 25echo "== build"
 26for p in $progs mandelpar; do "$nibc" build --target native --wire Main -o "bin/nib-$p" "nib/$p.nib" >/dev/null; done
 27"$nibc" build --target native --wire Serve -o bin/nib-httpserver nib/httpserver.nib >/dev/null
 28"$nibc" build --target native --wire Serve -o bin/nib-accounts nib/accounts.nib >/dev/null
 29"$nibc" build --target native --wire ServeHeavy -o bin/nib-accounts-heavy nib/accounts.nib >/dev/null
 30(cd go && for p in $progs mandelpar httpserver accounts; do go build -o "../bin/go-$p" "./$p"; done)
 31(cd rust && cargo build --release -q && for p in $progs mandelpar; do cp "target/release/$p" "../bin/rust-$p"; done)
 32(cd rust-http && cargo build --release -q && cp target/release/xlang-http ../bin/rust-httpserver && cp target/release/accounts ../bin/rust-accounts)
 33
 34arg_of() { case "$1" in knucleotide|revcomp) echo "$here/input/fasta.txt";; *) echo "";; esac; }
 35
 36if [ "$what" = all ] || [ "$what" = cpu ]; then
 37  bin/rust-fasta > input/fasta.txt
 38  echo "== check: each program's output equals Rust's"
 39  for p in $progs; do
 40    a=$(arg_of "$p")
 41    bin/rust-$p $a > "$out/$p.rust.out"
 42    for l in go nib; do
 43      bin/$l-$p $a > "$out/$p.$l.out"
 44      if cmp -s "$out/$p.rust.out" "$out/$p.$l.out"; then echo "  $p $l: same"; else echo "  $p $l: DIFFERENT"; exit 1; fi
 45    done
 46  done
 47  echo "== time (hyperfine: 1 warmup, 5 runs) and peak memory"
 48  for p in $progs; do
 49    a=$(arg_of "$p")
 50    for l in rust go nib; do
 51      hyperfine -N -w 1 -r 5 --output=null --export-json "$out/$p.$l.json" "bin/$l-$p $a" >/dev/null
 52      command time -f "%M" -o "$out/$p.$l.rss" "bin/$l-$p" $a >/dev/null
 53      echo "  $p $l: $(python3 -c "import json;r=json.load(open('$out/$p.$l.json'))['results'][0];print('%.3fs ±%.3f' % (r['median'], r['stddev']))") $(cat "$out/$p.$l.rss") KiB"
 54    done
 55  done
 56fi
 57
 58if [ "$what" = all ] || [ "$what" = par ]; then
 59  # Mandelbrot with its rows on every core: Rust and Go start a thread per
 60  # core taking rows from a shared counter; nib's is a `Vec.map` over rows,
 61  # which the compiler runs on its pool. Each sees the eight cores taskset
 62  # leaves it and sizes itself to them.
 63  echo "== parallel: mandelbrot on cores 0-7 (output checked against serial Rust's)"
 64  bin/rust-mandelbrot > "$out/mandelbrot.rust.out"
 65  for l in rust go nib; do
 66    taskset -c 0-7 bin/$l-mandelpar > "$out/mandelpar.$l.out"
 67    cmp -s "$out/mandelbrot.rust.out" "$out/mandelpar.$l.out" || { echo "  mandelpar $l: DIFFERENT"; exit 1; }
 68    hyperfine -N -w 1 -r 5 --output=null --export-json "$out/mandelpar.$l.json" "taskset -c 0-7 bin/$l-mandelpar" >/dev/null
 69    echo "  mandelpar $l: $(python3 -c "import json;r=json.load(open('$out/mandelpar.$l.json'))['results'][0];print('%.3fs ±%.3f' % (r['median'], r['stddev']))")"
 70  done
 71fi
 72
 73if [ "$what" = all ] || [ "$what" = state ]; then
 74  # Shared per-key state: 1024 accounts, GET /a/<id> deposits and answers the
 75  # sum of the account's recent deposits (32 kept, then 4096: WINDOW and nib's
 76  # ServeHeavy wire). Rust and Go run with one lock for the map and with 16
 77  # hash shards (SHARDS). Every server's answers to one sequence are compared.
 78  echo "== state: accounts servers on cores 0-7, wrk (4 threads, accounts.lua) on cores 8-15, 15 s per point"
 79  state_run() { # label binary port env...
 80    label=$1; b=$2; port=$3; shift 3
 81    env "$@" taskset -c 0-7 "bin/$b" > "$out/state.$label.log" 2>&1 &
 82    pid=$!
 83    sleep 1
 84    seq=$(for i in 5 5 5 1023 12 7 5; do curl -s "http://127.0.0.1:$port/a/$i"; echo -n " "; done)
 85    [ "$seq" = "2 4 6 5 3 2 8 " ] || { echo "  $label: answers '$seq' differ"; kill $pid; exit 1; }
 86    for c in 64 256; do
 87      taskset -c 8-15 wrk -t4 -c$c -d15s --latency -s accounts.lua "http://127.0.0.1:$port" > "$out/state.$label.c$c.txt" 2>&1 || true
 88      echo "  $label c=$c: $(grep -E 'Requests/sec' "$out/state.$label.c$c.txt" | awk '{print $2}') req/s, p99 $(grep -E '^ +99%' "$out/state.$label.c$c.txt" | awk '{print $2}'), $(grep -cE 'Non-2xx|Socket errors' "$out/state.$label.c$c.txt") error lines"
 89    done
 90    kill $pid 2>/dev/null || true
 91    wait $pid 2>/dev/null || true
 92  }
 93  for w in 32 4096; do
 94    nb=nib-accounts; [ $w = 4096 ] && nb=nib-accounts-heavy
 95    state_run "nib-w$w" $nb 18450 NIB_CORES=8
 96    state_run "rust-1lock-w$w" rust-accounts 18452 TOKIO_WORKER_THREADS=8 SHARDS=1 WINDOW=$w
 97    state_run "rust-16locks-w$w" rust-accounts 18452 TOKIO_WORKER_THREADS=8 SHARDS=16 WINDOW=$w
 98    state_run "go-1lock-w$w" go-accounts 18451 GOMAXPROCS=8 SHARDS=1 WINDOW=$w
 99    state_run "go-16locks-w$w" go-accounts 18451 GOMAXPROCS=8 SHARDS=16 WINDOW=$w
100  done
101fi
102
103if [ "$what" = all ] || [ "$what" = http ]; then
104  echo "== http: server on cores 0-7, wrk (4 threads) on cores 8-15, 15 s per point"
105  for l in rust go nib; do
106    case $l in rust) port=18442;; go) port=18441;; nib) port=18440;; esac
107    GOMAXPROCS=8 TOKIO_WORKER_THREADS=8 NIB_CORES=8 taskset -c 0-7 "bin/$l-httpserver" > "$out/http.$l.log" 2>&1 &
108    pid=$!
109    sleep 1
110    for path in plaintext json; do
111      for c in 64 256; do
112        taskset -c 8-15 wrk -t4 -c$c -d15s --latency "http://127.0.0.1:$port/$path" > "$out/http.$l.$path.c$c.txt" 2>&1 || true
113        echo "  $l /$path c=$c: $(grep -E 'Requests/sec' "$out/http.$l.$path.c$c.txt" | awk '{print $2}') req/s, p99 $(grep -E '^ +99%' "$out/http.$l.$path.c$c.txt" | awk '{print $2}'), $(grep -cE 'Non-2xx|Socket errors' "$out/http.$l.$path.c$c.txt") error lines"
114      done
115    done
116    kill $pid 2>/dev/null || true
117    wait $pid 2>/dev/null || true
118  done
119fi
120echo "results: $out"