Skip to content

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.

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.

Time per record, lower is better
Show as a table
Time per record: time and allocations, slog.TextHandler against v2
Scenarioslog.TextHandlerv2
Message only259.0 ns0 allocs164.5 ns0 allocs
5 attributes446.5 ns0 allocs302.5 ns0 allocs
10 attributes837.0 ns2 allocs508.5 ns1 alloc
Groups530.7 ns0 allocs334.0 ns0 allocs
5 attributes, 16 goroutines205.6 ns0 allocs168.9 ns0 allocs
Below the level3.2 ns0 allocs3.6 ns0 allocs

Apple M4 Max, Go 1.27.0, median of 5 runs.

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.

BenchmarkTimeMemoryAllocations
Handler/0 attrsA message and nothing else165.8 ns0 B0
Handler/5 attrsStrings, ints and a duration302.5 ns0 B0
Handler/10 attrsFive more, with an error and a key that needs quoting520.5 ns208 B1
HandlerGroupsWith, WithGroup and a group of five332.9 ns0 B0
HandlerColorFive attributes, in colour311.8 ns0 B0
HandlerSourceFive attributes and the caller’s file and line351.7 ns0 B0
HandlerDisabledA Debug record when the level is Info4.1 ns0 B0
HandlerParallelFive attributes from 16 goroutines at once169.7 ns0 B0

From a clone of the repository:

Terminal window
go test -run '^$' -bench . -benchmem

To compare two versions, run each a few times and compare with benchstat:

Terminal window
go test -run '^$' -bench . -count 10 > new.txt
benchstat old.txt new.txt

The allocation counts are also enforced by tests: TestAllocations fails if a record allocates more than it should.