summaryrefslogtreecommitdiff
path: root/tests/benchmarks/hash.lua
blob: 0f33684a0e0ae89c6dde61c20a8d3f2ed5a34814 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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, n do
        h = hash.md5(data)
    end
    t = os.mclock() - t
    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, n do
        h = hash.sha1(data)
    end
    t = os.mclock() - t
    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, n do
        h = hash.sha256(data)
    end
    t = os.mclock() - t
    print("sha256(%d): %d ms, hash: %s", COUNT, t * 10000, h)
end

function test_uuid(data)
    local h
    local n = COUNT / 10000
    local t = os.mclock()
    for i = 1, n do
        h = hash.uuid(data)
    end
    t = os.mclock() - t
    print("uuid(%d): %d ms, hash: %s", COUNT, t * 10000, h)
end

function test_xxhash32(data)
    data = bytes(data)
    local h
    local n = COUNT / 10
    local t = os.mclock()
    for i = 1, n do
        h = hash.xxhash32(data)
    end
    t = os.mclock() - t
    print("xxhash32(%d): %d ms, hash: %s", COUNT, t * 10, 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 n = COUNT / 10
    local t = os.mclock()
    for i = 1, n do
        h = hash.strhash32(data)
    end
    t = os.mclock() - t
    print("strhash32(%d): %d ms, hash: %s", COUNT, t * 10, h)
end

function test_strhash64(data)
    local h
    local n = COUNT / 10
    local t = os.mclock()
    for i = 1, n do
        h = hash.strhash64(data)
    end
    t = os.mclock() - t
    print("strhash64(%d): %d ms, hash: %s", COUNT, t * 10, h)
end

function test_strhash128(data)
    local h
    local n = COUNT / 10
    local t = os.mclock()
    for i = 1, n do
        h = hash.strhash128(data)
    end
    t = os.mclock() - t
    print("strhash128(%d): %d ms, hash: %s", COUNT, t * 10, h)
end

function test_random_uuid()
    local h
    local n = COUNT / 1000
    local t = os.mclock()
    for i = 1, n do
        h = hash.uuid()
    end
    t = os.mclock() - t
    print("uuid(%d): %d ms, hash: %s", COUNT, t * 1000, h)
end

function test_rand32()
    local h
    local n = COUNT / 10
    local t = os.mclock()
    for i = 1, n do
        h = hash.rand32()
    end
    t = os.mclock() - t
    print("rand32(%d): %d ms, hash: %s", COUNT, t * 10, h)
end

function test_rand64()
    local h
    local n = COUNT / 10
    local t = os.mclock()
    for i = 1, n do
        h = hash.rand64()
    end
    t = os.mclock() - t
    print("rand64(%d): %d ms, hash: %s", COUNT, t * 10, h)
end

function test_rand128()
    local h
    local n = COUNT / 10
    local t = os.mclock()
    for i = 1, n do
        h = hash.rand128()
    end
    t = os.mclock() - t
    print("rand128(%d): %d ms, hash: %s", COUNT, t * 10, h)
end

function test_longstr()
    print("========================================== test long string ==========================================")
    local data = ""
    for i = 1, 10000 do
        data = data .. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    end
    test_md5(data)
    test_sha1(data)
    test_sha256(data)
    test_uuid(data)
    test_xxhash32(data)
    test_xxhash64(data)
    test_xxhash128(data)
    test_strhash32(data)
    test_strhash64(data)
    test_strhash128(data)
end

function test_shortstr()
    print("========================================== test short string ==========================================")
    COUNT = COUNT * 100
    local data = ""
    for i = 1, 10 do
        data = data .. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    end
    test_md5(data)
    test_sha1(data)
    test_sha256(data)
    test_uuid(data)
    test_xxhash32(data)
    test_xxhash64(data)
    test_xxhash128(data)
    test_strhash32(data)
    test_strhash64(data)
    test_strhash128(data)
end

function test_random()
    print("========================================== test random ==========================================")
    test_random_uuid()
    test_rand32()
    test_rand64()
    test_rand128()
end

function main()
    test_longstr()
    test_shortstr()
    test_random()
end