-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.lua
66 lines (57 loc) · 1.42 KB
/
test.lua
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
local xxh32 = require("luaxxhash")
local f = io.open("sherlock.txt")
local text = f:read("*all")
local str = "Cats are interesting."
local str1 = "a"
local strt = {
str1:rep(4),
str1:rep(16),
str1:rep(32),
str1:rep(32) .. str1 .. str1:rep(4),
str:rep(256),
"",
"dupa",
str,
str1,
text
}
local answer = {
0xDA576727,
0x5DACDD8C,
0xA3234FBE,
0x3F87545B,
0xF1DB4C68,
0x02CC5D05,
0x1A47C09D,
0x65ED1AFC,
0x550D7456,
0xFAB5AEF6,
}
assert(xxh32('abc', 3, 0x5BD1E995) == 0xBDDEB201, "Hash produces incorrect value with seed")
for i=1, #strt do
local x = xxh32(strt[i])
assert(x == answer[i], "Hash produces incorrect values: " .. i .. " Hash: " .. x)
end
local testIndex = 10
local testRepeat = 10
local hashRepeat = 1000
function test(f)
local times = {}
local throughputs = {}
local len = #strt[testIndex]
for i=1,testRepeat do
local start = os.clock()
for i=1,hashRepeat do f(strt[testIndex], len) end
local time = os.clock() - start
local throughput = len*hashRepeat/time/(1024*1024*1024)
table.insert(times, time)
table.insert(throughputs, throughput)
end
print( ("min: %.3fs %.2f GB/s"):format( math.min(table.unpack(times)), math.min(table.unpack(throughputs))) )
print( ("max: %.3fs %.2f GB/s"):format( math.max(table.unpack(times)), math.max(table.unpack(throughputs))) )
end
if not arg[1] then
test(xxh32)
local ok, murmur = pcall(require, 'murmurhash3')
if ok then test(murmur) end
end