175 lines
5.8 KiB
Luau
175 lines
5.8 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) and tools/fixtures/png.py (1024 x 768 images).
|
|
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
|
|
|
|
-- 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/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/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
|