aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmark/crypto/benchmark_aead.odin
blob: bfd888a4354d13793d9ad1d20c18492a4af3c1e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package benchmark_core_crypto

import "base:runtime"
import "core:crypto"
import "core:testing"
import "core:text/table"
import "core:time"

import "core:crypto/aead"

@(private = "file")
ITERS :: 10000
@(private = "file")
SIZES := []int{64, 1024, 65536}

@(test)
benchmark_crypto_aead :: proc(t: ^testing.T) {
	runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()

	tbl: table.Table
	table.init(&tbl)
	defer table.destroy(&tbl)

	table.caption(&tbl, "AEAD")
	table.aligned_header_of_values(&tbl, .Right, "Algorithm", "Size", "Time", "Throughput")

	for algo, i in aead.Algorithm {
		if algo == .Invalid {
			continue
		}
		if i > 1 {
			table.row(&tbl)
		}

		algo_name := aead.ALGORITHM_NAMES[algo]
		key_sz := aead.KEY_SIZES[algo]

		key := make([]byte, key_sz, context.temp_allocator)
		crypto.rand_bytes(key)

		// TODO: Benchmark all available imlementations?
		ctx: aead.Context
		aead.init(&ctx, algo, key)

		for sz, _ in SIZES {
			options := &time.Benchmark_Options{
				rounds = ITERS,
				bytes = aead.IV_SIZES[algo] + sz,
				setup = setup_sized_buf,
				bench = do_bench_aead,
				teardown = teardown_sized_buf,
			}
			context.user_ptr = &ctx

			err := time.benchmark(options, context.allocator)
			testing.expect(t, err == nil)

			time_per_iter := options.duration / ITERS
			table.aligned_row_of_values(
				&tbl,
				.Right,
				algo_name,
				table.format(&tbl, "%d", sz),
				table.format(&tbl, "%8M", time_per_iter),
				table.format(&tbl, "%5.3f MiB/s", options.megabytes_per_second),
			)
		}
	}

	log_table(&tbl)
}

@(private = "file")
do_bench_aead :: proc(
	options: ^time.Benchmark_Options,
	allocator := context.allocator,
) -> (
	err: time.Benchmark_Error,
) {
	tag_: [aead.MAX_TAG_SIZE]byte

	ctx := (^aead.Context)(context.user_ptr)
	iv_sz := aead.iv_size(ctx)

	iv := options.input[:iv_sz]
	buf := options.input[iv_sz:]
	tag := tag_[:aead.tag_size(ctx)]

	for _ in 0 ..= options.rounds {
		aead.seal_ctx(ctx, buf, tag, iv, nil, buf)
	}
	options.count = options.rounds
	options.processed = options.rounds * (options.bytes - iv_sz)

	return
}