-- 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. 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 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