Skip to content

Commit

Permalink
Test and benchmark reflection-based []int and []string encoding (#210)
Browse files Browse the repository at this point in the history
* Add JSON Object array test and benchmark

* Add Benchmark Ints test

* Add int and string array tests

* reduce int and string slice verbosity for json and text encoder tests
  • Loading branch information
phungs authored and akshayjshah committed Dec 22, 2016
1 parent c13eae6 commit a5783ee
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ func TestMarshalerField(t *testing.T) {
assertCanBeReused(t, Marshaler("foo", fakeUser{"phil"}))
}

func TestIntsField(t *testing.T) {
assertFieldJSON(t, `"foo":[]`, Object("foo", []int{}))
assertFieldJSON(t, `"foo":[1]`, Object("foo", []int{1}))
assertFieldJSON(t, `"foo":[1,2,3]`, Object("foo", []int{1, 2, 3}))
assertCanBeReused(t, Object("foo", []int{1, 2, 3}))
}

func TestStringsField(t *testing.T) {
assertFieldJSON(t, `"foo":[]`, Object("foo", []string{}))
assertFieldJSON(t, `"foo":["bar 1"]`, Object("foo", []string{"bar 1"}))
assertFieldJSON(t, `"foo":["bar 1","bar 2","bar 3"]`, Object("foo", []string{"bar 1", "bar 2", "bar 3"}))
assertCanBeReused(t, Object("foo", []string{"bar 1", "bar 2", "bar 3"}))
}

func TestObjectField(t *testing.T) {
assertFieldJSON(t, `"foo":[5,6]`, Object("foo", []int{5, 6}))
assertCanBeReused(t, Object("foo", []int{5, 6}))
Expand Down
24 changes: 24 additions & 0 deletions json_encoder_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ func BenchmarkJSONLogMarshalerFunc(b *testing.B) {
}
}

func BenchmarkZapJSONInts(b *testing.B) {
ts := time.Unix(0, 0)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
enc := NewJSONEncoder()
enc.AddObject("ints", []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
enc.WriteEntry(ioutil.Discard, "fake", DebugLevel, ts)
enc.Free()
}
})
}

func BenchmarkZapJSONStrings(b *testing.B) {
ts := time.Unix(0, 0)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
enc := NewJSONEncoder()
enc.AddObject("strings", []string{"bar 1", "bar 2", "bar 3", "bar 4", "bar 5", "bar 6", "bar 7", "bar 8", "bar 9", "bar 10"})
enc.WriteEntry(ioutil.Discard, "fake", DebugLevel, ts)
enc.Free()
}
})
}

func BenchmarkZapJSON(b *testing.B) {
ts := time.Unix(0, 0)
b.RunParallel(func(pb *testing.PB) {
Expand Down
5 changes: 5 additions & 0 deletions json_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func TestJSONEncoderFields(t *testing.T) {
{"marshaler", `"k":{}`, func(e Encoder) {
assert.Error(t, e.AddMarshaler("k", loggable{false}), "Expected an error calling MarshalLog.")
}},
{"ints", `"k":[1,2,3]`, func(e Encoder) { e.AddObject("k", []int{1, 2, 3}) }},
{"strings", `"k":["bar 1","bar 2","bar 3"]`,
func(e Encoder) {
e.AddObject("k", []string{"bar 1", "bar 2", "bar 3"})
}},
{"arbitrary object", `"k":{"loggable":"yes"}`, func(e Encoder) {
assert.NoError(t, e.AddObject("k", map[string]string{"loggable": "yes"}), "Unexpected error JSON-serializing a map.")
}},
Expand Down
12 changes: 12 additions & 0 deletions logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ func BenchmarkMarshalerField(b *testing.B) {
})
}

func BenchmarkIntsField(b *testing.B) {
withBenchedLogger(b, func(log zap.Logger) {
log.Info("Array of Ints.", zap.Object("ints", []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
})
}

func BenchmarkStringsField(b *testing.B) {
withBenchedLogger(b, func(log zap.Logger) {
log.Info("Array of Strings.", zap.Object("strings", []string{"bar 1", "bar 2", "bar 3", "bar 4", "bar 5", "bar 6", "bar 7", "bar 8", "bar 9", "bar 10"}))
})
}

func BenchmarkObjectField(b *testing.B) {
withBenchedLogger(b, func(log zap.Logger) {
log.Info("Reflection-based serialization.", zap.Object("user", _jane))
Expand Down
5 changes: 5 additions & 0 deletions text_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func TestTextEncoderFields(t *testing.T) {
{"marshaler", "k={}", func(e Encoder) {
assert.Error(t, e.AddMarshaler("k", loggable{false}), "Expected an error calling MarshalLog.")
}},
{"ints", "k=[1 2 3]", func(e Encoder) { e.AddObject("k", []int{1, 2, 3}) }},
{"strings", `k=[bar 1 bar 2 bar 3]`,
func(e Encoder) {
e.AddObject("k", []string{"bar 1", "bar 2", "bar 3"})
}},
{"map[string]string", "k=map[loggable:yes]", func(e Encoder) {
assert.NoError(t, e.AddObject("k", map[string]string{"loggable": "yes"}), "Unexpected error serializing a map.")
}},
Expand Down

0 comments on commit a5783ee

Please sign in to comment.