Skip to content

Corrected and expanded Ruby protocol benchmarks#3369

Open
kpumuk wants to merge 1 commit intoapache:masterfrom
kpumuk:rb-proto-benchmark
Open

Corrected and expanded Ruby protocol benchmarks#3369
kpumuk wants to merge 1 commit intoapache:masterfrom
kpumuk:rb-proto-benchmark

Conversation

@kpumuk
Copy link
Copy Markdown
Contributor

@kpumuk kpumuk commented Mar 27, 2026

This pull requests fixes and expands Ruby protocol benchmark, with the main goal is to be able to optimize protocols and measure performance regressions. Before this change (completely broken):

$ ruby protocol_benchmark.rb
Unable to load thrift_native extension. Defaulting to pure Ruby libraries.
protocol_benchmark.rb:35:in '<main>': uninitialized constant Thrift::MemoryBuffer (NameError)

transport1 = Thrift::MemoryBuffer.new
                   ^^^^^^^^^^^^^^
Did you mean?  Thrift::MemoryBufferTransport

Major changes

  • Fixed incompatibilities in the structs
  • Added pure-ruby option to run the harness
  • Added compact and JSON protocols support
  • CLI configuration for number of runs for both small and large payloads, as well as scenario selector for targeted runs
  • Machine readable --json output.

Last two items allow to hook the benchmark script into autoresearch-style loops to fine-tune the protocol implementation.

I am currently using the benchmark in https://issues.apache.org/jira/browse/THRIFT-5938, and was able to catch a regression in the recursion depth enforcement.

Examples

$ ruby test/rb/benchmarks/protocol_benchmark.rb
Rehearsal ---------------------------------------------------------------------------------
ruby binary write large (1MB) structure once    0.253739   0.000987   0.254726 (  0.254751)
ruby binary read large (1MB) structure once     0.207167   0.005079   0.212246 (  0.212267)
c binary write large (1MB) structure once       0.159660   0.000990   0.160650 (  0.160692)
c binary read large (1MB) structure once        0.122496   0.000000   0.122496 (  0.122512)
ruby compact write large (1MB) structure once   0.156516   0.000990   0.157506 (  0.157522)
ruby compact read large (1MB) structure once    0.115820   0.000995   0.116815 (  0.116818)
ruby json write large (1MB) structure once      1.354133   0.011064   1.365197 (  1.366413)
ruby json read large (1MB) structure once       1.823078   0.007966   1.831044 (  1.831636)
ruby binary write 10000 small structures        0.126928   0.000000   0.126928 (  0.126932)
ruby binary read 10000 small structures         0.099345   0.001098   0.100443 (  0.100460)
c binary write 10000 small structures           0.076388   0.001005   0.077393 (  0.077431)
c binary read 10000 small structures            0.047427   0.000996   0.048423 (  0.048426)
ruby compact write 10000 small structures       0.078293   0.000000   0.078293 (  0.078310)
ruby compact read 10000 small structures        0.051696   0.000000   0.051696 (  0.051717)
ruby json write 10000 small structures          0.607577   0.000970   0.608547 (  0.608567)
ruby json read 10000 small structures           0.870446   0.001981   0.872427 (  0.872490)
header binary write 10000 small structures      0.177455   0.000000   0.177455 (  0.177460)
header binary read 10000 small structures       0.255720   0.001996   0.257716 (  0.257773)
header compact write 10000 small structures     0.137515   0.001004   0.138519 (  0.138550)
header compact read 10000 small structures      0.180783   0.000996   0.181779 (  0.181795)
header zlib write 10000 small structures        0.235636   0.000000   0.235636 (  0.235638)
header zlib read 10000 small structures         0.265005   0.000986   0.265991 (  0.265994)
------------------------------------------------------------------------ total: 7.441926sec

                                                    user     system      total        real
ruby binary write large (1MB) structure once    0.265310   0.002002   0.267312 (  0.267490)
ruby binary read large (1MB) structure once     0.199602   0.003000   0.202602 (  0.204004)
c binary write large (1MB) structure once       0.153209   0.002004   0.155213 (  0.155212)
c binary read large (1MB) structure once        0.112835   0.000000   0.112835 (  0.112835)
ruby compact write large (1MB) structure once   0.155836   0.000000   0.155836 (  0.155836)
ruby compact read large (1MB) structure once    0.120193   0.002997   0.123190 (  0.123204)
ruby json write large (1MB) structure once      1.308506   0.009005   1.317511 (  1.317579)
ruby json read large (1MB) structure once       1.914197   0.006003   1.920200 (  1.920275)
ruby binary write 10000 small structures        0.122585   0.000002   0.122587 (  0.122588)
ruby binary read 10000 small structures         0.084853   0.000000   0.084853 (  0.084850)
c binary write 10000 small structures           0.076188   0.000000   0.076188 (  0.076191)
c binary read 10000 small structures            0.046841   0.000000   0.046841 (  0.046832)
ruby compact write 10000 small structures       0.073694   0.000998   0.074692 (  0.074693)
ruby compact read 10000 small structures        0.048706   0.000000   0.048706 (  0.048716)
ruby json write 10000 small structures          0.619831   0.000000   0.619831 (  0.619882)
ruby json read 10000 small structures           0.864219   0.000989   0.865208 (  0.865246)
header binary write 10000 small structures      0.175500   0.000000   0.175500 (  0.175500)
header binary read 10000 small structures       0.242335   0.000000   0.242335 (  0.242333)
header compact write 10000 small structures     0.136052   0.000000   0.136052 (  0.136074)
header compact read 10000 small structures      0.191703   0.000000   0.191703 (  0.191704)
header zlib write 10000 small structures        0.235157   0.000987   0.236144 (  0.236168)
header zlib read 10000 small structures         0.260917   0.000000   0.260917 (  0.260932)

JSON:

ruby test/rb/benchmarks/protocol_benchmark.rb --large-runs 1 --small-runs 10000 --json --scenarios rb-bin-write-small 2&>/dev/null | jq

Output:

{
  "config": {
    "large_runs": 1,
    "small_runs": 10000,
    "scenarios": [
      "rb-bin-write-small"
    ],
    "skip_native": false,
    "native_available": false
  },
  "results": [
    {
      "id": "rb-bin-write-small",
      "label": "ruby binary write 10000 small structures",
      "benchmark": {
        "user": 0.13548200000000002,
        "system": 0.0007170000000000024,
        "total": 0.13619900000000001,
        "real": 0.13739799987524748
      }
    }
  ]
}
  • Did you create an Apache Jira ticket? (Request account here, not required for trivial changes)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

@mergeable mergeable bot added the ruby label Mar 27, 2026
@kpumuk kpumuk force-pushed the rb-proto-benchmark branch from 8924915 to 8e4a9b1 Compare March 27, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant