171 lines
5.8 KiB
Luau
171 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
|
|
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
|
|
-- 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
|