Files
rosetta/tools/bench.luau
T

235 lines
7.9 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).
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus), tools/fixtures/png.py (1024 x 768 images), tools/fixtures/mp3.py,
-- tools/fixtures/opus.py (3 s stereo files; MB/s of f32 output, 48 kHz stereo f32 is 0.384 MB/s of real time),
-- tools/fixtures/flac.py; wav encodes / decodes a generated tone.
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 Mp3 = require("@formats/audio/mp3")
local MC = require("@tests/gen/mp3_cases")
local Opus = require("@formats/audio/opus")
local OpusCelt = require("@tests/gen/opus_celt_st_128")
local OpusSilk = require("@tests/gen/opus_silk_wb_st")
local OpusHybrid = require("@tests/gen/opus_hybrid_st")
local Flac = require("@formats/audio/flac")
local FC = require("@tests/gen/flac_cases")
local Wav = require("@formats/audio/wav")
local Types = require("@formats/Types")
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))
return dt
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
for _, name in { "l3_cbr128_js", "l3_vbr_v2_48k", "l2_192_st" } do
for _, c in MC do
if c.name == name then
local b = buffer.fromstring(c.data)
local dec = assert(Mp3.open(Bytes.reader(b)))
local secs = dec.duration
local dt = bench("mp3 decode " .. name, 5, dec.frames * dec.channels * 4, function()
local d = assert(Mp3.open(Bytes.reader(b)))
d:readAll()
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", secs / dt))
end
end
end
for _, c in { { "celt 128k", OpusCelt }, { "silk wb", OpusSilk }, { "hybrid", OpusHybrid } } do
local b = buffer.fromstring(c[2].data)
bench("opus decode " .. c[1] .. " (3 s st)", 3, #c[2].ref, function()
Opus.decode(Bytes.reader(b))
end)
end
for _, name in { "s16_long", "s24_96k" } do
for _, c in FC do
if c.name == name then
local b = buffer.fromstring(c.data)
local dec = assert(Flac.open(Bytes.reader(b)))
local secs = dec.duration
local dt = bench("flac decode " .. name, 5, dec.frames * dec.channels * 4, function()
Flac.decode(Bytes.reader(b))
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", secs / dt))
end
end
end
do
local a = Types.Audio.new(44100, 2, 44100 * 10)
for i = 0, 44100 * 20 - 1 do
buffer.writef32(a.samples, i * 4, math.sin(i * 0.01) * 0.5)
end
bench("wav encode s16 (10 s stereo)", 5, buffer.len(a.samples), function()
Wav.encode(a)
end)
local file = assert(Wav.encode(a))
bench("wav decode s16 (10 s stereo)", 5, buffer.len(a.samples), function()
Wav.decode(Bytes.reader(file))
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