summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-09-30 00:53:37 +0800
committerruki <[email protected]>2025-09-30 00:53:37 +0800
commitd673b3732b66ddae22a5c2b3c613af5447d23dca (patch)
tree0a1e873623516ca5eb5c4cec4ed762eb193c7b6a
parentaced9c7ece3127676e9cb25d2161ff08e5560149 (diff)
improve hash tests
-rw-r--r--core/src/xmake/hash/md5.c5
-rw-r--r--tests/benchmarks/hash.lua52
-rw-r--r--xmake/core/sandbox/modules/hash.lua10
3 files changed, 50 insertions, 17 deletions
diff --git a/core/src/xmake/hash/md5.c b/core/src/xmake/hash/md5.c
index 1ca594941..0f6a9f598 100644
--- a/core/src/xmake/hash/md5.c
+++ b/core/src/xmake/hash/md5.c
@@ -108,9 +108,8 @@ tb_int_t xm_hash_md5(lua_State* lua)
tb_md5_exit(&md5, buffer, sizeof(buffer));
// make md5 string
- tb_size_t i = 0;
- tb_char_t s[256] = {0};
- for (i = 0; i < 16; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
+ tb_char_t s[256];
+ xm_hash_make_cstr(s, buffer, 16);
// save result
lua_pushstring(lua, s);
diff --git a/tests/benchmarks/hash.lua b/tests/benchmarks/hash.lua
index ae8a29d21..119f1bb71 100644
--- a/tests/benchmarks/hash.lua
+++ b/tests/benchmarks/hash.lua
@@ -1,33 +1,41 @@
+import("core.base.bytes")
+
local COUNT = 1000000
function test_md5(data)
+ data = bytes(data)
local h
+ local n = COUNT / 10000
local t = os.mclock()
- for i = 1, COUNT do
+ for i = 1, n do
h = hash.md5(data)
end
t = os.mclock() - t
- print("md5(%d): %d ms, hash: %s", COUNT, t, h)
+ print("md5(%d): %d ms, hash: %s", COUNT, t * 10000, h)
end
function test_sha1(data)
+ data = bytes(data)
local h
+ local n = COUNT / 10000
local t = os.mclock()
- for i = 1, COUNT do
+ for i = 1, n do
h = hash.sha1(data)
end
t = os.mclock() - t
- print("sha1(%d): %d ms, hash: %s", COUNT, t, h)
+ print("sha1(%d): %d ms, hash: %s", COUNT, t * 10000, h)
end
function test_sha256(data)
+ data = bytes(data)
local h
+ local n = COUNT / 10000
local t = os.mclock()
- for i = 1, COUNT do
+ for i = 1, n do
h = hash.sha256(data)
end
t = os.mclock() - t
- print("sha256(%d): %d ms, hash: %s", COUNT, t, h)
+ print("sha256(%d): %d ms, hash: %s", COUNT, t * 10000, h)
end
function test_uuid(data)
@@ -50,6 +58,30 @@ function test_uuid4(data)
print("uuid4(%d): %d ms, hash: %s", COUNT, t, h)
end
+function test_xxhash64(data)
+ data = bytes(data)
+ local h
+ local n = COUNT / 10
+ local t = os.mclock()
+ for i = 1, n do
+ h = hash.xxhash64(data)
+ end
+ t = os.mclock() - t
+ print("xxhash64(%d): %d ms, hash: %s", COUNT, t * 10, h)
+end
+
+function test_xxhash128(data)
+ data = bytes(data)
+ local h
+ local n = COUNT / 10
+ local t = os.mclock()
+ for i = 1, n do
+ h = hash.xxhash128(data)
+ end
+ t = os.mclock() - t
+ print("xxhash128(%d): %d ms, hash: %s", COUNT, t * 10, h)
+end
+
function test_strhash32(data)
local h
local t = os.mclock()
@@ -72,11 +104,13 @@ end
function main()
local data = io.readfile(os.programfile())
- --test_md5(data)
- --test_sha1(data)
- --test_sha256(data)
+ test_md5(data)
+ test_sha1(data)
+ test_sha256(data)
test_uuid(data)
test_uuid4(data)
+ test_xxhash64(data)
+ test_xxhash128(data)
test_strhash32(data)
test_strhash128(data)
end
diff --git a/xmake/core/sandbox/modules/hash.lua b/xmake/core/sandbox/modules/hash.lua
index 47235bcd6..11a85f3fb 100644
--- a/xmake/core/sandbox/modules/hash.lua
+++ b/xmake/core/sandbox/modules/hash.lua
@@ -47,7 +47,7 @@ end
function sandbox_hash.sha1(file_or_data)
local sha1, errors = hash.sha1(file_or_data)
if not sha1 then
- raise("cannot generate sha1 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate sha1, %s", errors or "unknown errors")
end
return sha1
end
@@ -56,7 +56,7 @@ end
function sandbox_hash.sha256(file_or_data)
local sha256, errors = hash.sha256(file_or_data)
if not sha256 then
- raise("cannot generate sha256 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate sha256, %s", errors or "unknown errors")
end
return sha256
end
@@ -65,7 +65,7 @@ end
function sandbox_hash.md5(file_or_data)
local md5, errors = hash.md5(file_or_data)
if not md5 then
- raise("cannot generate md5 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate md5, %s", errors or "unknown errors")
end
return md5
end
@@ -74,7 +74,7 @@ end
function sandbox_hash.xxhash64(file_or_data)
local xxhash64, errors = hash.xxhash64(file_or_data)
if not xxhash64 then
- raise("cannot generate xxhash64 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate xxhash64, %s", errors or "unknown errors")
end
return xxhash64
end
@@ -83,7 +83,7 @@ end
function sandbox_hash.xxhash128(file_or_data)
local xxhash128, errors = hash.xxhash128(file_or_data)
if not xxhash128 then
- raise("cannot generate xxhash128 for %s, %s", file_or_data, errors or "unknown errors")
+ raise("cannot generate xxhash128, %s", errors or "unknown errors")
end
return xxhash128
end