Created
September 6, 2022 04:42
-
-
Save bjorng/522470f313ac01d16156a597657f97bb to your computer and use it in GitHub Desktop.
Benchmarking for encoding and decoding Base64 binaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(bench). % -*- erlang -*-; | |
-mode(compile). | |
%% Based on the benchmark in https://github.com/erlang/otp/issues/5639. | |
main([]) -> | |
Data100B = crypto:strong_rand_bytes(100), | |
Data1MB = crypto:strong_rand_bytes(1024 * 1024), | |
io:format("== Testing with 100 B ==~n"), | |
test_encode(Data100B, 1_000_000), | |
test_decode(Data100B, 1_000_000), | |
io:format("~n== Testing with 1 MB ==~n"), | |
test_encode(Data1MB, 1000), | |
test_decode(Data1MB, 1000). | |
test_encode(Data, Iterations) -> | |
Encoded = base64:encode(Data), | |
[test(Fun, Data, Encoded, Iterations) | |
|| Fun <- [fun base64:encode/1]]. | |
test_decode(Data, Iterations) -> | |
Encoded = base64:encode(Data), | |
[test(Fun, Encoded, Data, Iterations) | |
|| Fun <- [fun base64:decode/1]]. | |
test(Fun, Input, Expected, Iterations) -> | |
%% Check that the fun works | |
Expected = Fun(Input), | |
{Time, ok} = timer:tc(fun() -> run_test(Fun, Input, Iterations) end), | |
Millis = Time div 1000, | |
io:format("~p: ~B iterations in ~B ms: ~B it/sec~n", | |
[Fun, Iterations, Millis, Iterations * 1000 div Millis]). | |
run_test(_Fun, _Input, 0) -> | |
ok; | |
run_test(Fun, Input, Iterations) -> | |
Fun(Input), | |
run_test(Fun, Input, Iterations - 1). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment