Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
go-benchmark/src/github.com/json-iterator/go-benchmark/benchmark_small_payload_test.go
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
95 lines (86 sloc)
1.93 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package go_benchmark | |
import ( | |
"testing" | |
"github.com/buger/jsonparser" | |
"github.com/json-iterator/go" | |
"encoding/json" | |
"github.com/mailru/easyjson/jlexer" | |
) | |
func BenchmarkJsonParserSmall(b *testing.B) { | |
b.ReportAllocs() | |
paths := [][]string{ | |
[]string{"uuid"}, | |
[]string{"tz"}, | |
[]string{"ua"}, | |
[]string{"st"}, | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
var data SmallPayload | |
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) { | |
switch idx { | |
case 0: | |
data.Uuid, _ = jsonparser.ParseString(value) | |
case 1: | |
v, _ := jsonparser.ParseInt(value) | |
data.Tz = int(v) | |
case 2: | |
data.Ua, _ = jsonparser.ParseString(value) | |
case 3: | |
v, _ := jsonparser.ParseInt(value) | |
data.St = int(v) | |
} | |
}, paths...) | |
} | |
} | |
func BenchmarkJsnoiterPullSmall(b *testing.B) { | |
b.ReportAllocs() | |
iter := jsoniter.ParseBytes(smallFixture) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
var data SmallPayload | |
iter.ResetBytes(smallFixture) | |
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() { | |
switch field { | |
case "uuid": | |
data.Uuid = iter.ReadString() | |
case "tz": | |
data.Tz = iter.ReadInt() | |
case "ua": | |
data.Ua = iter.ReadString() | |
case "st": | |
data.St = iter.ReadInt() | |
default: | |
iter.Skip() | |
} | |
} | |
} | |
} | |
func BenchmarkJsnoiterReflectSmall(b *testing.B) { | |
iter := jsoniter.ParseBytes(smallFixture) | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
var data SmallPayload | |
iter.ResetBytes(smallFixture) | |
jsoniter.Unmarshal(smallFixture, &data) | |
} | |
} | |
/* | |
encoding/json | |
*/ | |
func BenchmarkEncodingJsonStructSmall(b *testing.B) { | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
var data SmallPayload | |
json.Unmarshal(smallFixture, &data) | |
} | |
} | |
func BenchmarkEasyJsonSmall(b *testing.B) { | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
lexer := &jlexer.Lexer{Data: smallFixture} | |
data := new(SmallPayload) | |
data.UnmarshalEasyJSON(lexer) | |
} | |
} |