File: logger_bench_test.go

package info (click to toggle)
golang-github-cactus-mlog 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: makefile: 2
file content (110 lines) | stat: -rw-r--r-- 2,554 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2012-2016 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package mlog

import (
	"io"
	"log"
	"math/rand"
	"testing"
)

const (
	letterBytes    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	letterBytesAlt = letterBytes + "\"\t\r\n"
	letterIdxBits  = 6                    // 6 bits to represent a letter index
	letterIdxMask  = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
	letterIdxMax   = 63 / letterIdxBits   // # of letter indices fitting in 63 bits
)

// uses unseeded rand (seed(1))...only use for testing!
func randString(n int, altchars bool) string {
	lb := letterBytes
	if altchars {
		lb = letterBytesAlt
	}
	b := make([]byte, n)
	for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
		if remain == 0 {
			cache, remain = rand.Int63(), letterIdxMax
		}
		if idx := int(cache & letterIdxMask); idx < len(lb) {
			b[i] = lb[idx]
			i--
		}
		cache >>= letterIdxBits
		remain--
	}

	return string(b)
}

func BenchmarkLoggingDebugWithDisabled(b *testing.B) {
	logger := New(io.Discard, 0)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Debug("this is a test")
	}
}

func BenchmarkLoggingDebugWithEnabled(b *testing.B) {
	logger := New(io.Discard, Ldebug)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Debug("this is a test")
	}
}

func BenchmarkLoggingLikeStdlib(b *testing.B) {
	logger := New(io.Discard, Lstd)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Info("this is a test")
	}
}

func BenchmarkLoggingStdlibLog(b *testing.B) {
	logger := log.New(io.Discard, "info: ", log.LstdFlags)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Print("this is a test")
	}
}

func BenchmarkLoggingLikeStdlibShortfile(b *testing.B) {
	logger := New(io.Discard, Lstd|Lshortfile)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Info("this is a test")
	}
}

func BenchmarkLoggingStdlibLogShortfile(b *testing.B) {
	logger := log.New(io.Discard, "info: ", log.LstdFlags|log.Lshortfile)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Print("this is a test")
	}
}

func BenchmarkLoggingParallelLikeStdlib(b *testing.B) {
	logger := New(io.Discard, Lstd)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			logger.Info("this is a test")
		}
	})
}

func BenchmarkLoggingParallelStdlibLog(b *testing.B) {
	logger := log.New(io.Discard, "info: ", log.LstdFlags)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			logger.Print("this is a test")
		}
	})
}