Files
rosetta/tools/bench.luau
T

134 lines
5.0 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
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
-- layered + vector (ws-vec): PSD decode / encode of 8 RGBA layers 1024 x 768, flatten (codecs/Blend), SVG
-- rasterize at 512 x 512 (codecs/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