| 1 |
package bench |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
L "log" |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/Sirupsen/logrus" |
| 10 |
"github.com/mgutz/logxi/v1" |
| 11 |
"gopkg.in/inconshreveable/log15.v2" |
| 12 |
) |
| 13 |
|
| 14 |
type M map[string]interface{} |
| 15 |
|
| 16 |
var testObject = M{ |
| 17 |
"foo": "bar", |
| 18 |
"bah": M{ |
| 19 |
"int": 1, |
| 20 |
"float": -100.23, |
| 21 |
"date": "06-01-01T15:04:05-0700", |
| 22 |
"bool": true, |
| 23 |
"nullable": nil, |
| 24 |
}, |
| 25 |
} |
| 26 |
|
| 27 |
var pid = os.Getpid() |
| 28 |
|
| 29 |
func toJSON(m map[string]interface{}) string { |
| 30 |
b, _ := json.Marshal(m) |
| 31 |
return string(b) |
| 32 |
} |
| 33 |
|
| 34 |
// These tests write out all log levels with concurrency turned on and |
| 35 |
// equivalent fields. |
| 36 |
|
| 37 |
func BenchmarkLog(b *testing.B) { |
| 38 |
//fmt.Println("") |
| 39 |
l := L.New(os.Stdout, "bench ", L.LstdFlags) |
| 40 |
b.ResetTimer() |
| 41 |
for i := 0; i < b.N; i++ { |
| 42 |
debug := map[string]interface{}{"l": "debug", "key1": 1, "key2": "string", "key3": false} |
| 43 |
l.Printf(toJSON(debug)) |
| 44 |
|
| 45 |
info := map[string]interface{}{"l": "info", "key1": 1, "key2": "string", "key3": false} |
| 46 |
l.Printf(toJSON(info)) |
| 47 |
|
| 48 |
warn := map[string]interface{}{"l": "warn", "key1": 1, "key2": "string", "key3": false} |
| 49 |
l.Printf(toJSON(warn)) |
| 50 |
|
| 51 |
err := map[string]interface{}{"l": "error", "key1": 1, "key2": "string", "key3": false} |
| 52 |
l.Printf(toJSON(err)) |
| 53 |
} |
| 54 |
b.StopTimer() |
| 55 |
} |
| 56 |
|
| 57 |
func BenchmarkLogComplex(b *testing.B) { |
| 58 |
//fmt.Println("") |
| 59 |
l := L.New(os.Stdout, "bench ", L.LstdFlags) |
| 60 |
b.ResetTimer() |
| 61 |
for i := 0; i < b.N; i++ { |
| 62 |
debug := map[string]interface{}{"l": "debug", "key1": 1, "obj": testObject} |
| 63 |
l.Printf(toJSON(debug)) |
| 64 |
|
| 65 |
info := map[string]interface{}{"l": "info", "key1": 1, "obj": testObject} |
| 66 |
l.Printf(toJSON(info)) |
| 67 |
|
| 68 |
warn := map[string]interface{}{"l": "warn", "key1": 1, "obj": testObject} |
| 69 |
l.Printf(toJSON(warn)) |
| 70 |
|
| 71 |
err := map[string]interface{}{"l": "error", "key1": 1, "obj": testObject} |
| 72 |
l.Printf(toJSON(err)) |
| 73 |
} |
| 74 |
b.StopTimer() |
| 75 |
} |
| 76 |
|
| 77 |
func BenchmarkLogxi(b *testing.B) { |
| 78 |
//fmt.Println("") |
| 79 |
stdout := log.NewConcurrentWriter(os.Stdout) |
| 80 |
l := log.NewLogger3(stdout, "bench", log.NewJSONFormatter("bench")) |
| 81 |
l.SetLevel(log.LevelDebug) |
| 82 |
|
| 83 |
b.ResetTimer() |
| 84 |
for i := 0; i < b.N; i++ { |
| 85 |
l.Debug("debug", "key", 1, "key2", "string", "key3", false) |
| 86 |
l.Info("info", "key", 1, "key2", "string", "key3", false) |
| 87 |
l.Warn("warn", "key", 1, "key2", "string", "key3", false) |
| 88 |
l.Error("error", "key", 1, "key2", "string", "key3", false) |
| 89 |
} |
| 90 |
b.StopTimer() |
| 91 |
} |
| 92 |
|
| 93 |
func BenchmarkLogxiComplex(b *testing.B) { |
| 94 |
//fmt.Println("") |
| 95 |
stdout := log.NewConcurrentWriter(os.Stdout) |
| 96 |
l := log.NewLogger3(stdout, "bench", log.NewJSONFormatter("bench")) |
| 97 |
l.SetLevel(log.LevelDebug) |
| 98 |
|
| 99 |
b.ResetTimer() |
| 100 |
for i := 0; i < b.N; i++ { |
| 101 |
l.Debug("debug", "key", 1, "obj", testObject) |
| 102 |
l.Info("info", "key", 1, "obj", testObject) |
| 103 |
l.Warn("warn", "key", 1, "obj", testObject) |
| 104 |
l.Error("error", "key", 1, "obj", testObject) |
| 105 |
} |
| 106 |
b.StopTimer() |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
func BenchmarkLogrus(b *testing.B) { |
| 111 |
//fmt.Println("") |
| 112 |
l := logrus.New() |
| 113 |
l.Formatter = &logrus.JSONFormatter{} |
| 114 |
|
| 115 |
b.ResetTimer() |
| 116 |
for i := 0; i < b.N; i++ { |
| 117 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "key2": "string", "key3": false}).Debug("debug") |
| 118 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "key2": "string", "key3": false}).Info("info") |
| 119 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "key2": "string", "key3": false}).Warn("warn") |
| 120 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "key2": "string", "key3": false}).Error("error") |
| 121 |
} |
| 122 |
b.StopTimer() |
| 123 |
} |
| 124 |
|
| 125 |
func BenchmarkLogrusComplex(b *testing.B) { |
| 126 |
//fmt.Println("") |
| 127 |
l := logrus.New() |
| 128 |
l.Formatter = &logrus.JSONFormatter{} |
| 129 |
|
| 130 |
b.ResetTimer() |
| 131 |
for i := 0; i < b.N; i++ { |
| 132 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "obj": testObject}).Debug("debug") |
| 133 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "obj": testObject}).Info("info") |
| 134 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "obj": testObject}).Warn("warn") |
| 135 |
l.WithFields(logrus.Fields{"_n": "bench", "_p": pid, "key": 1, "obj": testObject}).Error("error") |
| 136 |
} |
| 137 |
b.StopTimer() |
| 138 |
} |
| 139 |
|
| 140 |
func BenchmarkLog15(b *testing.B) { |
| 141 |
//fmt.Println("") |
| 142 |
l := log15.New(log15.Ctx{"_n": "bench", "_p": pid}) |
| 143 |
l.SetHandler(log15.SyncHandler(log15.StreamHandler(os.Stdout, log15.JsonFormat()))) |
| 144 |
|
| 145 |
b.ResetTimer() |
| 146 |
for i := 0; i < b.N; i++ { |
| 147 |
l.Debug("debug", "key", 1, "key2", "string", "key3", false) |
| 148 |
l.Info("info", "key", 1, "key2", "string", "key3", false) |
| 149 |
l.Warn("warn", "key", 1, "key2", "string", "key3", false) |
| 150 |
l.Error("error", "key", 1, "key2", "string", "key3", false) |
| 151 |
} |
| 152 |
b.StopTimer() |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
func BenchmarkLog15Complex(b *testing.B) { |
| 157 |
//fmt.Println("") |
| 158 |
l := log15.New(log15.Ctx{"_n": "bench", "_p": pid}) |
| 159 |
l.SetHandler(log15.SyncHandler(log15.StreamHandler(os.Stdout, log15.JsonFormat()))) |
| 160 |
|
| 161 |
b.ResetTimer() |
| 162 |
for i := 0; i < b.N; i++ { |
| 163 |
l.Debug("debug", "key", 1, "obj", testObject) |
| 164 |
l.Info("info", "key", 1, "obj", testObject) |
| 165 |
l.Warn("warn", "key", 1, "obj", testObject) |
| 166 |
l.Error("error", "key", 1, "obj", testObject) |
| 167 |
} |
| 168 |
b.StopTimer() |
| 169 |
} |