-- Throughput of the core codecs (bash tools/bench.sh core): inflate / deflate, checksums, rbxm / rbxmx, json / xml, -- F.detect, gltf / obj and the other compressors. MB/s are of the decoded side, best of n runs. local Deflate = require("@rosetta/codecs/compress/Deflate") local Crc32 = require("@rosetta/core/Crc32") local Adler32 = require("@rosetta/core/Adler32") local Base64 = require("@rosetta/core/Base64") local Md5 = require("@rosetta/core/Md5") local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/Types") local B = require("@tests/gen/deflate_bench") local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau" local filter: string? = (...) :: any if filter == "" then filter = nil end local function want(labels: string): boolean return filter == nil or string.find(labels, filter :: string, 1, true) ~= nil end local function bench(label: string, n: number, bytes: number, f: () -> ()) if filter and not string.find(label, filter, 1, true) then return 0 end f() local dt, spent, i = math.huge, 0, 0 while i < n or (spent < 1.5 and i < 40) do -- best of n, and of at least 1.5 s: the machine is shared local t0 = os.clock() f() local d = os.clock() - t0 dt, spent, i = math.min(dt, d), spent + d, i + 1 end print(string.format("%-8s %-36s %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) bench("adler32", 10, B.size, function() Adler32.of(out) end) if want("compress -6 text compress -1 text compress -6 f32 inflate f32") then local part = buffer.create(1500000) buffer.copy(part, 0, out, 0, 1500000) local c6 = assert(Deflate.compress(part, { level = 6 })) bench("compress -6 text (1.5 MB in)", 2, 1500000, function() Deflate.compress(part, { level = 6 }) end) bench("compress -1 text (1.5 MB in)", 2, 1500000, function() Deflate.compress(part, { level = 1 }) end) -- f32 vertex data: a smooth surface sampled on a grid (positions, normals, uvs), as glTF / mesh writers emit local N = 180 local fl = buffer.create(N * N * 32) for y = 0, N - 1 do for x = 0, N - 1 do local o = (y * N + x) * 32 local h = math.sin(x * 0.07) * math.cos(y * 0.05) * 3 buffer.writef32(fl, o, x * 0.25) buffer.writef32(fl, o + 4, h) buffer.writef32(fl, o + 8, y * 0.25) buffer.writef32(fl, o + 12, -math.cos(x * 0.07) * 0.2) buffer.writef32(fl, o + 16, 0.97) buffer.writef32(fl, o + 20, math.sin(y * 0.05) * 0.15) buffer.writef32(fl, o + 24, x / N) buffer.writef32(fl, o + 28, y / N) end end local cf = assert(Deflate.compress(fl, { level = 6 })) bench("compress -6 f32 (" .. buffer.len(fl) // 1000 .. " KB in)", 2, buffer.len(fl), function() Deflate.compress(fl, { level = 6 }) end) bench("inflate f32", 5, buffer.len(fl), function() Deflate.inflate(cf, 0, buffer.len(cf)) end) if not filter then print(string.format("%-8s compress -6 sizes: text %d (zlib -6 ref %s), f32 %d", runtime, buffer.len(c6), tostring((B :: any).z6 or "?"), buffer.len(cf))) end end if want("base64 encode base64 decode md5") then local s = buffer.create(3000000) buffer.copy(s, 0, out, 0, 3000000) local enc = Base64.encode(s) bench("base64 encode", 5, 3000000, function() Base64.encode(s) end) bench("base64 decode", 5, 3000000, function() Base64.decode(enc) end) bench("md5", 5, 3000000, function() Md5.of(s) end) end if want("rbxm decode rbxm encode rbxmx decode rbxmx encode") then local Rbx = require("@rosetta/codecs/model/Rbx") local Rbxm = require("@rosetta/model/rbxm") local Rbxmx = require("@rosetta/model/rbxmx") local tree = Rbx.tree() local folder = Rbx.add(tree, Rbx.instance("Folder", "Parts")) for i = 1, 20000 do local p = Rbx.add(folder, Rbx.instance("Part", "P" .. i)) Rbx.set(p, "CFrame", "CFrame", { i, i % 7, -i, 1, 0, 0, 0, 1, 0, 0, 0, 1 }) Rbx.set(p, "size", "Vector3", { 4, 1, 2 + i % 3 }) Rbx.set(p, "Color3uint8", "Color3uint8", { i % 256, 128, 255 - i % 256 }) Rbx.set(p, "Anchored", "Bool", i % 2 == 0) Rbx.set(p, "Transparency", "Float32", (i % 4) / 4) Rbx.set(p, "Material", "Enum", 256) end local bin = Rbxm.encode(tree) :: buffer local xml = Rbxmx.encode(tree) :: buffer bench("rbxm decode (20k parts)", 3, buffer.len(bin), function() Rbxm.decode(Bytes.reader(bin)) end) bench("rbxm encode (20k parts)", 3, buffer.len(bin), function() Rbxm.encode(tree) end) bench("rbxmx decode (20k parts)", 3, buffer.len(xml), function() Rbxmx.decode(Bytes.reader(xml)) end) bench("rbxmx encode (20k parts)", 3, buffer.len(xml), function() Rbxmx.encode(tree) end) end if want("json decode (indented) json decode (compact) json encode") then local Json = require("@rosetta/codecs/text/Json") local JB = require("@tests/gen/json_bench") local v = assert(Json.decode(JB.text)) 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 if want("xml decode (DOM) xml events xml encode") then local Xml = require("@rosetta/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 if want("F.detect png F.detect unknown") then -- F.detect: content sniffing over every registered format (MB/s here = thousands of detects per second) local F = require("@rosetta") local PB = require("@tests/gen/png_bench") local png = buffer.fromstring(PB[1].data) local junk = buffer.create(4096) for i = 0, 4095 do buffer.writeu8(junk, i, (i * 7919) % 251) end local t0 = os.clock() F.detect(junk) if not filter then print(string.format("%-8s %-36s %8.2f ms", runtime, "F.detect first call (loads modules)", (os.clock() - t0) * 1000)) end bench("F.detect png (k/s)", 5, 1000, function() for _ = 1, 100 do F.detect(png) end end) bench("F.detect png + name (k/s)", 5, 1000, function() for _ = 1, 100 do F.detect(png, { name = "a.png" }) end end) bench("F.detect unknown (k/s)", 5, 1000, function() for _ = 1, 100 do F.detect(junk) end end) end if want("meshops computeNormals gltf glb decode gltf glb encode gltf json+base64 decode obj decode obj encode") then local MeshOps = require("@rosetta/ops/MeshOps") local Gltf = require("@rosetta/model/gltf") local Obj = require("@rosetta/model/obj") 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", 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 gltf = assert(Gltf.encode(scene, { binary = false })) bench("gltf json+base64 decode", 3, buffer.len(gltf), function() Gltf.decode(Bytes.reader(gltf)) end) local obj = assert(Obj.encode(scene)) bench("obj decode", 2, buffer.len(obj), function() Obj.decode(Bytes.reader(obj)) end) bench("obj encode", 2, buffer.len(obj), function() Obj.encode(scene) end) end if want("zstd -3 lz4 block brotli bzip2 xz -6") then local Zstd = require("@rosetta/codecs/compress/Zstd") local ZB = require("@tests/gen/zstd_bench") local z3 = buffer.fromstring(ZB.data3) bench("zstd -3 (no checksum)", 3, ZB.size, function() Zstd.decompress(z3, nil, nil, { verify = false }) end) local Lz4 = require("@rosetta/codecs/compress/Lz4") local LB = require("@tests/gen/lz4_bench") local lz = buffer.fromstring(LB.data) bench("lz4 block decompress (hc9)", 5, LB.size, function() Lz4.decompress(lz, nil, nil, LB.size) end) local Brotli = require("@rosetta/codecs/compress/Brotli") local BB = require("@tests/gen/brotli_bench") local bz = buffer.fromstring(BB.data) bench("brotli decompress (q9 w22)", 3, BB.size, function() Brotli.decompress(bz) end) local Bzip2 = require("@rosetta/codecs/compress/Bzip2") local BZ = require("@tests/gen/bzip2_bench") local b2 = buffer.fromstring(BZ.data) bench("bzip2 decompress -9", 3, BZ.size, function() Bzip2.decompress(b2) end) local Lzma = require("@rosetta/codecs/compress/Lzma") local XB = require("@tests/gen/lzma_bench") local lx = buffer.fromstring(XB.xz) bench("xz -6 (lzma2 + crc64)", 3, XB.size, function() Lzma.xz(lx) end) end