gocloak benchmarks, final numbers: five HTTP stacks, json/v2 and a cold JVM

Series: The gocloak universe. Part 1: gocloak-mcp · Part 2: gocloak-auth-proxy · Part 3: gocloak performance · Part 4: final benchmark numbers (you are here) · Part 5: pprof

In Part 3 I described how I benchmarked gocloak against Keycloak: the questions, the setup, the things that broke and the conclusions. This post has the complete tables from the final run, for everyone who wants to check the conclusions against the numbers, and four lessons the final run added.

Environment

All numbers are benchstat medians with n=8 (n=10 for the interleaved A/B runs) and GOMAXPROCS=4, built with Go 1.26.2, against Keycloak 26.8.2 in Docker Desktop on Windows 11 (AMD Ryzen AI 9 HX 370). Other coding agents were running on the same machine. Time values scatter by 30 to 40 % in places, while allocations are deterministic (±0 %). “n.s.” means not significant.

The five stacks are gocloak with its defaults, gocloak with an idle pool of 256 connections per host, plain resty, net/http with a tuned pool and a streaming decoder, and fasthttp. The benchmark code is in benchmarks/ in the gocloak repository, together with run.sh and profile.sh. In v15.1.0 the default keeps 100 idle connections per host, so the benchmark names changed: gocloak is the new default, gocloak-restypool the old resty pool and gocloak-direct uses SetDirectBodyRead(). The tables below are from before that change.

Offline: fake Keycloak, plain HTTP/1.1, one caller at a time

Time per call; the percentage compares with gocloak’s default:

Endpointgocloakgocloak, pool 256restynet/httpfasthttp
Token233 µs238 µs (n.s.)245 µs (n.s.)198 µs (n.s.)127 µs (-46 %)
One user136 µs157 µs (+15 %)173 µs (+27 %)188 µs (+38 %)111 µs (-19 %)
100 users1,637 µs1,075 µs (-34 %)833 µs (-49 %)1,148 µs (-30 %)636 µs (-61 %)
1,000 users5.33 ms6.03 ms (n.s.)5.53 ms (n.s.)7.52 ms (+41 %)5.93 ms (n.s.)
Groups306 µs252 µs (-18 %)184 µs (-40 %)312 µs (n.s.)354 µs (+15 %)
Clients502 µs327 µs (-35 %)361 µs (-28 %)1,206 µs (+140 %)436 µs (n.s.)

There is no consistent pattern. The pool setting cannot make a sequential call 15 % slower, and net/http is not 140 % slower on clients but 30 % faster on 100 users. These are the swings of a loaded machine. Even with n=8 and a p-value, the time column offline says little more than “same order of magnitude”.

The bytes per call are deterministic and tell a clear story:

Bytes per callgocloakrestynet/httpfasthttp
1,000 users3.88 MiB2.42 MiB (-38 %)2.44 MiB (-37 %)1.93 MiB (-50 %)
Token35.2 KiB27.6 KiB29.5 KiB9.5 KiB (-73 %)

All stacks need 45,100 to 45,200 allocations for 1,000 users, because the objects come from the JSON decoder. The extra 1.5 MiB in gocloak come from resty’s body limit handling, which Part 5 shows in the memory profile. v15.1.0 adds SetDirectBodyRead() as an opt-in that closes most of the gap.

Offline: 16 parallel callers over TLS

16 goroutines call GetUserByID in parallel:

TLS with HTTP/1.1. The default pool keeps too few idle connections, so many calls pay for a new TCP connection and a TLS handshake.
StackTLS + HTTP/1.1Dials per 1,000 callsTLS + HTTP/2
gocloak (default)51.0 µs30.425.2 µs
gocloak, pool 25628.2 µs (-45 %)1.09 (-96 %)27.8 µs
plain resty27.0 µs13.827.1 µs
net/http (tuned)19.2 µs (-62 %)0.7722.4 µs
fasthttp14.4 µs (-72 %)0.149.2 µs (falls back to HTTP/1.1)

Under HTTP/2 none of the Go clients opens new connections, and gocloak’s default is as fast as the large pool. Plain resty has the same default pool as gocloak but needed only 13.8 dials per 1,000 calls instead of 30.4. That value scatters by ±64 %, live resty was at gocloak’s level (237 against 247), and gocloak always runs first in each group. Probably noise or an order effect; I have not verified it.

Default and large pool also ran one after the other here. An interleaved A/B run of just those two gives 39.2 µs against 15.2 µs per call (-61 %, 2.6 times as fast) and 33.0 against 0.67 new connections per 1,000 calls (-98 %). Bytes per call drop by 25 %, allocations by 15 %.

Live: real Keycloak in Docker, plain HTTP/1.1, one caller at a time

Endpointgocloakgocloak, pool 256restynet/httpfasthttp
Token1.81 ms1.76 ms1.74 ms1.75 ms1.63 ms (-10 %)
One user906 µs938 µs952 µs959 µs820 µs (-9 %)
100 users4.66 ms4.74 ms4.68 ms4.67 ms4.59 ms (n.s.)
1,000 users39.8 ms38.9 ms39.4 ms39.0 ms38.6 ms (n.s.)
Groups3.03 ms2.99 ms3.00 ms3.03 ms2.92 ms (n.s.)
Clients1.25 ms1.28 ms1.23 ms1.24 ms1.18 ms (-6 %)

Live, all Go variants are equally fast. fasthttp saves 80 to 180 µs on small calls and nothing measurable on large ones. Bytes per call for 1,000 users live: gocloak 3.20 MiB, resty 2.19 MiB (-31 %), net/http 2.26 MiB (-29 %), fasthttp 1.35 MiB (-58 %). For the token fasthttp allocates 85 % fewer bytes.

Live with 16 parallel callers

StackTime per callDials per 1,000 calls
gocloak (default)359 µs247
gocloak, pool 256231 µs (-36 %)64 (-74 %)
plain resty442 µs (n.s.)237
net/http (tuned)231 µs (-36 %)68
fasthttp123 µs (-66 %)1.5

The 64 dials of the large pool come from the short ramp-up rounds of the benchmark, in which the pool fills. In a longer 5-second profiling run the default opened 309 connections per 1,000 calls and the large pool 4.

Local token verification with DecodeAccessToken, a real Keycloak token and a warm certificate cache: 50.5 µs.

json/v2 over the whole gocloak path

Part 3 compared the JSON engines in isolation. Here is the same comparison through gocloak’s full request path, default build against GOEXPERIMENT=jsonv2, as an interleaved A/B run against the fake Keycloak (n=10). Why interleaved: see the fourth lesson below.

gocloak call, offlinev1 enginev2 engineChange
GetUsers, 100 users697 µs529 µs-24 %
GetUsers, 1,000 users6.53 ms5.30 ms-19 %
Token, one user, groups, clientsnot significant
DecodeAccessToken31.7 µs32.2 µsnot significant

Only the user lists get measurably faster. For the other endpoints the JSON part is too small next to HTTP handling and the in-process server. The geometric mean over all A/B time values is -22 %.

Allocations drop everywhere: geometric mean -40 %, GetUsers -44 %, clients -29 %, one user -19 %, token -9 %, DecodeAccessToken -21 %.

Live I only have the sequential run.sh comparison. Latency does not change significantly there: 1,000 users go from 39.8 to 38.1 ms with p=0.065, and the remaining differences are within noise. Allocations drop as offline.

So json/v2 saves allocations and some CPU in the client. That counts for throughput under load. The latency of a single call does not change, because Keycloak dominates it. On Go 1.27 the v2 engine is the default, see Part 3.

v15.0 on Go 1.26 against v15.1 on Go 1.27

The findings of this series went into gocloak v15.1.0 (release notes, released on 21 November 2026): Go 1.27 as minimum version, which makes json/v2 the default engine, 100 idle connections per host, and SetDirectBodyRead() as an opt-in. ./ab.sh ref main compares the branch against main, with the baseline built on Go 1.26.2, interleaved as before.

Offline (n=10): GetUsers with 1,000 users -24 % time and -44 % allocations, clients -20 % and -29 %; geometric mean over all endpoints -21 % time and -27 % allocations. The pool benchmark drops from 37.2 to 15.8 µs per call (-57 %) and from 33.3 to 0.64 new connections per 1,000 calls.

Live against Keycloak (n=6): no latency change, because Keycloak dominates. Allocations drop by 10 % (token) to 27 % (user lists), bytes by 3 to 12 %, and with 16 parallel callers the new connections by 70 % (187 to 55 per 1,000 calls).

v15.1.0 has no breaking API changes. The models and the generated GoCloakIface are unchanged, and the only new export is SetDirectBodyRead(). Three things still change for users:

  • go.mod requires Go 1.27. With the default GOTOOLCHAIN=auto the go command downloads 1.27 itself; with GOTOOLCHAIN=local and an older Go the build fails.
  • The idle pool is 100 connections per host instead of GOMAXPROCS+1.
  • On Go 1.27 encoding/json can return different error texts for invalid JSON. If your code matches error strings, check it. GOEXPERIMENT=nojsonv2 selects the old engine.

If you look at the diff, the test files now write PolicyRepresentation{Clients: []string{...}} instead of going through the embedded ClientPolicyRepresentation. That is new Go 1.27 syntax: composite literals can set promoted fields of embedded structs directly, and the linter’s embedlit modernize check asks for it. The struct is the same, and the old notation still compiles.

Four lessons from the final run

A cold JVM lies to you

In my very first live run, 1,000 users took 153 to 185 ms. In the final run it was 39 ms. Same Keycloak container, same data, same client. The difference is the JVM: Keycloak keeps getting faster over many requests while the JIT compiler optimizes hot paths and caches fill. The 300 warm-up calls per endpoint in the setup were not enough for the first run.

With a JVM server like Keycloak, warm up generously and compare several runs before you trust a number. A benchmark against a freshly started Keycloak measures the warm-up, not the steady state.

One benchmark client can poison the next

The first complete run failed in the live pool benchmark. After the default gocloak client with its massive connection churn, the following clients ran into dial timeouts against [::1]:8080. The churn had overloaded Docker Desktop’s port forwarder on Windows. Even the large pool suddenly showed 300 dials per 1,000 calls and 1.1 ms instead of 0.2 ms per call. The previous client had ruined the conditions for the next one.

The fix is a configurable cool-down per target before each pool client: 2 seconds offline, 15 seconds live. After that the whole run went through without errors. If benchmarks run in a fixed order against shared infrastructure, check that a later candidate does not inherit the mess of an earlier one. Running them in a different order is a cheap test.

Git Bash rewrites arguments that start with a slash

go tool benchstat -col /client failed under Git Bash on Windows:

parsing -col: syntax error: expected key

MSYS, the layer under Git Bash, converts arguments that look like absolute Unix paths into Windows paths. /client became something like C:/Program Files/Git/client. Setting MSYS_NO_PATHCONV=1 turns that off:

Linux / macOS

# no conversion happens here, the variable is harmless
go tool benchstat -col /client results/offline-v1.txt

Windows (PowerShell)

# PowerShell passes /client unchanged
go tool benchstat -col /client results/offline-v1.txt

# in Git Bash on Windows instead:
# MSYS_NO_PATHCONV=1 go tool benchstat -col /client results/offline-v1.txt

A smaller note: run.sh uses set -o pipefail, so a single failed benchmark aborts the rest of the run. That is intended. I prefer an aborted run over half a data set that looks complete.

Interleave old and new

The run.sh comparisons of the JSON engines ran the v1 suite and the v2 suite one after the other, minutes apart. The load on the machine changed in between and faked effects of up to ±40 %. DecodeAccessToken looked 35 % slower with json/v2 offline and 39 % faster live; the interleaved A/B run shows no difference. go test -count makes it worse, because it runs the same sub-benchmark eight times in a row, so a load spike hits all eight samples of one variant.

The same artifact produced an open question in my first draft of this post: the token response seemed to profit only through the direct v2 API (32.4 against 4.4 µs). In the A/B run the v1 API on the v2 engine is 74 % faster too (14.2 to 3.7 µs). The 32 µs came from a load phase.

benchmarks/ab.sh builds both variants once with go test -c and runs them alternately for 10 rounds, one sample per round, with the order flipped every round. Part 3 shows the loop. If you compare old against new on a machine whose load you do not control, interleave the runs.

What the final numbers change

The conclusions from Part 3 hold, with sharper numbers:

  • Live, the HTTP client does not matter for latency. All Go stacks are within noise of each other; fasthttp saves 6 to 10 % on small calls.
  • The connection pool matters under parallel load: 2.6 times faster offline over TLS in the interleaved A/B run, 36 % faster live without TLS, and 98 % fewer new connections. v15.1.0 keeps 100 idle connections per host.
  • json/v2 cuts allocations by 9 to 44 % on every call. Through the whole gocloak path only the user lists get measurably faster (19 to 24 % offline), and live latency does not change.
  • gocloak allocates 30 to 60 % more bytes than plain resty because of the body limit handling. SetDirectBodyRead() in v15.1.0 cuts 39 % for 1,000 users.

The question that remained: where exactly does the time go, and why does gocloak allocate so much? That is what the profiles in Part 5 answer.