| 1 | // Copyright 2022 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package benchmarks contains benchmarks for slog. |
| 6 | // |
| 7 | // These benchmarks are loosely based on github.com/uber-go/zap/benchmarks. |
| 8 | // They have the following desirable properties: |
| 9 | // |
| 10 | // - They test a complete log event, from the user's call to its return. |
| 11 | // |
| 12 | // - The benchmarked code is run concurrently in multiple goroutines, to |
| 13 | // better simulate a real server (the most common environment for structured |
| 14 | // logs). |
| 15 | // |
| 16 | // - Some handlers are optimistic versions of real handlers, doing real-world |
| 17 | // tasks as fast as possible (and sometimes faster, in that an |
| 18 | // implementation may not be concurrency-safe). This gives us a lower bound |
| 19 | // on handler performance, so we can evaluate the (handler-independent) core |
| 20 | // activity of the package in an end-to-end context without concern that a |
| 21 | // slow handler implementation is skewing the results. |
| 22 | // |
| 23 | // - We also test the built-in handlers, for comparison. |
| 24 | // |
| 25 | // As of Go 1.20, fetching the pc for a single nearby frame is slow. We hope to |
| 26 | // improve its speed before this package is released. Run the benchmarks with |
| 27 | // |
| 28 | // -tags nopc |
| 29 | // |
| 30 | // to remove this cost. |
| 31 | package benchmarks |
| 32 | |
| 33 | import ( |
| 34 | "errors" |
| 35 | "time" |
| 36 | |
| 37 | "golang.org/x/exp/slog" |
| 38 | ) |
| 39 | |
| 40 | // The symbols in this file are exported so that the Zap benchmarks can use them. |
| 41 | |
| 42 | const TestMessage = "Test logging, but use a somewhat realistic message length." |
| 43 | |
| 44 | var ( |
| 45 | TestTime = time.Date(2022, time.May, 1, 0, 0, 0, 0, time.UTC) |
| 46 | TestString = "7e3b3b2aaeff56a7108fe11e154200dd/7819479873059528190" |
| 47 | TestInt = 32768 |
| 48 | TestDuration = 23 * time.Second |
| 49 | TestError = errors.New("fail") |
| 50 | ) |
| 51 | |
| 52 | var TestAttrs = []slog.Attr{ |
| 53 | slog.String("string", TestString), |
| 54 | slog.Int("status", TestInt), |
| 55 | slog.Duration("duration", TestDuration), |
| 56 | slog.Time("time", TestTime), |
| 57 | slog.Any("error", TestError), |
| 58 | } |
| 59 | |
| 60 | const WantText = "time=1651363200 level=0 msg=Test logging, but use a somewhat realistic message length. string=7e3b3b2aaeff56a7108fe11e154200dd/7819479873059528190 status=32768 duration=23000000000 time=1651363200 error=fail\n" |