Skip to content

Commit c55eed2

Browse files
committed
tests/benchmark: Add RNG benchmarks
1 parent f4d89d3 commit c55eed2

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

tests/benchmark/all.odin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package benchmarks
33
@(require) import "bytes"
44
@(require) import "crypto"
55
@(require) import "hash"
6+
@(require) import "math"
67
@(require) import "text/regex"
78
@(require) import "strings"
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package benchmark_core_math
2+
3+
import "base:runtime"
4+
5+
import "core:fmt"
6+
import "core:math/rand"
7+
import "core:log"
8+
import "core:strings"
9+
import "core:testing"
10+
import "core:text/table"
11+
import "core:time"
12+
13+
@(private = "file")
14+
ITERS :: 10000000
15+
@(private = "file")
16+
ITERS_BULK :: 1000
17+
18+
@(private = "file")
19+
SAMPLE_SEED : string : "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"
20+
21+
@(test)
22+
benchmark_rng :: proc(t: ^testing.T) {
23+
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
24+
25+
tbl: table.Table
26+
table.init(&tbl)
27+
defer table.destroy(&tbl)
28+
29+
table.caption(&tbl, "RNG")
30+
table.aligned_header_of_values(&tbl, .Right, "Algorithm", "Size", "Time", "Throughput")
31+
32+
context.random_generator = rand.default_random_generator()
33+
rand.reset_bytes(transmute([]byte)(SAMPLE_SEED))
34+
_benchmark_u64(t, &tbl, "chacha8rand")
35+
_benchmark_large(t, &tbl, "chacha8rand")
36+
37+
table.row(&tbl)
38+
39+
context.random_generator = rand.pcg_random_generator()
40+
_benchmark_u64(t, &tbl, "pcg64")
41+
_benchmark_large(t, &tbl, "pcg64")
42+
43+
table.row(&tbl)
44+
45+
context.random_generator = rand.xoshiro256_random_generator()
46+
_benchmark_u64(t, &tbl, "xorshiro256**")
47+
_benchmark_large(t, &tbl, "xorshiro256**")
48+
49+
log_table(&tbl)
50+
}
51+
52+
@(private = "file")
53+
_benchmark_u64 :: proc(t: ^testing.T, tbl: ^table.Table, algo_name: string) {
54+
options := &time.Benchmark_Options{
55+
rounds = ITERS,
56+
bytes = 8,
57+
setup = nil,
58+
bench = proc(options: ^time.Benchmark_Options, allocator: runtime.Allocator) -> (err: time.Benchmark_Error){
59+
sum: u64
60+
for _ in 0 ..= options.rounds {
61+
sum += rand.uint64()
62+
}
63+
options.hash = u128(sum)
64+
options.count = options.rounds
65+
options.processed = options.rounds * options.bytes
66+
return
67+
},
68+
teardown = nil,
69+
}
70+
71+
err := time.benchmark(options, context.allocator)
72+
testing.expect(t, err == nil)
73+
74+
time_per_iter := options.duration / ITERS
75+
table.aligned_row_of_values(
76+
tbl,
77+
.Right,
78+
algo_name,
79+
table.format(tbl, "uint64"),
80+
table.format(tbl, "%8M", time_per_iter),
81+
table.format(tbl, "%5.3f MiB/s", options.megabytes_per_second),
82+
)
83+
}
84+
85+
@(private = "file")
86+
_benchmark_large :: proc(t: ^testing.T, tbl: ^table.Table, algo_name: string) {
87+
options := &time.Benchmark_Options{
88+
rounds = ITERS_BULK,
89+
bytes = 1024768,
90+
setup = nil,
91+
bench = proc(options: ^time.Benchmark_Options, allocator: runtime.Allocator) -> (err: time.Benchmark_Error){
92+
n: int
93+
for _ in 0 ..= options.rounds {
94+
n += rand.read(options.output)
95+
}
96+
options.hash = u128(n)
97+
options.count = options.rounds
98+
options.processed = options.rounds * options.bytes
99+
return
100+
},
101+
output = make([]byte, 1024768, context.temp_allocator),
102+
teardown = nil,
103+
}
104+
105+
err := time.benchmark(options, context.allocator)
106+
testing.expect(t, err == nil)
107+
108+
time_per_iter := options.duration / ITERS_BULK
109+
table.aligned_row_of_values(
110+
tbl,
111+
.Right,
112+
algo_name,
113+
table.format(tbl, "1 MiB"),
114+
table.format(tbl, "%8M", time_per_iter),
115+
table.format(tbl, "%5.3f MiB/s", options.megabytes_per_second),
116+
)
117+
}
118+
119+
@(private)
120+
log_table :: proc(tbl: ^table.Table) {
121+
sb := strings.builder_make()
122+
defer strings.builder_destroy(&sb)
123+
124+
wr := strings.to_writer(&sb)
125+
126+
fmt.sbprintln(&sb)
127+
table.write_plain_table(wr, tbl)
128+
129+
log.info(strings.to_string(sb))
130+
}

0 commit comments

Comments
 (0)