brewery
notashelf /
4ea090cf330d87b264b6d554a8307347c46f7e56

nixir

public

Import-resolving Nix IR plugin

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/nixir.git
tests/benchmark/run.shBash119 lines3.1 KB
1
#!/usr/bin/env bash
2
set -e
3
4
echo "# Running benchmarks..."
5
echo ""
6
7
BENCH_DIR="$(pwd)/tests/benchmark"
8
IRC_BIN="$(pwd)/build/nix-irc"
9
10
GREEN='\033[0;32m'
11
BLUE='\033[0;34m'
12
YELLOW='\033[0;33m'
13
NC='\033[0m'
14
15
get_ms() {
16
	local time_str="$1"
17
	if [[ $time_str =~ ([0-9]+)m([0-9.]+)s ]]; then
18
		local mins="${BASH_REMATCH[1]}"
19
		local secs="${BASH_REMATCH[2]}"
20
		local ms
21
		ms=$(awk "BEGIN {printf \"%.1f\", ($mins * 60000) + ($secs * 1000)}")
22
		echo "$ms"
23
	else
24
		echo "0"
25
	fi
26
}
27
28
run_benchmark() {
29
	local name="$1"
30
	local file="$2"
31
32
	echo -e "${BLUE}=== $name ===${NC}"
33
	echo ""
34
35
	# Measure compilation time
36
	echo -n "  Compilation time:     "
37
	local compile_start
38
	compile_start=$(date +%s%N)
39
	"$IRC_BIN" "$file" /tmp/bench.nixir >/dev/null 2>&1
40
	local compile_end
41
	compile_end=$(date +%s%N)
42
	local compile_ms=$(((compile_end - compile_start) / 1000000))
43
	echo -e "${YELLOW}${compile_ms}ms${NC}"
44
45
	# Source and IR sizes
46
	local src_size
47
	src_size=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null)
48
	local ir_size
49
	ir_size=$(stat -c%s /tmp/bench.nixir 2>/dev/null || stat -f%z /tmp/bench.nixir 2>/dev/null)
50
	local ratio=$((ir_size * 100 / src_size))
51
	echo -e "  Source size:          ${src_size}B"
52
	echo -e "  IR bundle size:       ${ir_size}B (${ratio}% of source)"
53
54
	echo ""
55
56
	# Native Nix evaluation (baseline)
57
	echo -n "  Native Nix eval:      "
58
	local native_total=0
59
	for _ in {1..5}; do
60
		local t
61
		t=$( (time nix-instantiate --eval --strict "$file" >/dev/null 2>&1) 2>&1 | grep "real" | awk '{print $2}')
62
		local ms
63
		ms=$(get_ms "$t")
64
		native_total=$(awk "BEGIN {print $native_total + $ms}")
65
	done
66
	local native_avg
67
	native_avg=$(awk "BEGIN {printf \"%.1f\", $native_total / 5}")
68
	echo -e "${GREEN}${native_avg}ms${NC} avg (5 runs)"
69
70
	echo ""
71
}
72
73
echo "Measuring IR compilation speed and bundle size characteristics."
74
echo ""
75
76
run_benchmark "Simple Expression" "$BENCH_DIR/simple.nix"
77
run_benchmark "Medium Complexity" "$BENCH_DIR/medium.nix"
78
run_benchmark "Large Expression" "$BENCH_DIR/large.nix"
79
80
# Overall statistics
81
echo -e "${BLUE}=== Overall Statistics ===${NC}"
82
echo ""
83
84
testdir=$(mktemp -d)
85
total_nix=0
86
total_ir=0
87
total_compile_time=0
88
89
for f in "$BENCH_DIR"/*.nix; do
90
	nixsize=$(stat -c%s "$f" 2>/dev/null || stat -f%z "$f" 2>/dev/null)
91
	base=$(basename "$f" .nix)
92
	irfile="${testdir}/${base}.nixir"
93
94
	start=$(date +%s%N)
95
	"$IRC_BIN" "$f" "$irfile" >/dev/null 2>&1
96
	end=$(date +%s%N)
97
	compile_time=$(((end - start) / 1000000))
98
99
	if [ -f "$irfile" ]; then
100
		irsize=$(stat -c%s "$irfile" 2>/dev/null || stat -f%z "$irfile" 2>/dev/null)
101
		total_nix=$((total_nix + nixsize))
102
		total_ir=$((total_ir + irsize))
103
		total_compile_time=$((total_compile_time + compile_time))
104
	fi
105
done
106
107
total_ratio=$((total_ir * 100 / total_nix))
108
avg_compile_time=$((total_compile_time / 3))
109
110
# TBH those are entirely unnecessary. However, I'm a sucker for data
111
# and those are trivial to compile. Might as well. Who knows, maybe it'll
112
# come in handy in the future.
113
echo "  Total source size:     ${total_nix}B"
114
echo "  Total IR size:         ${total_ir}B"
115
echo "  Compression ratio:     ${total_ratio}% of source"
116
echo "  Average compile time:  ${avg_compile_time}ms"
117
echo ""
118
119
rm -rf "$testdir"