~bigbes/tarantool

tarantool-protobuf

ref: 55f0c318230172882b5b8b2bcb7460f69578c406 tarantool-protobuf/bench/packed_bench.lua -rw-r--r-- 2.3 KiB
55f0c318 — Eugene Blikh ci: cache pinned just release in Garage S3 (just/ prefix) 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env tarantool
-- Packed-scalar encode microbench for aah verification.
-- c_repeated.Holder has packed_int32 / packed_int64 / packed_sint32 /
-- packed_uint32 / packed_bool — exercises every packed varint flavor.
-- Element values are kept in the 1-byte fast-path range so the CHARS
-- lookup branch dominates (the realistic hot case).

package.path = './runtime/?.lua;./runtime/?/init.lua;'
    .. './examples/expected/?.lua;./examples/expected/?/init.lua;'
    .. package.path

-- macOS arm64 mcode arena hardening; see bench/jit_trace.lua for full rationale.
jit.opt.start('sizemcode=64', 'maxmcode=4096')

local clock = require('clock')
local ffi   = require('ffi')

local holder_full = require('full.c_repeated.c_repeated_pb')

local INT64 = ffi.typeof('int64_t')

local function build_payload(n_elems, field)
    local arr = {}
    for i = 1, n_elems do
        -- Cycle through 0-127 to stay on the 1-byte varint fast path.
        arr[i] = i % 128
    end
    if field == 'packed_int64' then
        -- Convert to int64_t cdata to match the documented convention.
        for i = 1, n_elems do arr[i] = INT64(arr[i]) end
    elseif field == 'packed_bool' then
        for i = 1, n_elems do arr[i] = (i % 2 == 0) end
    end
    return {[field] = arr}
end

local FIELDS = {'packed_int32', 'packed_sint32', 'packed_uint32', 'packed_bool', 'packed_int64'}
local SIZES = {10, 100, 1000}

local function bench_one(label, fn, p, encoded_bytes)
    for _ = 1, 1000 do fn(p) end
    local probe = 5000
    local t0 = clock.proc()
    for _ = 1, probe do fn(p) end
    local dt = clock.proc() - t0
    local rate = probe / dt
    local iters = math.max(probe, math.min(2000000, math.floor(rate * 0.5)))
    t0 = clock.proc()
    for _ = 1, iters do fn(p) end
    dt = clock.proc() - t0
    local msgs = iters / dt
    local mb = msgs * encoded_bytes / 1024 / 1024
    print(string.format('  %-30s  %10.0f msgs/s  %8.1f MB/s', label, msgs, mb))
end

print('=== Packed scalar encode bench (c_repeated.Holder) ===')
for _, field in ipairs(FIELDS) do
    for _, n in ipairs(SIZES) do
        local p = build_payload(n, field)
        local bytes = #holder_full.Holder_encode(p)
        print(string.format('\n[%s × %d, encoded %d bytes]', field, n, bytes))
        bench_one('Holder_encode', holder_full.Holder_encode, p, bytes)
    end
end