From aced9c7ece3127676e9cb25d2161ff08e5560149 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 30 Sep 2025 00:50:15 +0800 Subject: add hash benchmarks --- tests/benchmarks/hash.lua | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/benchmarks/hash.lua diff --git a/tests/benchmarks/hash.lua b/tests/benchmarks/hash.lua new file mode 100644 index 000000000..ae8a29d21 --- /dev/null +++ b/tests/benchmarks/hash.lua @@ -0,0 +1,82 @@ +local COUNT = 1000000 + +function test_md5(data) + local h + local t = os.mclock() + for i = 1, COUNT do + h = hash.md5(data) + end + t = os.mclock() - t + print("md5(%d): %d ms, hash: %s", COUNT, t, h) +end + +function test_sha1(data) + local h + local t = os.mclock() + for i = 1, COUNT do + h = hash.sha1(data) + end + t = os.mclock() - t + print("sha1(%d): %d ms, hash: %s", COUNT, t, h) +end + +function test_sha256(data) + local h + local t = os.mclock() + for i = 1, COUNT do + h = hash.sha256(data) + end + t = os.mclock() - t + print("sha256(%d): %d ms, hash: %s", COUNT, t, h) +end + +function test_uuid(data) + local h + local t = os.mclock() + for i = 1, COUNT do + h = hash.uuid(data) + end + t = os.mclock() - t + print("uuid(%d): %d ms, hash: %s", COUNT, t, h) +end + +function test_uuid4(data) + local h + local t = os.mclock() + for i = 1, COUNT do + h = hash.uuid4(data) + end + t = os.mclock() - t + print("uuid4(%d): %d ms, hash: %s", COUNT, t, h) +end + +function test_strhash32(data) + local h + local t = os.mclock() + for i = 1, COUNT do + h = hash.strhash32(data) + end + t = os.mclock() - t + print("strhash32(%d): %d ms, hash: %s", COUNT, t, h) +end + +function test_strhash128(data) + local h + local t = os.mclock() + for i = 1, COUNT do + h = hash.strhash128(data) + end + t = os.mclock() - t + print("strhash128(%d): %d ms, hash: %s", COUNT, t, h) +end + +function main() + local data = io.readfile(os.programfile()) + --test_md5(data) + --test_sha1(data) + --test_sha256(data) + test_uuid(data) + test_uuid4(data) + test_strhash32(data) + test_strhash128(data) +end -- cgit v1.3.1