-
-
Save bigpigeon/93f90fe84cf96f10356541c22db6e7dc to your computer and use it in GitHub Desktop.
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 main | |
import ( | |
"encoding/json" | |
"io" | |
"strings" | |
"testing" | |
) | |
const benchmarhData = `{ | |
"a": "1", | |
"b": {"c": {"d": {"e": {"f": "2"}}, | |
"d2": 22, | |
"d3": [{"e3": {"f4":"4"}}, {"e4": 44}] | |
} | |
}, | |
"g": "3" | |
}` | |
func BenchmarkJsonDecode(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var v interface{} | |
err := json.NewDecoder(strings.NewReader(benchmarhData)).Decode(&v) | |
if err != nil { | |
b.Fatal(err) | |
} | |
} | |
b.ReportAllocs() | |
} | |
func getAllToken(decoder *json.Decoder) { | |
for { | |
_, err := decoder.Token() | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
panic(err) | |
} | |
} | |
} | |
func BenchmarkGetToken(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
getAllToken(json.NewDecoder(strings.NewReader(benchmarhData))) | |
} | |
b.ReportAllocs() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment