Benchmarks
These numbers measure the logger and its handler alone: every one of them writes to a writer that drops its input, so no time goes to a terminal or a disk. A real write costs more than any of these records, but it costs the same whichever handler made the line.
Each record is one request’s worth of attributes: a method, a path, a status, a duration and a size, with a remote
address, a bool, a float, an error and a user agent for ten. “Groups” is a logger with two attributes from With and
a WithGroup, and a record with a group of five.
Against slog’s own handlers and v1
Section titled “Against slog’s own handlers and v1”All of them ran on the same harness, with the same values. slog’s handlers and golog are measured through a
*slog.Logger with LogAttrs. v1 takes a string, so its benchmark formats the same key=value pairs into the message
with fmt.Sprintf, as its callers had to. v1’s console logger colours every line, so it is compared with golog in
colour; slog’s handlers have no colour, so they are compared with golog without.
Show as a table
| Scenario | slog.TextHandler | v2 |
|---|---|---|
| Message only | 259.0 ns0 allocs | 164.5 ns0 allocs |
| 5 attributes | 446.5 ns0 allocs | 302.5 ns0 allocs |
| 10 attributes | 837.0 ns2 allocs | 508.5 ns1 alloc |
| Groups | 530.7 ns0 allocs | 334.0 ns0 allocs |
| 5 attributes, 16 goroutines | 205.6 ns0 allocs | 168.9 ns0 allocs |
| Below the level | 3.2 ns0 allocs | 3.6 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Show as a table
| Scenario | slog.JSONHandler | v2 |
|---|---|---|
| Message only | 263.6 ns0 allocs | 164.5 ns0 allocs |
| 5 attributes | 445.2 ns0 allocs | 302.5 ns0 allocs |
| 10 attributes | 777.1 ns3 allocs | 508.5 ns1 alloc |
| Groups | 483.4 ns0 allocs | 334.0 ns0 allocs |
| 5 attributes, 16 goroutines | 208.7 ns0 allocs | 168.9 ns0 allocs |
| Below the level | 3.2 ns0 allocs | 3.6 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Show as a table
| Scenario | v1 | v2 in colour |
|---|---|---|
| Message only | 113.3 ns1 alloc | 174.1 ns0 allocs |
| 5 attributes | 260.7 ns7 allocs | 310.4 ns0 allocs |
| 10 attributes | 490.4 ns10 allocs | 521.8 ns1 alloc |
| Groups | 314.8 ns7 allocs | 344.2 ns0 allocs |
| 5 attributes, 16 goroutines | 204.2 ns7 allocs | 169.5 ns0 allocs |
| Below the level | 149.4 ns6 allocs | 3.6 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Where the time goes
Section titled “Where the time goes”A *slog.Logger does its own work on every record before any handler sees it: it reads the clock and looks up the
caller, so that a handler can print the source. With a handler that does nothing, that is about 130 ns here: most of
the time of a short record, whichever handler gets it. v1 only reads the clock, which is why it is quicker for a short
record from one goroutine. What golog adds on top is less than slog’s handlers add, and allocates nothing.
With many goroutines logging at once, the records are formatted in parallel and only the write waits for the lock.
In the 16-goroutine rows, the time is wall time per record across all of them, so it measures throughput, not how
long one call takes. Below the level, a record is dropped before any of its values are formatted; v1’s callers had already paid for
fmt.Sprintf by then, and its allocations.
v2’s own suite
Section titled “v2’s own suite”| Benchmark | Time | Memory | Allocations |
|---|---|---|---|
Handler/0 attrsA message and nothing else | 165.8 ns | 0 B | 0 |
Handler/5 attrsStrings, ints and a duration | 302.5 ns | 0 B | 0 |
Handler/10 attrsFive more, with an error and a key that needs quoting | 520.5 ns | 208 B | 1 |
HandlerGroupsWith, WithGroup and a group of five | 332.9 ns | 0 B | 0 |
HandlerColorFive attributes, in colour | 311.8 ns | 0 B | 0 |
HandlerSourceFive attributes and the caller’s file and line | 351.7 ns | 0 B | 0 |
HandlerDisabledA Debug record when the level is Info | 4.1 ns | 0 B | 0 |
HandlerParallelFive attributes from 16 goroutines at once | 169.7 ns | 0 B | 0 |
Run them yourself
Section titled “Run them yourself”From a clone of the repository:
go test -run '^$' -bench . -benchmemTo compare two versions, run each a few times and compare with benchstat:
go test -run '^$' -bench . -count 10 > new.txtbenchstat old.txt new.txtThe allocation counts are also enforced by tests: TestAllocations fails if a record allocates more than it should.