102 lines
3.6 KiB
Luau
102 lines
3.6 KiB
Luau
-- Throughput (bash tools/bench.sh): codec MB/s under both runtimes. MB/s are of decompressed output, best of n runs.
|
|
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus) and tools/fixtures/png.py (1024 x 768 images).
|
|
local Deflate = require("@formats/codecs/Deflate")
|
|
local Crc32 = require("@formats/core/Crc32")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Png = require("@formats/image/png")
|
|
local B = require("@tests/gen/deflate_bench")
|
|
local PB = require("@tests/gen/png_bench")
|
|
|
|
local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
|
|
|
|
local function bench(label: string, n: number, bytes: number, f: () -> ())
|
|
f()
|
|
local dt = math.huge -- best of n: the machine is shared, the mean is noisy
|
|
for _ = 1, n do
|
|
local t0 = os.clock()
|
|
f()
|
|
dt = math.min(dt, os.clock() - t0)
|
|
end
|
|
print(string.format("%-8s %-30s %8.2f ms %7.1f MB/s", runtime, label, dt * 1000, bytes / dt / 1e6))
|
|
end
|
|
|
|
local z = buffer.fromstring(B.zlib)
|
|
local g = buffer.fromstring(B.gzip)
|
|
local out = assert(Deflate.zlib(z))
|
|
assert(buffer.len(out) == B.size and Crc32.of(out) == B.crc, "bench corpus mismatch")
|
|
bench("inflate (growing output)", 5, B.size, function()
|
|
Deflate.inflate(z, 2, buffer.len(z) - 6)
|
|
end)
|
|
bench("inflate (exact size)", 5, B.size, function()
|
|
Deflate.inflate(z, 2, buffer.len(z) - 6, B.size)
|
|
end)
|
|
bench("zlib (+ adler32)", 5, B.size, function()
|
|
Deflate.zlib(z)
|
|
end)
|
|
bench("gunzip (+ crc32)", 5, B.size, function()
|
|
Deflate.gunzip(g)
|
|
end)
|
|
bench("crc32", 10, B.size, function()
|
|
Crc32.of(out)
|
|
end)
|
|
local part = buffer.create(1500000)
|
|
buffer.copy(part, 0, out, 0, 1500000)
|
|
bench("compress -6 (1.5 MB in)", 2, 1500000, function()
|
|
Deflate.compress(part, { level = 6 })
|
|
end)
|
|
for _, c in PB do
|
|
local b = buffer.fromstring(c.data)
|
|
bench("png decode " .. c.name, 3, c.width * c.height * 4, function()
|
|
Png.decode(Bytes.reader(b))
|
|
end)
|
|
end
|
|
do
|
|
local img = assert(Png.decode(Bytes.reader(buffer.fromstring(PB[1].data))))
|
|
bench("png encode rgba8", 2, img.width * img.height * 4, function()
|
|
Png.encode(img)
|
|
end)
|
|
end
|
|
|
|
-- ---------------------------------------------------------------------------------------- ws-imgA: gif + quantize
|
|
do
|
|
local Gif = require("@formats/image/gif")
|
|
local Quantize = require("@formats/codecs/Quantize")
|
|
local Types = require("@formats/Types")
|
|
local GB = require("@tests/gen/gif_bench")
|
|
local still = buffer.fromstring(GB.still)
|
|
local anim = buffer.fromstring(GB.anim)
|
|
local simg = assert(Gif.decode(Bytes.reader(still)))
|
|
local aimg = assert(Gif.decode(Bytes.reader(anim)))
|
|
local sw, sh = simg.width, simg.height
|
|
bench("gif decode 512x384", 5, sw * sh * 4, function()
|
|
Gif.decode(Bytes.reader(still))
|
|
end)
|
|
bench("gif decode 40 x 128x128 anim", 3, #(aimg.frames :: any) * 128 * 128 * 4, function()
|
|
Gif.decode(Bytes.reader(anim))
|
|
end)
|
|
bench("gif encode 512x384 (exact pal)", 3, sw * sh * 4, function()
|
|
Gif.encode(simg)
|
|
end)
|
|
local photo = Types.Image.new(sw, sh)
|
|
for i = 0, sw * sh - 1 do
|
|
local x, y = i % sw, i // sw
|
|
buffer.writeu32(photo.pixels, i * 4, (x % 256) + (y % 256) * 256 + ((x * y) % 256) * 65536 + 0xFF000000)
|
|
end
|
|
bench("quantize mediancut 256", 3, sw * sh * 4, function()
|
|
Quantize.palette(photo.pixels, sw, sh, 256)
|
|
end)
|
|
bench("quantize octree 256", 3, sw * sh * 4, function()
|
|
Quantize.palette(photo.pixels, sw, sh, 256, { method = "octree" })
|
|
end)
|
|
local pal = Quantize.palette(photo.pixels, sw, sh, 256)
|
|
bench("quantize map", 3, sw * sh * 4, function()
|
|
Quantize.map(photo.pixels, sw, sh, pal)
|
|
end)
|
|
bench("quantize map + dither", 3, sw * sh * 4, function()
|
|
Quantize.map(photo.pixels, sw, sh, pal, { dither = true })
|
|
end)
|
|
bench("gif encode 512x384 (quantize)", 2, sw * sh * 4, function()
|
|
Gif.encode(photo)
|
|
end)
|
|
end
|