1// Copyright (c) 2016 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21package zapcore_test
22
23import (
24	"encoding/json"
25	"testing"
26	"time"
27
28	. "go.uber.org/zap/zapcore"
29)
30
31func BenchmarkJSONLogMarshalerFunc(b *testing.B) {
32	for i := 0; i < b.N; i++ {
33		enc := NewJSONEncoder(testEncoderConfig())
34		enc.AddObject("nested", ObjectMarshalerFunc(func(enc ObjectEncoder) error {
35			enc.AddInt64("i", int64(i))
36			return nil
37		}))
38	}
39}
40
41func BenchmarkZapJSON(b *testing.B) {
42	b.RunParallel(func(pb *testing.PB) {
43		for pb.Next() {
44			enc := NewJSONEncoder(testEncoderConfig())
45			enc.AddString("str", "foo")
46			enc.AddInt64("int64-1", 1)
47			enc.AddInt64("int64-2", 2)
48			enc.AddFloat64("float64", 1.0)
49			enc.AddString("string1", "\n")
50			enc.AddString("string2", "��")
51			enc.AddString("string3", "��")
52			enc.AddString("string4", "��")
53			enc.AddBool("bool", true)
54			buf, _ := enc.EncodeEntry(Entry{
55				Message: "fake",
56				Level:   DebugLevel,
57			}, nil)
58			buf.Free()
59		}
60	})
61}
62
63func BenchmarkStandardJSON(b *testing.B) {
64	record := struct {
65		Level   string                 `json:"level"`
66		Message string                 `json:"msg"`
67		Time    time.Time              `json:"ts"`
68		Fields  map[string]interface{} `json:"fields"`
69	}{
70		Level:   "debug",
71		Message: "fake",
72		Time:    time.Unix(0, 0),
73		Fields: map[string]interface{}{
74			"str":     "foo",
75			"int64-1": int64(1),
76			"int64-2": int64(1),
77			"float64": float64(1.0),
78			"string1": "\n",
79			"string2": "��",
80			"string3": "��",
81			"string4": "��",
82			"bool":    true,
83		},
84	}
85	b.ResetTimer()
86	b.RunParallel(func(pb *testing.PB) {
87		for pb.Next() {
88			json.Marshal(record)
89		}
90	})
91}
92