-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.lua
90 lines (84 loc) · 2.54 KB
/
bench.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
local inflate = require('inflate')
local zzlib = require('zzlib')
local miniz = require('miniz')
local http = require('https')
local fs = require("fs")
local uv = require("uv")
if not fs.existsSync("sample.zip") then
local running = coroutine.running()
http.get("https://files-example-com.github.io/uploads/zip_10MB.zip", function(response)
if response.statusCode ~= 200 then
return error("Failed to retrieve sample ZIP archive.")
end
local buffer = {}
response:on("data", function(chunk)
buffer[#buffer + 1] = chunk
end)
response:on("end", function()
fs.writeFileSync("sample.zip", table.concat(buffer))
coroutine.resume(running)
end)
end):done()
coroutine.yield()
end
local function benchmark(fn, n)
local start = (uv.hrtime() * 1e-06) / 1000
for _ = 1, n do
fn()
end
local finish = (uv.hrtime() * 1e-06) / 1000
local result = finish - start
return result, result / n
end
local base = 10
local sampleData = assert(fs.readFileSync("./sample.zip"))
-- Reusing reader object to avoid reading a file multiple times
local reader = assert(miniz.new_reader("./sample.zip"))
local function inflateTest()
local stream = inflate.new(sampleData)
for _, offset, size, packed in stream:files() do
if packed then
stream:inflate(offset)
else
stream:extract(offset, size)
end
end
end
local function inflateCrcTest()
local stream = inflate.new(sampleData)
for name, offset, size, packed, crc in stream:files() do
if packed then
stream:inflate(offset, crc)
else
stream:extract(offset, size)
end
end
end
local function zzlibTest()
for name, name, offset, size, packed in zzlib.files(sampleData) do
if packed then
zzlib.unzip(sampleData, offset)
else
sampleData:sub(offset, offset + size - 1)
end
end
end
local function zzlibCrcTest()
for _, _, offset, size, packed, crc in zzlib.files(sampleData) do
if packed then
zzlib.unzip(sampleData, offset, crc)
else
sampleData:sub(offset, offset + size - 1)
end
end
end
local function minizTest()
for i = 1, reader:get_num_files() do
reader:extract(i)
end
end
p("inflate", benchmark(inflateTest, base))
p("inflateCrc", benchmark(inflateCrcTest, base))
p("zzlib", benchmark(zzlibTest, base))
p("zzlibCrc", benchmark(zzlibCrcTest, base))
p("miniz", benchmark(miniz, base))