Skip to content

Commit

Permalink
test: add floats benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jan 12, 2022
1 parent 9a20f0e commit 1da9437
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 32 deletions.
56 changes: 24 additions & 32 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import (
"github.com/go-faster/errors"
)

//go:embed testdata/file.json
var benchData []byte
var (
//go:embed testdata/file.json
benchData []byte
//go:embed testdata/floats.json
floatsData []byte
)

func Benchmark_large_file(b *testing.B) {
b.Run("JX", func(b *testing.B) {
Expand Down Expand Up @@ -163,37 +167,25 @@ func Benchmark_large_file(b *testing.B) {
}

func BenchmarkValid(b *testing.B) {
b.Run("JX", func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(benchData)))
var d Decoder
for n := 0; n < b.N; n++ {
d.ResetBytes(benchData)
if err := d.Validate(); err != nil {
b.Fatal(err)
}
}
})
b.Run("Std", func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(benchData)))

for n := 0; n < b.N; n++ {
if !json.Valid(benchData) {
b.Fatal("invalid")
bch := []struct {
name string
input []byte
}{
{"Big", benchData},
{"Floats", floatsData},
}
for _, bench := range bch {
b.Run(bench.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(bench.input)))
var d Decoder
for n := 0; n < b.N; n++ {
d.ResetBytes(bench.input)
if err := d.Validate(); err != nil {
b.Fatal(err)
}
}
}
})
}

func Benchmark_std_large_file(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
var result []struct{}
err := json.Unmarshal(benchData, &result)
if err != nil {
b.Error(err)
}
})
}
}

Expand Down
102 changes: 102 additions & 0 deletions testdata/floats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
[
0.6046602879796196,
0.9405090880450124,
0.6645600532184904,
0.4377141871869802,
0.4246374970712657,
0.6868230728671094,
0.06563701921747622,
0.15651925473279124,
0.09696951891448456,
0.30091186058528707,
0.5152126285020654,
0.8136399609900968,
0.21426387258237492,
0.380657189299686,
0.31805817433032985,
0.4688898449024232,
0.28303415118044517,
0.29310185733681576,
0.6790846759202163,
0.21855305259276428,
0.20318687664732285,
0.360871416856906,
0.5706732760710226,
0.8624914374478864,
0.29311424455385804,
0.29708256355629153,
0.7525730355516119,
0.2065826619136986,
0.865335013001561,
0.6967191657466347,
0.5238203060500009,
0.028303083325889995,
0.15832827774512764,
0.6072534395455154,
0.9752416188605784,
0.07945362337387198,
0.5948085976830626,
0.05912065131387529,
0.692024587353112,
0.30152268100656,
0.17326623818270528,
0.5410998550087353,
0.544155573000885,
0.27850762181610883,
0.4231522015718281,
0.5305857153507052,
0.2535405005150605,
0.28208099496492467,
0.7886049150193449,
0.3618054804803169,
0.8805431227416171,
0.2971122606397708,
0.8943617293304537,
0.09745461839911657,
0.9769168685862624,
0.07429099894984302,
0.22228941700678773,
0.6810783123925709,
0.24151508854715265,
0.31152244431052484,
0.932846428518434,
0.741848959991823,
0.8010550426526613,
0.7302314772948083,
0.18292491645390843,
0.4283570818068078,
0.8969919575618727,
0.6826534880132438,
0.9789293555766876,
0.9222122589217269,
0.09083727535388708,
0.4931419977048804,
0.9269868035744142,
0.9549454404167818,
0.3479539636282229,
0.6908388315056789,
0.7109071952999951,
0.5637795958152644,
0.6494894605929404,
0.5517650490127749,
0.7558235074915978,
0.40380328579570035,
0.13065111702897217,
0.9859647293402467,
0.8963417453962161,
0.3220839705208817,
0.7211477651926741,
0.6445397825093294,
0.08552050754191123,
0.6695752976997745,
0.6227283173637045,
0.3696928436398219,
0.2368225468054852,
0.5352818906344061,
0.18724610140105305,
0.2388407028053186,
0.6280981712183633,
0.1267529293726013,
0.28133029380535923,
0.41032284435628247
]

0 comments on commit 1da9437

Please sign in to comment.