Files
rosetta/tools/bench.luau
T

1109 lines
40 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), 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; zstd / brotli / bzip2 / lzma (*_bench corpora), fonts
-- (glyphs per second), json / xml / yaml, jpeg / gif / tiff / qoi / tga / pnm / hdr and ImageOps.
local Deflate = require("@formats/codecs/compress/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"
-- jitTune(on): LuaJIT's trace limits as host/init.luau sets them for the api (on) or LuaJIT's defaults (off); the
-- codec sections that care print both
local function jitTune(on: boolean)
local jit = (_G :: any).jit
if jit and type(jit.opt) == "table" then
if on then
jit.opt.start("maxtrace=8000", "maxmcode=65536")
else
jit.opt.start("maxtrace=1000", "maxmcode=8192")
end
end
end
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
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
-- vorbis (ws-audio): 10 s of 44.1 kHz stereo music at oggenc -q 5 (tools/fixtures/vorbis.py)
do
local Vorbis = require("@formats/audio/vorbis")
local VB = require("@tests/gen/vorbis_bench")
local b = buffer.fromstring(VB.data)
local dt = bench("vorbis decode q5 (10 s st)", 3, VB.frames * VB.channels * 4, function()
Vorbis.decode(Bytes.reader(b))
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", VB.frames / VB.rate / dt))
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
-- flac encode (ws-audio): 10 s of 44.1 kHz stereo 16-bit (a synthetic two-tone signal with noise)
do
local Fl = require("@formats/audio/flac")
local n = 441000
local a = Types.Audio.new(44100, 2, n)
local seed = 1
for i = 0, n - 1 do
seed = (seed * 1103515245 + 12345) % 2147483648
local noise = (seed / 2147483648 - 0.5) * 0.01
buffer.writef32(a.samples, i * 8, math.floor((math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1 + noise) * 32768) / 32768)
buffer.writef32(a.samples, i * 8 + 4, math.floor((math.sin(i * 0.017) * 0.3 + noise) * 32768) / 32768)
end
for _, level in { 0, 5, 8 } do
local dt = bench("flac encode -" .. level .. " (10 s st 16)", 2, n * 4, function()
Fl.encode(a, { level = level })
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", 10 / dt))
end
end
-- mp3 / mp2 encode (ws-audio): 10 s of 44.1 kHz stereo tones
do
local M3 = require("@formats/audio/mp3")
local n = 441000
local a = Types.Audio.new(44100, 2, n)
for i = 0, n - 1 do
buffer.writef32(a.samples, i * 8, math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1)
buffer.writef32(a.samples, i * 8 + 4, math.sin(i * 0.017) * 0.3)
end
for _, cfg in { { layer = 3, bitrate = 128 }, { layer = 2, bitrate = 192 } } do
local dt = bench(string.format("mp%d encode %d kbps (10 s st)", cfg.layer, cfg.bitrate), 2, n * 8, function()
M3.encode(a, cfg)
end)
print(string.format("%-8s %-30s %8.1fx real time", runtime, "", 10 / dt))
end
end
-- vorbis encode (ws-audio): 10 s of 44.1 kHz stereo tones at quality 3
do
local Vb = require("@formats/audio/vorbis")
local n = 441000
local a = Types.Audio.new(44100, 2, n)
for i = 0, n - 1 do
buffer.writef32(a.samples, i * 8, math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1)
buffer.writef32(a.samples, i * 8 + 4, math.sin(i * 0.017) * 0.3)
end
local dt = bench("vorbis encode q3 (10 s st)", 2, n * 8, function()
Vb.encode(a, { quality = 3 })
end)
print(string.format("%-8s %-30s %8.1fx real time", runtime, "", 10 / dt))
end
do
local Json = require("@formats/codecs/text/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/text/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
-- ws-arch codecs: zstd, brotli, bzip2, xz / lzma
do
local Zstd = require("@formats/codecs/compress/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/compress/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/compress/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/compress/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
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
-- ------------------------------------------------------------------------------------------------ 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/image/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
do
-- 3D (ws-3d): a 400 x 400 grid mesh (160k vertices, 318k triangles) through our own writers, then the readers; a
-- 128^3 noise voxel grid through VOX and greedy meshing
local Types = require("@formats/Types")
local MeshOps = require("@formats/ops/MeshOps")
local Gltf = require("@formats/model/gltf")
local Obj = require("@formats/model/obj")
local Stl = require("@formats/model/stl")
local Ply = require("@formats/model/ply")
local Vox = require("@formats/model/vox")
local N = 400
local pos = Types.Scene.accessor(nil, "f32", 3, N * N)
local uv = Types.Scene.accessor(nil, "f32", 2, N * N)
for y = 0, N - 1 do
for x = 0, N - 1 do
local i = y * N + x
buffer.writef32(pos.data, i * 12, x / N)
buffer.writef32(pos.data, i * 12 + 4, math.sin(x * 0.1) * math.cos(y * 0.1) * 0.05)
buffer.writef32(pos.data, i * 12 + 8, y / N)
buffer.writef32(uv.data, i * 8, x / N)
buffer.writef32(uv.data, i * 8 + 4, y / N)
end
end
local ind = Types.Scene.accessor(nil, "u32", 1, (N - 1) * (N - 1) * 6)
local k = 0
for y = 0, N - 2 do
for x = 0, N - 2 do
local a = y * N + x
for _, v in { a, a + N, a + 1, a + 1, a + N, a + N + 1 } do
buffer.writeu32(ind.data, k * 4, v)
k += 1
end
end
end
local prim = { attributes = { POSITION = pos, TEXCOORD_0 = uv }, indices = ind, mode = "triangles" } :: any
MeshOps.computeNormals(prim)
local scene = Types.Scene.fromPrimitive(prim, "grid")
bench("meshops computeNormals 318k tris", 3, buffer.len(pos.data), function()
MeshOps.computeNormals(prim)
end)
local glb = assert(Gltf.encode(scene, { binary = true }))
bench("gltf glb decode (" .. buffer.len(glb) // 1000 .. " KB)", 5, buffer.len(glb), function()
Gltf.decode(Bytes.reader(glb))
end)
bench("gltf glb encode", 3, buffer.len(glb), function()
Gltf.encode(scene, { binary = true })
end)
local obj = assert(Obj.encode(scene))
bench("obj decode (" .. buffer.len(obj) // 1000 .. " KB)", 2, buffer.len(obj), function()
Obj.decode(Bytes.reader(obj))
end)
bench("obj encode", 2, buffer.len(obj), function()
Obj.encode(scene)
end)
local stl = assert(Stl.encode(scene))
bench("stl binary decode (" .. buffer.len(stl) // 1000 .. " KB)", 5, buffer.len(stl), function()
Stl.decode(Bytes.reader(stl))
end)
local ply = assert(Ply.encode(scene))
bench("ply binary decode (" .. buffer.len(ply) // 1000 .. " KB)", 5, buffer.len(ply), function()
Ply.decode(Bytes.reader(ply))
end)
local V = 128
local grid = Types.Voxels.new(V, V, V)
for z = 0, V - 1 do
for y = 0, V - 1 do
for x = 0, V - 1 do
local h = (math.sin(x * 0.11) + math.cos(y * 0.07) + 2) * V / 8 + z * 0
if z < h then
Types.Voxels.set(grid, x, y, z, 1 + (x // 8 + y // 8) % 6)
end
end
end
end
local vox = assert(Vox.encode(grid))
bench("vox decode (" .. buffer.len(vox) // 1000 .. " KB)", 5, buffer.len(vox), function()
Vox.decode(Bytes.reader(vox))
end)
bench("greedy mesh 128^3 (MB of cells)", 3, V * V * V, function()
MeshOps.voxelsToPrimitive(grid)
end)
end
do
-- layered + vector (ws-vec): PSD decode / encode of 8 RGBA layers 1024 x 768, flatten (codecs/image/Blend), SVG
-- rasterize at 512 x 512 (codecs/image/Raster). No fixtures: the inputs are built here.
local Types = require("@formats/Types")
local Psd = require("@formats/layered/psd")
local LayerOps = require("@formats/ops/LayerOps")
local Svg = require("@formats/vector/svg")
local W, H = 1024, 768
local doc = Types.Layered.new(W, H)
local modes = { "normal", "multiply", "screen", "overlay", "normal", "softLight", "difference", "normal" }
for k = 1, 8 do
local img = Types.Image.new(W, H)
local px = img.pixels
for i = 0, W * H - 1 do
local x, y = i % W, i // W
buffer.writeu32(px, i * 4, ((x * k) % 256) + ((y * 3) % 256) * 256 + (((x + y) // 4) % 256) * 65536 + (if (x // 64 + y // 64 + k) % 3 == 0 then 0 else 200) * 16777216)
end
local l = Types.Layered.layer("L" .. k, img)
l.blend = modes[k]
doc.layers[k] = l
end
local psd = assert(Psd.encode(doc))
bench("psd decode 8 layers 1024x768", 2, W * H * 4 * 9, function()
Psd.decode(Bytes.reader(psd))
end)
bench("psd encode 8 layers (RLE)", 1, W * H * 4 * 9, function()
Psd.encode(doc)
end)
bench("flatten 8 layers (blend modes)", 2, W * H * 4 * 8, function()
LayerOps.flatten(doc)
end)
local shapes = {}
for k = 0, 39 do
table.insert(shapes, string.format("<circle cx='%d' cy='%d' r='%d' fill='url(#g)' stroke='#222' stroke-width='2' opacity='0.8'/>", (k * 37) % 100, (k * 53) % 100, 5 + k % 12))
table.insert(shapes, string.format("<path d='M%d %d q 20 -30 40 0 t 40 0' fill='none' stroke='hsl(%d, 70%%, 50%%)' stroke-width='3' stroke-dasharray='4 2'/>", k % 50, k * 2, k * 9))
end
local text = "<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100'><defs><radialGradient id='g' fx='0.3' fy='0.3'><stop offset='0' stop-color='#fff'/><stop offset='1' stop-color='#36c'/></radialGradient></defs>" .. table.concat(shapes) .. "</svg>"
local v = assert(Svg.decode(Bytes.reader(buffer.fromstring(text))))
bench("svg parse (80 elements)", 5, #text, function()
Svg.decode(Bytes.reader(buffer.fromstring(text)))
end)
bench("svg rasterize 512x512 (80 shapes)", 2, 512 * 512 * 4, function()
Svg.rasterize(v, 512, 512)
end)
end
do
local Webp = require("@formats/image/webp")
local WB = require("@tests/gen/webp_bench")
local n = WB.width * WB.height * 4
local lossy, lossless = buffer.fromstring(WB.lossy), buffer.fromstring(WB.lossless)
bench("webp lossy decode 512x512", 3, n, function()
Webp.decode(Bytes.reader(lossy))
end)
bench("webp lossless decode 512x512", 3, n, function()
Webp.decode(Bytes.reader(lossless))
end)
local img = assert(Webp.decode(Bytes.reader(lossless)))
bench("webp lossless encode (effort 5)", 1, n, function()
Webp.encode(img)
end)
bench("webp lossless encode (effort 1)", 1, n, function()
Webp.encode(img, { effort = 1 })
end)
end
do
local Xml = require("@formats/codecs/text/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
-- ws-m4a: AAC-LC / ALAC decode and ALAC encode (MB/s of 16-bit PCM equivalent: frames * channels * 2), MDCT
do
local M4a = require("@formats/audio/m4a")
local Alac = require("@formats/codecs/audio/Alac")
local Mdct = require("@formats/codecs/audio/Mdct")
local MA = require("@tests/gen/m4a_aac")
local ML = require("@tests/gen/m4a_alac")
local aac = buffer.fromstring(MA[1].data)
local a = assert(M4a.decode(Bytes.reader(aac)))
bench("aac-lc decode (m4a, stereo)", 3, a.frames * a.channels * 2, function()
M4a.decode(Bytes.reader(aac))
end)
local alac = buffer.fromstring(ML[1].data)
local l = assert(M4a.decode(Bytes.reader(alac)))
bench("alac decode (m4a, stereo 16)", 3, l.frames * l.channels * 2, function()
M4a.decode(Bytes.reader(alac))
end)
local enc = assert(Alac.encoder({ sampleRate = 44100, channels = 2, bitDepth = 16 }))
local ints = table.create(a.frames * 2, 0)
for i = 1, a.frames * 2 do
ints[i] = math.floor(buffer.readf32(a.samples, (i - 1) * 4) * 32767)
end
bench("alac encode (stereo 16)", 2, a.frames * 2 * 2, function()
local f = 0
while f < a.frames do
local n = math.min(4096, a.frames - f)
enc:encode(ints, f * 2 + 1, n)
f += n
end
end)
local plan = assert(Mdct.new(2048))
local x, X = table.create(2048, 0.5), table.create(1024, 0)
bench("mdct 2048 x 1000 (forward)", 3, 1000 * 2048 * 4, function()
for _ = 1, 1000 do
Mdct.forward(plan, x, 1, X, 1)
end
end)
end
do
local Opus = require("@formats/audio/opus")
local Cases = require("@tests/audio/_opus_cases")
local c = Cases.list[2]
local a = Cases.signal(c)
local dt = bench("opus celt encode 128k (1.5 s st)", 2, a.frames * a.channels * 2, function()
Opus.encode(a, { bitrate = 128000 })
end)
local _ = dt
end
-- MIDI through the SoundFont synth (tools/fixtures/sf2.py's test song; tools/fixtures/midi.py's "song" through the
-- built-in waveform set): seconds of stereo 44.1 kHz audio rendered per second of CPU
do
local Midi = require("@formats/music/midi")
local Sf2 = require("@formats/music/sf2")
local SF = require("@tests/gen/sf2_cases")
local MC = require("@tests/gen/midi_cases")
local sf = assert(Sf2.decode(Bytes.reader(buffer.fromstring(SF.sf2))))
local function realtime(label: string, data: string, font: any)
local song = assert(Midi.decode(Bytes.reader(buffer.fromstring(data))))
local best = math.huge
local frames = 0
for _ = 1, 3 do
local dec = assert(Midi.render(song, { soundfont = font, tail = 0 }))
local out = buffer.create(4096 * 8)
local t0 = os.clock()
frames = 0
while true do
local _, n = dec:read(4096, out)
if n == 0 then
break
end
frames += n
end
best = math.min(best, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f x realtime", runtime, label, best * 1000, frames / 44100 / best))
end
realtime("midi render sf2 (test song)", SF.song, sf)
for _, c in MC do
if c.name == "song" then
realtime("midi render fallback", c.data, nil)
end
end
end
-- the synth's per-voice cost: 32 sustained, filtered, looped voices (sf2 saw preset with a mod env) for 2 s
do
local SoundFont = require("@formats/codecs/audio/SoundFont")
local Sf2 = require("@formats/music/sf2")
local SF = require("@tests/gen/sf2_cases")
local sf = assert(Sf2.decode(Bytes.reader(buffer.fromstring(SF.sf2))))
local frames = 88200
local out = buffer.create(frames * 8)
local best = math.huge
for _ = 1, 3 do
local s = SoundFont.synth(sf, 44100)
SoundFont.Synth.midi(s, 0xC0, 1, 0)
for k = 0, 31 do
SoundFont.Synth.midi(s, 0x90, 40 + k, 100)
end
buffer.fill(out, 0, 0)
local t0 = os.clock()
for f = 0, frames - 1, 1024 do
SoundFont.Synth.render(s, out, f, 1024)
end
best = math.min(best, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f M voice-samples/s", runtime, "synth 32 voices x 2 s", best * 1000, 32 * frames / best / 1e6))
end
-- the tracker engine: 32 channels of looped samples (one note each, linear interpolation, volume ramps), and an
-- IT-style module with background voices (NNA continue) and resonant filters on every channel
do
local Tracker = require("@formats/codecs/audio/Tracker")
local function song(format: string, filter: boolean): any
local m = Tracker.newModule(format, 32)
m.order = { 0 }
m.patterns[1] = Tracker.newPattern(64, 32)
local s = Tracker.newSample("saw")
s.length = 1000
s.data = buffer.create(4000)
for i = 0, 999 do
buffer.writef32(s.data, i * 4, (i % 100) / 50 - 1)
end
s.loopStart, s.loopEnd, s.loopType = 0, 1000, 1
m.samples[1] = s
if format == "it" then
local ins = Tracker.newInstrument("nna")
for n = 1, 120 do
ins.sampleMap[n] = 1
end
ins.nna = 1
ins.fadeout = 64
if filter then
ins.cutoff, ins.resonance = 60, 40
end
m.instruments[1] = ins
end
for ch = 1, 32 do
for row = 0, 63, 16 do
Tracker.setCell(m, 0, row, ch, 37 + ch + row // 16, 1, 0, 0, 0)
end
end
return m
end
local function realtime(label: string, m: any, voices: number)
local best, frames = math.huge, 0
local out = buffer.create(4096 * 8)
for _ = 1, 3 do
local dec = Tracker.render(m) :: any
frames = 0
local t0 = os.clock()
while true do
local _, n = dec:read(4096, out)
if n == 0 then
break
end
frames += n
end
best = math.min(best, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f x realtime %5.1f M voice-samples/s", runtime, label, best * 1000, frames / 44100 / best, voices * frames / best / 1e6))
end
realtime("tracker 32 ch (mod)", song("mod", false), 32)
realtime("tracker 32 ch + NNA, filters", song("it", true), 64)
end
do
-- 3D (ws-3d): a 400 x 400 grid mesh (160k vertices, 318k triangles) through our own writers, then the readers; a
-- 128^3 noise voxel grid through VOX and greedy meshing
local Types = require("@formats/Types")
local MeshOps = require("@formats/ops/MeshOps")
local Gltf = require("@formats/model/gltf")
local Obj = require("@formats/model/obj")
local Stl = require("@formats/model/stl")
local Ply = require("@formats/model/ply")
local Vox = require("@formats/model/vox")
local N = 400
local pos = Types.Scene.accessor(nil, "f32", 3, N * N)
local uv = Types.Scene.accessor(nil, "f32", 2, N * N)
for y = 0, N - 1 do
for x = 0, N - 1 do
local i = y * N + x
buffer.writef32(pos.data, i * 12, x / N)
buffer.writef32(pos.data, i * 12 + 4, math.sin(x * 0.1) * math.cos(y * 0.1) * 0.05)
buffer.writef32(pos.data, i * 12 + 8, y / N)
buffer.writef32(uv.data, i * 8, x / N)
buffer.writef32(uv.data, i * 8 + 4, y / N)
end
end
local ind = Types.Scene.accessor(nil, "u32", 1, (N - 1) * (N - 1) * 6)
local k = 0
for y = 0, N - 2 do
for x = 0, N - 2 do
local a = y * N + x
for _, v in { a, a + N, a + 1, a + 1, a + N, a + N + 1 } do
buffer.writeu32(ind.data, k * 4, v)
k += 1
end
end
end
local prim = { attributes = { POSITION = pos, TEXCOORD_0 = uv }, indices = ind, mode = "triangles" } :: any
MeshOps.computeNormals(prim)
local scene = Types.Scene.fromPrimitive(prim, "grid")
bench("meshops computeNormals 318k tris", 3, buffer.len(pos.data), function()
MeshOps.computeNormals(prim)
end)
local glb = assert(Gltf.encode(scene, { binary = true }))
bench("gltf glb decode (" .. buffer.len(glb) // 1000 .. " KB)", 5, buffer.len(glb), function()
Gltf.decode(Bytes.reader(glb))
end)
bench("gltf glb encode", 3, buffer.len(glb), function()
Gltf.encode(scene, { binary = true })
end)
local obj = assert(Obj.encode(scene))
bench("obj decode (" .. buffer.len(obj) // 1000 .. " KB)", 2, buffer.len(obj), function()
Obj.decode(Bytes.reader(obj))
end)
bench("obj encode", 2, buffer.len(obj), function()
Obj.encode(scene)
end)
local stl = assert(Stl.encode(scene))
bench("stl binary decode (" .. buffer.len(stl) // 1000 .. " KB)", 5, buffer.len(stl), function()
Stl.decode(Bytes.reader(stl))
end)
local ply = assert(Ply.encode(scene))
bench("ply binary decode (" .. buffer.len(ply) // 1000 .. " KB)", 5, buffer.len(ply), function()
Ply.decode(Bytes.reader(ply))
end)
local Fbx = require("@formats/model/fbx")
local fbx = assert(Fbx.encode(scene))
bench("fbx binary decode (" .. buffer.len(fbx) // 1000 .. " KB)", 3, buffer.len(fbx), function()
Fbx.decode(Bytes.reader(fbx))
end)
bench("fbx binary encode (zlib arrays)", 2, buffer.len(fbx), function()
Fbx.encode(scene)
end)
bench("fbx binary encode (raw arrays)", 3, buffer.len(fbx), function()
Fbx.encode(scene, { compress = false })
end)
local fbxa = assert(Fbx.encode(scene, { ascii = true }))
bench("fbx ascii decode (" .. buffer.len(fbxa) // 1000 .. " KB)", 2, buffer.len(fbxa), function()
Fbx.decode(Bytes.reader(fbxa))
end)
local ThreeMf = require("@formats/model/3mf")
local tmf = assert(ThreeMf.encode(scene))
bench("3mf decode (" .. buffer.len(tmf) // 1000 .. " KB zipped)", 2, buffer.len(tmf), function()
ThreeMf.decode(Bytes.reader(tmf))
end)
bench("3mf encode", 2, buffer.len(tmf), function()
ThreeMf.encode(scene)
end)
local V = 128
local grid = Types.Voxels.new(V, V, V)
for z = 0, V - 1 do
for y = 0, V - 1 do
for x = 0, V - 1 do
local h = (math.sin(x * 0.11) + math.cos(y * 0.07) + 2) * V / 8 + z * 0
if z < h then
Types.Voxels.set(grid, x, y, z, 1 + (x // 8 + y // 8) % 6)
end
end
end
end
local vox = assert(Vox.encode(grid))
bench("vox decode (" .. buffer.len(vox) // 1000 .. " KB)", 5, buffer.len(vox), function()
Vox.decode(Bytes.reader(vox))
end)
bench("greedy mesh 128^3 (MB of cells)", 3, V * V * V, function()
MeshOps.voxelsToPrimitive(grid)
end)
end
do
-- STEP (tools/fixtures/step.py): the largest case, parse and write
local Step = require("@formats/cad/step")
local SC = require("@tests/gen/step_cases")
local big = SC[1]
for _, c in SC do
if #c.data > #big.data then
big = c
end
end
local data = buffer.fromstring(big.data)
local cad = assert(Step.decode(Bytes.reader(data)))
bench(string.format("step parse (%.1f MB)", #big.data / 1e6), 3, #big.data, function()
Step.decode(Bytes.reader(data))
end)
bench("step write", 3, #big.data, function()
Step.encode(cad)
end)
end
-- ------------------------------------------------------------------------------------------- ws-video-codecs: fps
do
local Jpeg = require("@formats/codecs/image/Jpeg")
local Mpeg1 = require("@formats/codecs/video/Mpeg1")
local Base64 = require("@formats/core/Base64")
local Types = require("@formats/Types")
local MB = require("@tests/gen/mjpeg_bench")
local M1 = require("@tests/gen/mpeg1_bench")
-- best of n runs of f (which handles `frames` frames): ms per frame and frames per second
local function fps(label: string, n: number, frames: number, f: () -> ())
f()
local dt = math.huge
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 fps", runtime, label, dt * 1000 / frames, frames / dt))
end
for _, c in MB.frames do
local b = Base64.decode(c.data) :: buffer
local img = assert(Jpeg.decode(b, 0, nil, { yuv = true }))
assert(Crc32.of(img.pixels) == c.yuv, "mjpeg bench: " .. c.name .. " does not match TurboJPEG")
fps("mjpeg decode yuv " .. c.name, 10, 1, function()
Jpeg.decode(b, 0, nil, { yuv = true })
end)
fps("mjpeg decode rgba " .. c.name, 10, 1, function()
Jpeg.decode(b)
end)
end
for _, s in MB.sources do
local img = Types.Image.new(s.width, s.height, "yuv420p")
img.pixels = assert(Deflate.zlib(Base64.decode(s.yuv) :: buffer))
fps(string.format("mjpeg encode yuv %dx%d q85", s.width, s.height), 5, 1, function()
Jpeg.encode(img, { quality = 85 })
end)
end
-- (LuaJIT also with the trace limits the api sets, host/init.luau: the default 1000 traces thrash on MPEG-1)
local function mpeg1(tag: string)
for _, s in M1 do
local file = Base64.decode(s.file) :: buffer
for _, rgba in { false, true } do
fps(string.format("mpeg1 decode %s %dx%d%s", if rgba then "rgba" else "yuv", s.width, s.height, tag), 3, #s.units, function()
local dec = Mpeg1.new({ reorder = true, rgba = rgba })
for i, u in s.units do
dec:decode(file, u[1], u[2], i)
end
while dec:flush() do
end
end)
end
end
end
jitTune(false)
mpeg1("")
if runtime == "luajit" then
jitTune(true)
mpeg1(" (jit tuned)")
jitTune(false)
end
end
-- ------------------------------------------------------------------------------------------- ws-video: containers
do
local F = require("@formats")
local VB = require("@tests/gen/videobench")
local function clock(n: number, f: () -> number): (number, number)
local frames = f()
local dt = math.huge
for _ = 1, n do
local t0 = os.clock()
frames = f()
dt = math.min(dt, os.clock() - t0)
end
return dt, frames
end
for _, c in VB do
local data = buffer.fromstring(c.data)
local dtOpen = clock(3, function(): number
F.video.open(data, { audio = false })
return 1
end)
for _, rgba in { false, true } do
local ok, dt, frames = pcall(clock, 2, function(): number
local v = assert(F.video.open(data, { audio = false, rgba = rgba }))
local n = 0
while true do
local f, err = v:nextFrame()
if not f then
assert(err == nil, tostring(err))
break
end
n += 1
end
return n
end)
if ok then
print(string.format("%-8s %-30s %8.2f ms %7.1f fps (open %.1f ms)", runtime, string.format("video %s %s %s", c.size, c.name, if rgba then "rgba" else "yuv"), dt * 1000 / frames, frames / dt, dtOpen * 1000))
else
print(string.format("%-8s %-30s %s", runtime, "video " .. c.size .. " " .. c.name, tostring(dt)))
end
end
if c.name == "raw.y4m" then
local dt, frames = clock(2, function(): number
assert(F.convert(data, "avi", { quality = 85 }))
return 8
end)
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, "video " .. c.size .. " y4m -> mjpeg avi", dt * 1000 / frames, frames / dt))
end
end
end
-- ------------------------------------------------------------------------------------------- ws-video-vp8: fps
do
-- VP8 video (codecs/image/Vp8, tools/fixtures/vp8video.py): whole IVF streams (libvpx, panning testsrc2) through one
-- decoder object; frames per second. On LuaJIT the library's host module raises the trace limits (maxtrace=8000,
-- see src/Formats/host/init.luau): the second pair of lines runs with them, as the api does.
local Vp8 = require("@formats/codecs/image/Vp8")
local VB = require("@tests/gen/vp8video_bench")
local function run(tag: string)
for _, c in VB do
local b = buffer.fromstring(c.ivf)
local frames = {}
local p = 32
while p + 12 <= buffer.len(b) do
local n = buffer.readu32(b, p)
table.insert(frames, { p + 12, n })
p += 12 + n
end
local dt = bench(string.format("vp8 decode %dx%d%s", c.width, c.height, tag), 3, c.width * c.height * 1.5 * #frames, function()
local dec = Vp8.newDecoder()
for _, f in frames do
dec:decodeFrame(b, f[1], f[2])
end
end)
print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #frames .. " frames)", #frames / dt))
end
end
jitTune(false)
run("")
if runtime == "luajit" then
jitTune(true)
run(" (jit tuned)")
jitTune(false)
end
end
do
-- H.264 (codecs/video/H264): whole Annex B streams, frames per second (decode + the cropped yuv420p output copies). The
-- testsrc2 clips are easy (mostly static); the mandelbrot zooms move everywhere. LuaJIT runs twice: with its default
-- trace limits and with the ones host/init.luau sets (maxtrace=8000, maxmcode=65536).
local H264 = require("@formats/codecs/video/H264")
local HB = require("@tests/gen/h264_bench")
local function run(tag: string)
for _, c in HB do
local n = c.width * c.height * 3 // 2 * #c.frames
local dt = bench(string.format("h264 %s %dx%d%s", c.name, c.width, c.height, tag), 3, n, function()
H264.decodeAll(c.data)
end)
print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #c.frames .. " frames)", #c.frames / dt))
end
end
jitTune(false)
run("")
if runtime == "luajit" then
jitTune(true)
run(" (jit tuned)")
jitTune(false)
end
end
-- ws-audio2: lossy
do
-- Musepack (codecs/audio/Musepack) SV8 / SV7 and AC-3 5.1 (codecs/audio/Ac3): whole-file decodes, MB/s of f32 output
-- (tools/fixtures/mpc.py: 1.2 s of 44.1 kHz stereo at quality 5; tools/fixtures/ac3.py: 0.16 s of 48 kHz 5.1 at
-- 448 kbps, decoded 4 times per run)
local Mpc = require("@formats/audio/mpc")
local MpcFx = require("@tests/gen/mpc_cases")
local Ac3F = require("@formats/audio/ac3")
local Ac3Fx = require("@tests/gen/ac3_cases")
for _, c in MpcFx do
if c.name == "sv8_q5_stereo" or c.name == "sv7_q5_stereo" then
local b = buffer.fromstring(c.data)
bench("mpc " .. c.name, 5, c.frames * c.channels * 4, function()
assert(Mpc.decode(Bytes.reader(b)))
end)
end
end
for _, c in Ac3Fx do
if c.name == "5.1_448" then
local b = buffer.fromstring(c.data)
bench("ac3 5.1 448 kbps", 5, c.frames * c.channels * 4 * 4, function()
for _ = 1, 4 do
assert(Ac3F.decode(Bytes.reader(b)))
end
end)
end
end
end