Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -35,19 +36,32 @@ type reportLine struct {
Notes string `json:"notes"`
}

func BenchAndReportSerializers(generateReport bool, validate bool) error {
func BenchAndReportSerializers(generateReport bool, validate bool, namesRe *regexp.Regexp) error {
data := make([]reportLine, len(benchmarkCases))
for i, bench := range benchmarkCases {
if namesRe != nil && !namesRe.MatchString(bench.Name) {
continue
}

marshalRes := testing.Benchmark(func(b *testing.B) {
goserbench.BenchMarshalSmallStruct(b, bench.New())
})
fmt.Printf("%10s - Marshal - %s %s\n", bench.Name, marshalRes.String(),
marshalRes.MemString())

unmarshalOk := false
unmarshalRes := testing.Benchmark(func(b *testing.B) {
goserbench.BenchUnmarshalSmallStruct(b, bench.New(), validate)
unmarshalOk = true
})
fmt.Printf("%10s - Unmarshal - %s %s\n", bench.Name, unmarshalRes.String(),
unmarshalRes.MemString())
if !unmarshalOk {
fmt.Printf("\nUnmarshal benchmark of %q did not complete successfully\n", bench.Name)
fmt.Printf("Test with go test -validate -run Bench -bench BenchmarkSerializers/unmarshal/%s\n\n", bench.Name)
return fmt.Errorf("benchmark %s failed", bench.Name)
} else {
fmt.Printf("%10s - Unmarshal - %s %s\n", bench.Name, unmarshalRes.String(),
unmarshalRes.MemString())
}

data[i] = reportLine{
Name: bench.Name,
Expand Down
14 changes: 13 additions & 1 deletion genreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package main

import (
"fmt"
"os"
"regexp"
"testing"
)

Expand All @@ -19,7 +21,17 @@ Re-Generating Report = true

`, *validate)

err := BenchAndReportSerializers(true, *validate)
var nameRe *regexp.Regexp
if *nameFlag != "" {
var err error
nameRe, err = regexp.Compile(*nameFlag)
if err != nil {
fmt.Printf("Error compiling -name regexp: %v\n", err)
os.Exit(1)
}
}

err := BenchAndReportSerializers(true, *validate, nameRe)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 3 additions & 0 deletions goserbench/goserbench.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func BenchUnmarshalSmallStruct(b *testing.B, s Serializer, validate bool) {
if !o.BirthDay.Equal(i.BirthDay) {
b.Fatalf("wrong birthday: got %v, want %v", o.BirthDay, i.BirthDay)
}
if o.Money != i.Money {
b.Fatalf("wrong money: got %v, want %v", o.Money, i.Money)
}
}
}
}
20 changes: 19 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package main
import (
"flag"
"fmt"
"os"
"regexp"
"testing"
)

var (
validate = flag.Bool("validate", false, "to validate the correctness of the serializers")
genReport = flag.Bool("genreport", false, "to re-generate the report")
nameFlag = flag.String("name", "", "restrict benchmarks to run only if they match this regexp")
)

func main() {
Expand All @@ -22,7 +26,21 @@ Re-Generating Report = %t

`, *validate, *genReport)

err := BenchAndReportSerializers(*genReport, *validate)
var nameRe *regexp.Regexp
if *nameFlag != "" {
var err error
nameRe, err = regexp.Compile(*nameFlag)
if err != nil {
fmt.Printf("Error compiling -name regexp: %v\n", err)
os.Exit(1)
}
}

// Manually call testing.Init(), otherwise validation errors may cause
// internal panics.
testing.Init()

err := BenchAndReportSerializers(*genReport, *validate, nameRe)
if err != nil {
panic(err)
}
Expand Down
Loading