summaryrefslogtreecommitdiff
path: root/tests/benchmarks/hash.lua
blob: 119f1bb71842e5876e62ca867fbd5f71dd494461 (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
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 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_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()
    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_xxhash64(data)
    test_xxhash128(data)
    test_strhash32(data)
    test_strhash128(data)
end