-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbenchmarks_test.go
111 lines (93 loc) · 2.89 KB
/
benchmarks_test.go
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
111
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package loggo_test
import (
"io/ioutil"
"os"
gc "gopkg.in/check.v1"
"github.com/juju/loggo/v2"
)
type BenchmarksSuite struct {
logger loggo.Logger
writer *writer
}
var _ = gc.Suite(&BenchmarksSuite{})
func (s *BenchmarksSuite) SetUpTest(c *gc.C) {
loggo.ResetLogging()
s.logger = loggo.GetLogger("test.writer")
s.writer = &writer{}
err := loggo.RegisterWriter("test", s.writer)
c.Assert(err, gc.IsNil)
}
func (s *BenchmarksSuite) BenchmarkLoggingNoWriters(c *gc.C) {
// No writers
_, _ = loggo.RemoveWriter("test")
for i := 0; i < c.N; i++ {
s.logger.Warningf("just a simple warning for %d", i)
}
}
func (s *BenchmarksSuite) BenchmarkLoggingNoWritersNoFormat(c *gc.C) {
// No writers
_, _ = loggo.RemoveWriter("test")
for i := 0; i < c.N; i++ {
s.logger.Warningf("just a simple warning")
}
}
func (s *BenchmarksSuite) BenchmarkLoggingTestWriters(c *gc.C) {
for i := 0; i < c.N; i++ {
s.logger.Warningf("just a simple warning for %d", i)
}
c.Assert(s.writer.Log(), gc.HasLen, c.N)
}
func (s *BenchmarksSuite) BenchmarkLoggingDiskWriter(c *gc.C) {
logFile := s.setupTempFileWriter(c)
defer logFile.Close()
msg := "just a simple warning for %d"
for i := 0; i < c.N; i++ {
s.logger.Warningf(msg, i)
}
offset, err := logFile.Seek(0, os.SEEK_CUR)
c.Assert(err, gc.IsNil)
c.Assert((offset > int64(len(msg))*int64(c.N)), gc.Equals, true,
gc.Commentf("Not enough data was written to the log file."))
}
func (s *BenchmarksSuite) BenchmarkLoggingDiskWriterNoMessages(c *gc.C) {
logFile := s.setupTempFileWriter(c)
defer logFile.Close()
// Change the log level
writer, err := loggo.RemoveWriter("testfile")
c.Assert(err, gc.IsNil)
err = loggo.RegisterWriter("testfile", loggo.NewMinimumLevelWriter(writer, loggo.WARNING))
c.Assert(err, gc.IsNil)
msg := "just a simple warning for %d"
for i := 0; i < c.N; i++ {
s.logger.Debugf(msg, i)
}
offset, err := logFile.Seek(0, os.SEEK_CUR)
c.Assert(err, gc.IsNil)
c.Assert(offset, gc.Equals, int64(0),
gc.Commentf("Data was written to the log file."))
}
func (s *BenchmarksSuite) BenchmarkLoggingDiskWriterNoMessagesLogLevel(c *gc.C) {
logFile := s.setupTempFileWriter(c)
defer logFile.Close()
// Change the log level
s.logger.SetLogLevel(loggo.WARNING)
msg := "just a simple warning for %d"
for i := 0; i < c.N; i++ {
s.logger.Debugf(msg, i)
}
offset, err := logFile.Seek(0, os.SEEK_CUR)
c.Assert(err, gc.IsNil)
c.Assert(offset, gc.Equals, int64(0),
gc.Commentf("Data was written to the log file."))
}
func (s *BenchmarksSuite) setupTempFileWriter(c *gc.C) *os.File {
_, _ = loggo.RemoveWriter("test")
logFile, err := ioutil.TempFile(c.MkDir(), "loggo-test")
c.Assert(err, gc.IsNil)
writer := loggo.NewSimpleWriter(logFile, loggo.DefaultFormatter)
err = loggo.RegisterWriter("testfile", writer)
c.Assert(err, gc.IsNil)
return logFile
}