#!/bin/bash # Benchmark: moxie (legacy) vs moxie-iskra (lattice) compilation # Measures wall time, output size, and match rate across the smesh corpus. # Run with RUNS=N to control iterations (default 5). # Run with DROP_CACHE=1 to drop filesystem caches between runs (needs sudo). set -e SMESH="${SMESH:-$HOME/s/smesh}" MOXIE="${MOXIE:-$HOME/s/moxie}" RUNS="${RUNS:-5}" DROP_CACHE="${DROP_CACHE:-0}" OUT="/tmp/bench-compile" export MOXIEROOT="$MOXIE" export GOWORK=off rm -rf "$OUT" mkdir -p "$OUT" median() { sort -n | awk '{a[NR]=$1} END {print a[int((NR+1)/2)]}' } drop_caches() { if [ "$DROP_CACHE" = "1" ]; then sync echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null 2>&1 || true fi } run_timed() { local label="$1"; shift local times="" for i in $(seq 1 "$RUNS"); do drop_caches t0=$(date +%s%N) "$@" >/dev/null 2>/dev/null || true t1=$(date +%s%N) ms=$(( (t1 - t0) / 1000000 )) times="$times $ms" done med=$(echo "$times" | tr ' ' '\n' | grep -v '^$' | median) min=$(echo "$times" | tr ' ' '\n' | grep -v '^$' | sort -n | head -1) max=$(echo "$times" | tr ' ' '\n' | grep -v '^$' | sort -n | tail -1) printf " %-45s %6d ms (min %d, max %d)\n" "$label" "$med" "$min" "$max" } echo "=== Benchmark: moxie vs moxie-iskra ===" echo " smesh: $SMESH" echo " moxie: $MOXIE" echo " runs: $RUNS" echo " drop_cache: $DROP_CACHE" echo "" # --- Source stats --- webmx=$(find "$SMESH/web" -name '*.mx' -not -name '*_test.mx' | wc -l) weblines=$(find "$SMESH/web" -name '*.mx' -not -name '*_test.mx' -exec cat {} + | wc -l) nativemx=$(find "$SMESH" -maxdepth 1 -name '*.mx' -not -name '*_test.mx' | wc -l) nativemx=$((nativemx + $(find "$SMESH/pkg" -name '*.mx' -not -name '*_test.mx' 2>/dev/null | wc -l))) nativelines=$({ find "$SMESH" -maxdepth 1 -name '*.mx' -not -name '*_test.mx' -exec cat {} +; find "$SMESH/pkg" -name '*.mx' -not -name '*_test.mx' -exec cat {} +; } 2>/dev/null | wc -l) echo "Source corpus:" echo " web frontend: $webmx files, $weblines lines" echo " native relay: $nativemx files, $nativelines lines" echo "" # --- 1. Native relay --- echo "--- Native relay build ---" run_timed "moxie build (native relay)" \ sh -c "cd $SMESH && moxie build -o $OUT/relay-moxie ." relayMoxie=$(stat -c%s "$OUT/relay-moxie" 2>/dev/null || echo 0) run_timed "moxie-iskra build (native relay)" \ sh -c "cd $SMESH && moxie-iskra build -o $OUT/relay-iskra . 2>/dev/null" relayIskra=$(stat -c%s "$OUT/relay-iskra" 2>/dev/null || echo 0) printf " moxie output: %d bytes (%d KB)\n" "$relayMoxie" $((relayMoxie/1024)) printf " moxie-iskra output: %d bytes (%d KB)\n" "$relayIskra" $((relayIskra/1024)) echo "" # --- 2. Web frontend: moxiejs (legacy JS transpiler) --- echo "--- Web frontend: moxiejs (JS output) ---" run_timed "moxiejs (web/app -> JS)" \ sh -c "cd $SMESH && GOWORK=off moxiejs -runtime $MOXIE/jsruntime -o $OUT/js/ ./web/app/" jssize=$(find "$OUT/js" -name '*.mjs' -exec cat {} + 2>/dev/null | wc -c) jsgz=$(find "$OUT/js" -name '*.mjs' -exec cat {} + 2>/dev/null | gzip -c | wc -c) printf " output: %d bytes (%d KB), gzipped: %d bytes (%d KB)\n" "$jssize" $((jssize/1024)) "$jsgz" $((jsgz/1024)) echo "" # --- 3. Web frontend: moxie-iskra wasm32 --- echo "--- Web frontend: moxie-iskra (WASM output) ---" run_timed "moxie-iskra build -target wasm32 (web/app)" \ sh -c "cd $SMESH && moxie-iskra build -target wasm32 -o $OUT/app.wasm web/app/ 2>/dev/null" wasmsize=$(stat -c%s "$OUT/app.wasm" 2>/dev/null || echo 0) wasmgz=$(gzip -c "$OUT/app.wasm" 2>/dev/null | wc -c) printf " output: %d bytes (%d KB), gzipped: %d bytes (%d KB)\n" "$wasmsize" $((wasmsize/1024)) "$wasmgz" $((wasmgz/1024)) # wasm-opt if command -v wasm-opt >/dev/null 2>&1; then wasm-opt -Oz --strip-debug "$OUT/app.wasm" -o "$OUT/app.opt.wasm" 2>/dev/null optsize=$(stat -c%s "$OUT/app.opt.wasm" 2>/dev/null || echo 0) optgz=$(gzip -c "$OUT/app.opt.wasm" 2>/dev/null | wc -c) printf " optimized: %d bytes (%d KB), gzipped: %d bytes (%d KB)\n" "$optsize" $((optsize/1024)) "$optgz" $((optgz/1024)) fi echo "" # --- 4. Size comparison --- echo "=== Size comparison ===" printf " %-35s %8s %8s\n" "" "raw" "gzipped" relayMoxieGz=0 relayIskraGz=0 if [ -f "$OUT/relay-moxie" ]; then relayMoxieGz=$(gzip -c "$OUT/relay-moxie" | wc -c); fi if [ -f "$OUT/relay-iskra" ]; then relayIskraGz=$(gzip -c "$OUT/relay-iskra" | wc -c); fi printf " %-35s %7dK %7dK\n" "native: moxie" $((relayMoxie/1024)) $((relayMoxieGz/1024)) printf " %-35s %7dK %7dK\n" "native: moxie-iskra" $((relayIskra/1024)) $((relayIskraGz/1024)) printf " %-35s %7dK %7dK\n" "web: moxiejs (JS)" $((jssize/1024)) $((jsgz/1024)) printf " %-35s %7dK %7dK\n" "web: moxie-iskra (WASM)" $((wasmsize/1024)) $((wasmgz/1024)) if [ -f "$OUT/app.opt.wasm" ]; then printf " %-35s %7dK %7dK\n" "web: moxie-iskra (WASM, -Oz)" $((optsize/1024)) $((optgz/1024)) fi if [ "$jsgz" -gt 0 ] && [ "$optgz" -gt 0 ]; then ratio=$((jsgz * 100 / optgz)) printf "\n WASM (-Oz, gzipped) is %d.%dx smaller than JS (gzipped)\n" $((ratio/100)) $(( (ratio/10) % 10)) fi echo "" rm -rf "$OUT" echo "done."