Files
rosetta/tools/bench.luau
T

175 lines
5.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), tools/fixtures/png.py (1024 x 768 images), zstd / brotli /
-- bzip2 / lzma (their *_bench corpora) and fonts (glyph outlines + rasterization: glyphs per second).
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-arch codecs: zstd, brotli, bzip2, xz / lzma
do
local Zstd = require("@formats/codecs/Zstd")
local ZB = require("@tests/gen/zstd_bench")
local z3, z19 = buffer.fromstring(ZB.data3), buffer.fromstring(ZB.data19)
local zo = assert(Zstd.decompress(z3))
assert(buffer.len(zo) == ZB.size and Crc32.of(zo) == ZB.crc, "zstd bench corpus mismatch")
bench("zstd -3 (+ xxh64)", 3, ZB.size, function()
Zstd.decompress(z3)
end)
bench("zstd -3 (no checksum)", 3, ZB.size, function()
Zstd.decompress(z3, nil, nil, { verify = false })
end)
bench("zstd -19 (no checksum)", 3, ZB.size, function()
Zstd.decompress(z19, nil, nil, { verify = false })
end)
end
do
local Brotli = require("@formats/codecs/Brotli")
local BB = require("@tests/gen/brotli_bench")
local bz = buffer.fromstring(BB.data)
local bout = assert(Brotli.decompress(bz))
assert(buffer.len(bout) == BB.size and Crc32.of(bout) == BB.crc, "brotli bench corpus mismatch")
bench("brotli decompress (q9 w22)", 5, BB.size, function()
Brotli.decompress(bz)
end)
end
do
local Bzip2 = require("@formats/codecs/Bzip2")
local BZ = require("@tests/gen/bzip2_bench")
local bz = buffer.fromstring(BZ.data)
bench("bzip2 decompress -9", 3, BZ.size, function()
Bzip2.decompress(bz)
end)
end
do
local Lzma = require("@formats/codecs/Lzma")
local LB = require("@tests/gen/lzma_bench")
local lx, la = buffer.fromstring(LB.xz), buffer.fromstring(LB.alone)
local lo = assert(Lzma.xz(lx))
assert(buffer.len(lo) == LB.size and Crc32.of(lo) == LB.crc, "lzma bench corpus mismatch")
bench("xz -6 (lzma2 + crc64)", 3, LB.size, function()
Lzma.xz(lx)
end)
bench("lzma alone -6", 3, LB.size, function()
Lzma.alone(la)
end)
end
-- fonts: outlines and rasterization (MB/s here = thousands of glyphs per second)
do
local Ttf = require("@formats/font/ttf")
local FX = require("@tests/gen/fonts")
for _, c in FX.fonts do
if c.name == "Roboto-Medium" or c.name == "SourceSans3-Regular" then
local f = assert(Ttf.decode(Bytes.reader(buffer.fromstring(c.data))))
local gl = {}
for cp = 33, 126 do
table.insert(gl, f:glyphIndex(cp))
end
bench(c.name .. " outlines (kglyph/s)", 3, #gl * 1000, function()
for _, g in gl do
f:outline(g)
end
end)
bench(c.name .. " rasterize 32 px (kglyph/s)", 3, #gl * 1000, function()
for _, g in gl do
f:rasterize(g, 32)
end
end)
end
end
do
local Json = require("@formats/codecs/Json")
local JB = require("@tests/gen/json_bench")
local v = assert(Json.decode(JB.text))
assert(Json.encode(v) == JB.compact, "json bench mismatch")
bench("json decode (indented)", 5, #JB.text, function()
Json.decode(JB.text)
end)
bench("json decode (compact)", 5, #JB.compact, function()
Json.decode(JB.compact)
end)
bench("json encode (compact, sorted)", 5, #JB.compact, function()
Json.encode(v)
end)
end
do
local Xml = require("@formats/codecs/Xml")
local XB = require("@tests/gen/xml_bench")
local doc = assert(Xml.decode(XB.text))
bench("xml decode (DOM)", 5, #XB.text, function()
Xml.decode(XB.text)
end)
bench("xml events", 5, #XB.text, function()
for _ in Xml.events(XB.text) do
end
end)
bench("xml encode", 5, #XB.text, function()
Xml.encode(doc)
end)
end
do
local Yaml = require("@formats/data/yaml")
local YB = require("@tests/gen/yaml_bench")
local yb = buffer.fromstring(YB.text)
local v = assert(Yaml.decode(Bytes.reader(yb)))
bench("yaml decode (block)", 3, #YB.text, function()
Yaml.decode(Bytes.reader(yb))
end)
bench("yaml encode", 3, #YB.text, function()
Yaml.encode(v)
end)
end