Skip to content

Instantly share code, notes, and snippets.

@ChrisHines
Created February 20, 2016 03:08
Show Gist options
  • Save ChrisHines/5c32bd11dd16db713776 to your computer and use it in GitHub Desktop.
Save ChrisHines/5c32bd11dd16db713776 to your computer and use it in GitHub Desktop.
Simple benchmark for three logging packages.
package main
import (
"io/ioutil"
"testing"
"github.com/Sirupsen/logrus"
"github.com/go-kit/kit/log"
"gopkg.in/inconshreveable/log15.v2"
)
func BenchmarkGokitLogfmt(b *testing.B) {
lg := log.NewLogfmtLogger(ioutil.Discard)
for i := 0; i < b.N; i++ {
lg.Log("lvl", "info", "msg", "test message", "k1", "v1", "k2", "v2", "k3", "v3")
}
}
func BenchmarkLog15Logfmt(b *testing.B) {
lg := log15.New()
lg.SetHandler(log15.StreamHandler(ioutil.Discard, log15.LogfmtFormat()))
for i := 0; i < b.N; i++ {
lg.Info("test message", "k1", "v1", "k2", "v2", "k3", "v3")
}
}
func BenchmarkLogrusLogfmt(b *testing.B) {
lg := logrus.New()
lg.Out = ioutil.Discard
for i := 0; i < b.N; i++ {
lg.WithFields(logrus.Fields{
"k1": "v1", "k2": "v2", "k3": "v3",
}).Info("test message")
}
}
@ChrisHines
Copy link
Author

On my machine just now:

    BenchmarkGokitLogfmt-4    200000          5025 ns/op         688 B/op         14 allocs/op
    BenchmarkLog15Logfmt-4    100000         18200 ns/op        2016 B/op         45 allocs/op
    BenchmarkLogrusLogfmt-4   100000         13740 ns/op        1616 B/op         27 allocs/op

@ChrisHines
Copy link
Author

GokitLogfmt has improved since the first posting. Here are updated benchmarks for Go 1.6.2 on the same machine as before with the latest versions of all the packages. This time using benchstat with 10 runs.

name            time/op
GokitLogfmt-4   3.60µs ± 3%
Log15Logfmt-4   18.2µs ± 1%
LogrusLogfmt-4  12.7µs ± 1%

name            alloc/op
GokitLogfmt-4     432B ± 0%
Log15Logfmt-4   1.96kB ± 0%
LogrusLogfmt-4  1.62kB ± 0%

name            allocs/op
GokitLogfmt-4     12.0 ± 0%
Log15Logfmt-4     45.0 ± 0%
LogrusLogfmt-4    27.0 ± 0%

@ChrisHines
Copy link
Author

Another performance improvement in GokitLogfmt: Less memory allocation when handling values that require quoting.

name            time/op
GokitLogfmt-4   3.41µs ± 3%
Log15Logfmt-4   18.4µs ± 4%
LogrusLogfmt-4  12.8µs ± 1%

name            alloc/op
GokitLogfmt-4     320B ± 0%
Log15Logfmt-4   1.96kB ± 0%
LogrusLogfmt-4  1.62kB ± 0%

name            allocs/op
GokitLogfmt-4     11.0 ± 0%
Log15Logfmt-4     45.0 ± 0%
LogrusLogfmt-4    27.0 ± 0%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment