394 lines
14 KiB
Luau
394 lines
14 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)
|
|
-- ------------------------------------------------------------------------------------------------ jpeg (ws-imgA)
|
|
do
|
|
local Jpeg = require("@formats/image/jpeg")
|
|
local Types = require("@formats/Types")
|
|
local JB = require("@tests/gen/jpeg_bench")
|
|
for _, c in JB.files do
|
|
local b = buffer.fromstring(c.data)
|
|
local img = Jpeg.decode(Bytes.reader(b))
|
|
assert(img and Crc32.of(img.pixels) == c.crc, "jpeg bench: " .. c.name .. " does not match djpeg")
|
|
bench("jpeg decode " .. c.name, 3, c.width * c.height * 4, function()
|
|
Jpeg.decode(Bytes.reader(b))
|
|
end)
|
|
end
|
|
local b0 = buffer.fromstring(JB.files[1].data)
|
|
bench("jpeg decode 1/8 (DC only)", 3, JB.width * JB.height * 4, function()
|
|
Jpeg.decode(Bytes.reader(b0), { scale = 8 })
|
|
end)
|
|
local src = Types.Image.new(JB.width, JB.height, "rgb8")
|
|
buffer.writestring(src.pixels, 0, JB.rgb)
|
|
for _, o in { { label = "baseline 4:2:0 q85", quality = 85 }, { label = "progressive 4:2:0 q85", quality = 85, progressive = true }, { label = "baseline 4:4:4 q85 optimized", quality = 85, subsampling = "444", optimize = true } } do
|
|
bench("jpeg encode " .. o.label, 2, JB.width * JB.height * 3, function()
|
|
Jpeg.encode(src, o :: any)
|
|
end)
|
|
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
|
|
|
|
-- ------------------------------------------------------------------------------------------------ ops/ImageOps
|
|
do
|
|
local Ops = require("@formats/ops/ImageOps")
|
|
local img = assert(Png.decode(Bytes.reader(buffer.fromstring(PB[1].data))))
|
|
local n = img.width * img.height * 4
|
|
bench("imageops resize 1/2 bilinear", 2, n, function()
|
|
Ops.resize(img, img.width // 2, img.height // 2, "bilinear")
|
|
end)
|
|
bench("imageops resize 1/2 lanczos", 2, n, function()
|
|
Ops.resize(img, img.width // 2, img.height // 2, "lanczos")
|
|
end)
|
|
bench("imageops resize x1.5 bilinear", 1, n, function()
|
|
Ops.resize(img, img.width * 3 // 2, img.height * 3 // 2, "bilinear")
|
|
end)
|
|
bench("imageops rotate 30 (expand)", 1, n, function()
|
|
Ops.rotate(img, 30)
|
|
end)
|
|
bench("imageops rotate90", 3, n, function()
|
|
Ops.rotate90(img, 1)
|
|
end)
|
|
bench("imageops convert rgba8 -> gray8", 3, n, function()
|
|
Ops.convert(img, "gray8")
|
|
end)
|
|
local other = assert(Ops.flip(img, "h"))
|
|
bench("imageops composite over", 3, n, function()
|
|
Ops.composite(img, other)
|
|
end)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ image/tiff (ws-imgA)
|
|
do
|
|
local Tiff = require("@formats/image/tiff")
|
|
local Types = require("@formats/Types")
|
|
local src = Types.Image.new(1024, 768)
|
|
for i = 0, 1024 * 768 - 1 do
|
|
local x, y = i % 1024, i // 1024
|
|
buffer.writeu32(src.pixels, i * 4, 0xFF000000 + (x % 256) + ((y % 256) * 256) + (((x + y) // 4 % 256) * 65536))
|
|
end
|
|
for _, comp in { "none", "lzw", "deflate" } do
|
|
local enc = Tiff.encode(src, { compression = comp :: any }) :: buffer
|
|
bench("tiff decode rgba8 " .. comp, 3, 1024 * 768 * 4, function()
|
|
Tiff.decode(Bytes.reader(enc))
|
|
end)
|
|
bench("tiff encode rgba8 " .. comp, 2, 1024 * 768 * 4, function()
|
|
Tiff.encode(src, { compression = comp :: any })
|
|
end)
|
|
end
|
|
local enc16 = Tiff.encode(Types.Image.new(1024, 768, "rgba16"), { compression = "lzw" }) :: buffer
|
|
bench("tiff decode rgba16 lzw -> rgba8", 3, 1024 * 768 * 8, function()
|
|
Tiff.decode(Bytes.reader(enc16))
|
|
end)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ imgA-simple: qoi / tga / pnm / hdr
|
|
-- fixtures: tools/fixtures/qoi.py (tests/gen/qoi_bench.luau: run python3 -I tools/fixtures.py qoi first)
|
|
do
|
|
local Qoi = require("@formats/image/qoi")
|
|
local Tga = require("@formats/image/tga")
|
|
local Pnm = require("@formats/image/pnm")
|
|
local Hdr = require("@formats/image/hdr")
|
|
local Types = require("@formats/Types")
|
|
local QB = require("@tests/gen/qoi_bench")
|
|
local qd = buffer.fromstring(QB.data)
|
|
local n = QB.width * QB.height * 4
|
|
bench("qoi decode 1024x768", 5, n, function()
|
|
Qoi.decode(Bytes.reader(qd))
|
|
end)
|
|
local img = Types.Image.new(QB.width, QB.height)
|
|
img.pixels = buffer.fromstring(QB.pixels)
|
|
bench("qoi encode 1024x768", 5, n, function()
|
|
Qoi.encode(img)
|
|
end)
|
|
local tr = Tga.encode(img, { rle = true }) :: buffer
|
|
bench("tga decode rle 1024x768", 3, n, function()
|
|
Tga.decode(Bytes.reader(tr))
|
|
end)
|
|
bench("tga encode rle 1024x768", 3, n, function()
|
|
Tga.encode(img, { rle = true })
|
|
end)
|
|
local pp = Pnm.encode(img, { format = "ppm" }) :: buffer
|
|
bench("ppm decode 1024x768", 3, n, function()
|
|
Pnm.decode(Bytes.reader(pp))
|
|
end)
|
|
local f = Hdr.decode(Bytes.reader(Hdr.encode(img) :: buffer), { native = true }) :: Types.Image
|
|
local hb = Hdr.encode(f) :: buffer
|
|
bench("hdr decode rle 1024x768 (rgbaf32)", 3, n, function()
|
|
Hdr.decode(Bytes.reader(hb), { native = true })
|
|
end)
|
|
bench("hdr encode rle 1024x768", 3, n, function()
|
|
Hdr.encode(f)
|
|
end)
|
|
end
|