From b32c793886bc54e28783765f6185238b9774f062 Mon Sep 17 00:00:00 2001 From: Fenny <25108519+Fenny@users.noreply.github.com> Date: Mon, 14 Sep 2020 11:58:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=82=20uncomment=20internal=20benchmark?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: RW <7063188+ReneWerner87@users.noreply.github.com> --- .../bytebufferpool/bytebuffer_timing_test.go | 53 +- internal/encoding/json/golang_bench_test.go | 554 +++++++++--------- internal/encoding/json/golang_number_test.go | 26 +- internal/encoding/json/json_test.go | 104 ++-- internal/encoding/json/parse_test.go | 149 +++-- internal/encoding/json/token_test.go | 180 +++--- internal/fasttemplate/template_timing_test.go | 548 +++++++++-------- internal/schema/decoder_test.go | 52 +- 8 files changed, 822 insertions(+), 844 deletions(-) diff --git a/internal/bytebufferpool/bytebuffer_timing_test.go b/internal/bytebufferpool/bytebuffer_timing_test.go index 29f92de4..2624609a 100644 --- a/internal/bytebufferpool/bytebuffer_timing_test.go +++ b/internal/bytebufferpool/bytebuffer_timing_test.go @@ -1,32 +1,27 @@ package bytebufferpool -import ( - "bytes" - "testing" -) +// func BenchmarkByteBufferWrite(b *testing.B) { +// s := []byte("foobarbaz") +// b.RunParallel(func(pb *testing.PB) { +// var buf ByteBuffer +// for pb.Next() { +// for i := 0; i < 100; i++ { +// buf.Write(s) +// } +// buf.Reset() +// } +// }) +// } -func BenchmarkByteBufferWrite(b *testing.B) { - s := []byte("foobarbaz") - b.RunParallel(func(pb *testing.PB) { - var buf ByteBuffer - for pb.Next() { - for i := 0; i < 100; i++ { - buf.Write(s) - } - buf.Reset() - } - }) -} - -func BenchmarkBytesBufferWrite(b *testing.B) { - s := []byte("foobarbaz") - b.RunParallel(func(pb *testing.PB) { - var buf bytes.Buffer - for pb.Next() { - for i := 0; i < 100; i++ { - buf.Write(s) - } - buf.Reset() - } - }) -} +// func BenchmarkBytesBufferWrite(b *testing.B) { +// s := []byte("foobarbaz") +// b.RunParallel(func(pb *testing.PB) { +// var buf bytes.Buffer +// for pb.Next() { +// for i := 0; i < 100; i++ { +// buf.Write(s) +// } +// buf.Reset() +// } +// }) +// } diff --git a/internal/encoding/json/golang_bench_test.go b/internal/encoding/json/golang_bench_test.go index 07cc378c..b8d0ac1e 100644 --- a/internal/encoding/json/golang_bench_test.go +++ b/internal/encoding/json/golang_bench_test.go @@ -13,14 +13,8 @@ package json import ( "bytes" "compress/gzip" - "fmt" "io/ioutil" "os" - "reflect" - "runtime" - "strings" - "sync" - "testing" ) type codeResponse struct { @@ -80,295 +74,295 @@ func codeInit() { } } -func BenchmarkCodeEncoder(b *testing.B) { - b.ReportAllocs() - if codeJSON == nil { - b.StopTimer() - codeInit() - b.StartTimer() - } - b.RunParallel(func(pb *testing.PB) { - enc := NewEncoder(ioutil.Discard) - for pb.Next() { - if err := enc.Encode(&codeStruct); err != nil { - b.Fatal("Encode:", err) - } - } - }) - b.SetBytes(int64(len(codeJSON))) -} +// func BenchmarkCodeEncoder(b *testing.B) { +// b.ReportAllocs() +// if codeJSON == nil { +// b.StopTimer() +// codeInit() +// b.StartTimer() +// } +// b.RunParallel(func(pb *testing.PB) { +// enc := NewEncoder(ioutil.Discard) +// for pb.Next() { +// if err := enc.Encode(&codeStruct); err != nil { +// b.Fatal("Encode:", err) +// } +// } +// }) +// b.SetBytes(int64(len(codeJSON))) +// } -func BenchmarkCodeMarshal(b *testing.B) { - b.ReportAllocs() - if codeJSON == nil { - b.StopTimer() - codeInit() - b.StartTimer() - } - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - if _, err := Marshal(&codeStruct); err != nil { - b.Fatal("Marshal:", err) - } - } - }) - b.SetBytes(int64(len(codeJSON))) -} +// func BenchmarkCodeMarshal(b *testing.B) { +// b.ReportAllocs() +// if codeJSON == nil { +// b.StopTimer() +// codeInit() +// b.StartTimer() +// } +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// if _, err := Marshal(&codeStruct); err != nil { +// b.Fatal("Marshal:", err) +// } +// } +// }) +// b.SetBytes(int64(len(codeJSON))) +// } -func benchMarshalBytes(n int) func(*testing.B) { - sample := []byte("hello world") - // Use a struct pointer, to avoid an allocation when passing it as an - // interface parameter to Marshal. - v := &struct { - Bytes []byte - }{ - bytes.Repeat(sample, (n/len(sample))+1)[:n], - } - return func(b *testing.B) { - for i := 0; i < b.N; i++ { - if _, err := Marshal(v); err != nil { - b.Fatal("Marshal:", err) - } - } - } -} +// func benchMarshalBytes(n int) func(*testing.B) { +// sample := []byte("hello world") +// // Use a struct pointer, to avoid an allocation when passing it as an +// // interface parameter to Marshal. +// v := &struct { +// Bytes []byte +// }{ +// bytes.Repeat(sample, (n/len(sample))+1)[:n], +// } +// return func(b *testing.B) { +// for i := 0; i < b.N; i++ { +// if _, err := Marshal(v); err != nil { +// b.Fatal("Marshal:", err) +// } +// } +// } +// } -func BenchmarkMarshalBytes(b *testing.B) { - b.ReportAllocs() - // 32 fits within encodeState.scratch. - b.Run("32", benchMarshalBytes(32)) - // 256 doesn't fit in encodeState.scratch, but is small enough to - // allocate and avoid the slower base64.NewEncoder. - b.Run("256", benchMarshalBytes(256)) - // 4096 is large enough that we want to avoid allocating for it. - b.Run("4096", benchMarshalBytes(4096)) -} +// func BenchmarkMarshalBytes(b *testing.B) { +// b.ReportAllocs() +// // 32 fits within encodeState.scratch. +// b.Run("32", benchMarshalBytes(32)) +// // 256 doesn't fit in encodeState.scratch, but is small enough to +// // allocate and avoid the slower base64.NewEncoder. +// b.Run("256", benchMarshalBytes(256)) +// // 4096 is large enough that we want to avoid allocating for it. +// b.Run("4096", benchMarshalBytes(4096)) +// } -func BenchmarkCodeDecoder(b *testing.B) { - b.ReportAllocs() - if codeJSON == nil { - b.StopTimer() - codeInit() - b.StartTimer() - } - b.RunParallel(func(pb *testing.PB) { - var buf bytes.Buffer - dec := NewDecoder(&buf) - var r codeResponse - for pb.Next() { - buf.Write(codeJSON) - // hide EOF - buf.WriteByte('\n') - buf.WriteByte('\n') - buf.WriteByte('\n') - if err := dec.Decode(&r); err != nil { - b.Fatal("Decode:", err) - } - } - }) - b.SetBytes(int64(len(codeJSON))) -} +// func BenchmarkCodeDecoder(b *testing.B) { +// b.ReportAllocs() +// if codeJSON == nil { +// b.StopTimer() +// codeInit() +// b.StartTimer() +// } +// b.RunParallel(func(pb *testing.PB) { +// var buf bytes.Buffer +// dec := NewDecoder(&buf) +// var r codeResponse +// for pb.Next() { +// buf.Write(codeJSON) +// // hide EOF +// buf.WriteByte('\n') +// buf.WriteByte('\n') +// buf.WriteByte('\n') +// if err := dec.Decode(&r); err != nil { +// b.Fatal("Decode:", err) +// } +// } +// }) +// b.SetBytes(int64(len(codeJSON))) +// } -func BenchmarkUnicodeDecoder(b *testing.B) { - b.ReportAllocs() - j := []byte(`"\uD83D\uDE01"`) - b.SetBytes(int64(len(j))) - r := bytes.NewReader(j) - dec := NewDecoder(r) - var out string - b.ResetTimer() - for i := 0; i < b.N; i++ { - if err := dec.Decode(&out); err != nil { - b.Fatal("Decode:", err) - } - r.Seek(0, 0) - } -} +// func BenchmarkUnicodeDecoder(b *testing.B) { +// b.ReportAllocs() +// j := []byte(`"\uD83D\uDE01"`) +// b.SetBytes(int64(len(j))) +// r := bytes.NewReader(j) +// dec := NewDecoder(r) +// var out string +// b.ResetTimer() +// for i := 0; i < b.N; i++ { +// if err := dec.Decode(&out); err != nil { +// b.Fatal("Decode:", err) +// } +// r.Seek(0, 0) +// } +// } -func BenchmarkDecoderStream(b *testing.B) { - b.ReportAllocs() - b.StopTimer() - var buf bytes.Buffer - dec := NewDecoder(&buf) - buf.WriteString(`"` + strings.Repeat("x", 1000000) + `"` + "\n\n\n") - var x interface{} - if err := dec.Decode(&x); err != nil { - b.Fatal("Decode:", err) - } - ones := strings.Repeat(" 1\n", 300000) + "\n\n\n" - b.StartTimer() - for i := 0; i < b.N; i++ { - if i%300000 == 0 { - buf.WriteString(ones) - } - x = nil - if err := dec.Decode(&x); err != nil || x != 1.0 { - b.Fatalf("Decode: %v after %d", err, i) - } - } -} +// func BenchmarkDecoderStream(b *testing.B) { +// b.ReportAllocs() +// b.StopTimer() +// var buf bytes.Buffer +// dec := NewDecoder(&buf) +// buf.WriteString(`"` + strings.Repeat("x", 1000000) + `"` + "\n\n\n") +// var x interface{} +// if err := dec.Decode(&x); err != nil { +// b.Fatal("Decode:", err) +// } +// ones := strings.Repeat(" 1\n", 300000) + "\n\n\n" +// b.StartTimer() +// for i := 0; i < b.N; i++ { +// if i%300000 == 0 { +// buf.WriteString(ones) +// } +// x = nil +// if err := dec.Decode(&x); err != nil || x != 1.0 { +// b.Fatalf("Decode: %v after %d", err, i) +// } +// } +// } -func BenchmarkCodeUnmarshal(b *testing.B) { - b.ReportAllocs() - if codeJSON == nil { - b.StopTimer() - codeInit() - b.StartTimer() - } - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - var r codeResponse - if err := Unmarshal(codeJSON, &r); err != nil { - b.Fatal("Unmarshal:", err) - } - } - }) - b.SetBytes(int64(len(codeJSON))) -} +// func BenchmarkCodeUnmarshal(b *testing.B) { +// b.ReportAllocs() +// if codeJSON == nil { +// b.StopTimer() +// codeInit() +// b.StartTimer() +// } +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// var r codeResponse +// if err := Unmarshal(codeJSON, &r); err != nil { +// b.Fatal("Unmarshal:", err) +// } +// } +// }) +// b.SetBytes(int64(len(codeJSON))) +// } -func BenchmarkCodeUnmarshalReuse(b *testing.B) { - b.ReportAllocs() - if codeJSON == nil { - b.StopTimer() - codeInit() - b.StartTimer() - } - b.RunParallel(func(pb *testing.PB) { - var r codeResponse - for pb.Next() { - if err := Unmarshal(codeJSON, &r); err != nil { - b.Fatal("Unmarshal:", err) - } - } - }) - b.SetBytes(int64(len(codeJSON))) -} +// func BenchmarkCodeUnmarshalReuse(b *testing.B) { +// b.ReportAllocs() +// if codeJSON == nil { +// b.StopTimer() +// codeInit() +// b.StartTimer() +// } +// b.RunParallel(func(pb *testing.PB) { +// var r codeResponse +// for pb.Next() { +// if err := Unmarshal(codeJSON, &r); err != nil { +// b.Fatal("Unmarshal:", err) +// } +// } +// }) +// b.SetBytes(int64(len(codeJSON))) +// } -func BenchmarkUnmarshalString(b *testing.B) { - b.ReportAllocs() - data := []byte(`"hello, world"`) - b.RunParallel(func(pb *testing.PB) { - var s string - for pb.Next() { - if err := Unmarshal(data, &s); err != nil { - b.Fatal("Unmarshal:", err) - } - } - }) -} +// func BenchmarkUnmarshalString(b *testing.B) { +// b.ReportAllocs() +// data := []byte(`"hello, world"`) +// b.RunParallel(func(pb *testing.PB) { +// var s string +// for pb.Next() { +// if err := Unmarshal(data, &s); err != nil { +// b.Fatal("Unmarshal:", err) +// } +// } +// }) +// } -func BenchmarkUnmarshalFloat64(b *testing.B) { - b.ReportAllocs() - data := []byte(`3.14`) - b.RunParallel(func(pb *testing.PB) { - var f float64 - for pb.Next() { - if err := Unmarshal(data, &f); err != nil { - b.Fatal("Unmarshal:", err) - } - } - }) -} +// func BenchmarkUnmarshalFloat64(b *testing.B) { +// b.ReportAllocs() +// data := []byte(`3.14`) +// b.RunParallel(func(pb *testing.PB) { +// var f float64 +// for pb.Next() { +// if err := Unmarshal(data, &f); err != nil { +// b.Fatal("Unmarshal:", err) +// } +// } +// }) +// } -func BenchmarkUnmarshalInt64(b *testing.B) { - b.ReportAllocs() - data := []byte(`3`) - b.RunParallel(func(pb *testing.PB) { - var x int64 - for pb.Next() { - if err := Unmarshal(data, &x); err != nil { - b.Fatal("Unmarshal:", err) - } - } - }) -} +// func BenchmarkUnmarshalInt64(b *testing.B) { +// b.ReportAllocs() +// data := []byte(`3`) +// b.RunParallel(func(pb *testing.PB) { +// var x int64 +// for pb.Next() { +// if err := Unmarshal(data, &x); err != nil { +// b.Fatal("Unmarshal:", err) +// } +// } +// }) +// } -func BenchmarkIssue10335(b *testing.B) { - b.ReportAllocs() - j := []byte(`{"a":{ }}`) - b.RunParallel(func(pb *testing.PB) { - var s struct{} - for pb.Next() { - if err := Unmarshal(j, &s); err != nil { - b.Fatal(err) - } - } - }) -} +// func BenchmarkIssue10335(b *testing.B) { +// b.ReportAllocs() +// j := []byte(`{"a":{ }}`) +// b.RunParallel(func(pb *testing.PB) { +// var s struct{} +// for pb.Next() { +// if err := Unmarshal(j, &s); err != nil { +// b.Fatal(err) +// } +// } +// }) +// } -func BenchmarkUnmapped(b *testing.B) { - b.ReportAllocs() - j := []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`) - b.RunParallel(func(pb *testing.PB) { - var s struct{} - for pb.Next() { - if err := Unmarshal(j, &s); err != nil { - b.Fatal(err) - } - } - }) -} +// func BenchmarkUnmapped(b *testing.B) { +// b.ReportAllocs() +// j := []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`) +// b.RunParallel(func(pb *testing.PB) { +// var s struct{} +// for pb.Next() { +// if err := Unmarshal(j, &s); err != nil { +// b.Fatal(err) +// } +// } +// }) +// } -func BenchmarkTypeFieldsCache(b *testing.B) { - b.ReportAllocs() - var maxTypes int = 1e6 - if testenv.Builder() != "" { - maxTypes = 1e3 // restrict cache sizes on builders - } +// func BenchmarkTypeFieldsCache(b *testing.B) { +// b.ReportAllocs() +// var maxTypes int = 1e6 +// if testenv.Builder() != "" { +// maxTypes = 1e3 // restrict cache sizes on builders +// } - // Dynamically generate many new types. - types := make([]reflect.Type, maxTypes) - fs := []reflect.StructField{{ - Type: reflect.TypeOf(""), - Index: []int{0}, - }} - for i := range types { - fs[0].Name = fmt.Sprintf("TypeFieldsCache%d", i) - types[i] = reflect.StructOf(fs) - } +// // Dynamically generate many new types. +// types := make([]reflect.Type, maxTypes) +// fs := []reflect.StructField{{ +// Type: reflect.TypeOf(""), +// Index: []int{0}, +// }} +// for i := range types { +// fs[0].Name = fmt.Sprintf("TypeFieldsCache%d", i) +// types[i] = reflect.StructOf(fs) +// } - // clearClear clears the cache. Other JSON operations, must not be running. - clearCache := func() { - fieldCache = sync.Map{} - } +// // clearClear clears the cache. Other JSON operations, must not be running. +// clearCache := func() { +// fieldCache = sync.Map{} +// } - // MissTypes tests the performance of repeated cache misses. - // This measures the time to rebuild a cache of size nt. - for nt := 1; nt <= maxTypes; nt *= 10 { - ts := types[:nt] - b.Run(fmt.Sprintf("MissTypes%d", nt), func(b *testing.B) { - nc := runtime.GOMAXPROCS(0) - for i := 0; i < b.N; i++ { - clearCache() - var wg sync.WaitGroup - for j := 0; j < nc; j++ { - wg.Add(1) - go func(j int) { - for _, t := range ts[(j*len(ts))/nc : ((j+1)*len(ts))/nc] { - cachedTypeFields(t) - } - wg.Done() - }(j) - } - wg.Wait() - } - }) - } +// // MissTypes tests the performance of repeated cache misses. +// // This measures the time to rebuild a cache of size nt. +// for nt := 1; nt <= maxTypes; nt *= 10 { +// ts := types[:nt] +// b.Run(fmt.Sprintf("MissTypes%d", nt), func(b *testing.B) { +// nc := runtime.GOMAXPROCS(0) +// for i := 0; i < b.N; i++ { +// clearCache() +// var wg sync.WaitGroup +// for j := 0; j < nc; j++ { +// wg.Add(1) +// go func(j int) { +// for _, t := range ts[(j*len(ts))/nc : ((j+1)*len(ts))/nc] { +// cachedTypeFields(t) +// } +// wg.Done() +// }(j) +// } +// wg.Wait() +// } +// }) +// } - // HitTypes tests the performance of repeated cache hits. - // This measures the average time of each cache lookup. - for nt := 1; nt <= maxTypes; nt *= 10 { - // Pre-warm a cache of size nt. - clearCache() - for _, t := range types[:nt] { - cachedTypeFields(t) - } - b.Run(fmt.Sprintf("HitTypes%d", nt), func(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - cachedTypeFields(types[0]) - } - }) - }) - } -} +// // HitTypes tests the performance of repeated cache hits. +// // This measures the average time of each cache lookup. +// for nt := 1; nt <= maxTypes; nt *= 10 { +// // Pre-warm a cache of size nt. +// clearCache() +// for _, t := range types[:nt] { +// cachedTypeFields(t) +// } +// b.Run(fmt.Sprintf("HitTypes%d", nt), func(b *testing.B) { +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// cachedTypeFields(types[0]) +// } +// }) +// }) +// } +// } diff --git a/internal/encoding/json/golang_number_test.go b/internal/encoding/json/golang_number_test.go index f89ac8b3..ecee8c97 100644 --- a/internal/encoding/json/golang_number_test.go +++ b/internal/encoding/json/golang_number_test.go @@ -113,17 +113,17 @@ func TestNumberIsValid(t *testing.T) { } } -func BenchmarkNumberIsValid(b *testing.B) { - s := "-61657.61667E+61673" - for i := 0; i < b.N; i++ { - isValidNumber(s) - } -} +// func BenchmarkNumberIsValid(b *testing.B) { +// s := "-61657.61667E+61673" +// for i := 0; i < b.N; i++ { +// isValidNumber(s) +// } +// } -func BenchmarkNumberIsValidRegexp(b *testing.B) { - var jsonNumberRegexp = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`) - s := "-61657.61667E+61673" - for i := 0; i < b.N; i++ { - jsonNumberRegexp.MatchString(s) - } -} +// func BenchmarkNumberIsValidRegexp(b *testing.B) { +// var jsonNumberRegexp = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`) +// s := "-61657.61667E+61673" +// for i := 0; i < b.N; i++ { +// jsonNumberRegexp.MatchString(s) +// } +// } diff --git a/internal/encoding/json/json_test.go b/internal/encoding/json/json_test.go index e78debed..49c3d0da 100644 --- a/internal/encoding/json/json_test.go +++ b/internal/encoding/json/json_test.go @@ -470,47 +470,47 @@ func newValue(model interface{}) reflect.Value { return reflect.New(reflect.TypeOf(model)) } -func BenchmarkMarshal(b *testing.B) { - j := make([]byte, 0, 128*1024) +// func BenchmarkMarshal(b *testing.B) { +// j := make([]byte, 0, 128*1024) - for _, v := range testValues { - b.Run(testName(v), func(b *testing.B) { - if marshal == nil { - return - } +// for _, v := range testValues { +// b.Run(testName(v), func(b *testing.B) { +// if marshal == nil { +// return +// } - for i := 0; i != b.N; i++ { - j, _ = marshal(j[:0], v) - } +// for i := 0; i != b.N; i++ { +// j, _ = marshal(j[:0], v) +// } - b.SetBytes(int64(len(j))) - }) - } -} +// b.SetBytes(int64(len(j))) +// }) +// } +// } -func BenchmarkUnmarshal(b *testing.B) { - for _, v := range testValues { - b.Run(testName(v), func(b *testing.B) { - if unmarshal == nil { - return - } +// func BenchmarkUnmarshal(b *testing.B) { +// for _, v := range testValues { +// b.Run(testName(v), func(b *testing.B) { +// if unmarshal == nil { +// return +// } - x := v - if d, ok := x.(time.Duration); ok { - x = duration(d) - } +// x := v +// if d, ok := x.(time.Duration); ok { +// x = duration(d) +// } - j, _ := json.Marshal(x) - x = newValue(v).Interface() +// j, _ := json.Marshal(x) +// x = newValue(v).Interface() - for i := 0; i != b.N; i++ { - unmarshal(j, x) - } +// for i := 0; i != b.N; i++ { +// unmarshal(j, x) +// } - b.SetBytes(int64(len(j))) - }) - } -} +// b.SetBytes(int64(len(j))) +// }) +// } +// } type buffer struct{ data []byte } @@ -1178,28 +1178,28 @@ func TestUnmarshalFuzzBugs(t *testing.T) { } } -func BenchmarkEasyjsonUnmarshalSmallStruct(b *testing.B) { - type Hashtag struct { - Indices []int `json:"indices"` - Text string `json:"text"` - } +// func BenchmarkEasyjsonUnmarshalSmallStruct(b *testing.B) { +// type Hashtag struct { +// Indices []int `json:"indices"` +// Text string `json:"text"` +// } - //easyjson:json - type Entities struct { - Hashtags []Hashtag `json:"hashtags"` - Urls []*string `json:"urls"` - UserMentions []*string `json:"user_mentions"` - } +// //easyjson:json +// type Entities struct { +// Hashtags []Hashtag `json:"hashtags"` +// Urls []*string `json:"urls"` +// UserMentions []*string `json:"user_mentions"` +// } - var json = []byte(`{"hashtags":[{"indices":[5, 10],"text":"some-text"}],"urls":[],"user_mentions":[]}`) +// var json = []byte(`{"hashtags":[{"indices":[5, 10],"text":"some-text"}],"urls":[],"user_mentions":[]}`) - for i := 0; i < b.N; i++ { - var value Entities - if err := Unmarshal(json, &value); err != nil { - b.Fatal(err) - } - } -} +// for i := 0; i < b.N; i++ { +// var value Entities +// if err := Unmarshal(json, &value); err != nil { +// b.Fatal(err) +// } +// } +// } type testMarshaller struct { v string diff --git a/internal/encoding/json/parse_test.go b/internal/encoding/json/parse_test.go index 7fd1b53a..4199d907 100644 --- a/internal/encoding/json/parse_test.go +++ b/internal/encoding/json/parse_test.go @@ -1,7 +1,6 @@ package json import ( - "bytes" "strings" "testing" ) @@ -82,93 +81,93 @@ func TestAppendToLower(t *testing.T) { } } -func BenchmarkParseString(b *testing.B) { - s := []byte(`"__segment_internal"`) +// func BenchmarkParseString(b *testing.B) { +// s := []byte(`"__segment_internal"`) - for i := 0; i != b.N; i++ { - parseString(s) - } -} +// for i := 0; i != b.N; i++ { +// parseString(s) +// } +// } -func BenchmarkToLower(b *testing.B) { - s := []byte("someFieldWithALongName") +// func BenchmarkToLower(b *testing.B) { +// s := []byte("someFieldWithALongName") - for i := 0; i != b.N; i++ { - bytes.ToLower(s) - } -} +// for i := 0; i != b.N; i++ { +// bytes.ToLower(s) +// } +// } -func BenchmarkAppendToLower(b *testing.B) { - a := []byte(nil) - s := []byte("someFieldWithALongName") +// func BenchmarkAppendToLower(b *testing.B) { +// a := []byte(nil) +// s := []byte("someFieldWithALongName") - for i := 0; i != b.N; i++ { - a = appendToLower(a[:0], s) - } -} +// for i := 0; i != b.N; i++ { +// a = appendToLower(a[:0], s) +// } +// } -var benchmarkHasPrefixString = []byte("some random string") -var benchmarkHasPrefixResult = false +// var benchmarkHasPrefixString = []byte("some random string") +// var benchmarkHasPrefixResult = false -func BenchmarkHasPrefix(b *testing.B) { - for i := 0; i < b.N; i++ { - benchmarkHasPrefixResult = hasPrefix(benchmarkHasPrefixString, "null") - } -} +// func BenchmarkHasPrefix(b *testing.B) { +// for i := 0; i < b.N; i++ { +// benchmarkHasPrefixResult = hasPrefix(benchmarkHasPrefixString, "null") +// } +// } -func BenchmarkHasNullPrefix(b *testing.B) { - for i := 0; i < b.N; i++ { - benchmarkHasPrefixResult = hasNullPrefix(benchmarkHasPrefixString) - } -} +// func BenchmarkHasNullPrefix(b *testing.B) { +// for i := 0; i < b.N; i++ { +// benchmarkHasPrefixResult = hasNullPrefix(benchmarkHasPrefixString) +// } +// } -func BenchmarkHasTruePrefix(b *testing.B) { - for i := 0; i < b.N; i++ { - benchmarkHasPrefixResult = hasTruePrefix(benchmarkHasPrefixString) - } -} +// func BenchmarkHasTruePrefix(b *testing.B) { +// for i := 0; i < b.N; i++ { +// benchmarkHasPrefixResult = hasTruePrefix(benchmarkHasPrefixString) +// } +// } -func BenchmarkHasFalsePrefix(b *testing.B) { - for i := 0; i < b.N; i++ { - benchmarkHasPrefixResult = hasFalsePrefix(benchmarkHasPrefixString) - } -} +// func BenchmarkHasFalsePrefix(b *testing.B) { +// for i := 0; i < b.N; i++ { +// benchmarkHasPrefixResult = hasFalsePrefix(benchmarkHasPrefixString) +// } +// } -func BenchmarkParseStringEscapeNone(b *testing.B) { - var j = []byte(`"` + strings.Repeat(`a`, 1000) + `"`) - var s string - b.SetBytes(int64(len(j))) +// func BenchmarkParseStringEscapeNone(b *testing.B) { +// var j = []byte(`"` + strings.Repeat(`a`, 1000) + `"`) +// var s string +// b.SetBytes(int64(len(j))) - for i := 0; i < b.N; i++ { - if err := Unmarshal(j, &s); err != nil { - b.Fatal(err) - } - s = "" - } -} +// for i := 0; i < b.N; i++ { +// if err := Unmarshal(j, &s); err != nil { +// b.Fatal(err) +// } +// s = "" +// } +// } -func BenchmarkParseStringEscapeOne(b *testing.B) { - var j = []byte(`"` + strings.Repeat(`a`, 998) + `\n"`) - var s string - b.SetBytes(int64(len(j))) +// func BenchmarkParseStringEscapeOne(b *testing.B) { +// var j = []byte(`"` + strings.Repeat(`a`, 998) + `\n"`) +// var s string +// b.SetBytes(int64(len(j))) - for i := 0; i < b.N; i++ { - if err := Unmarshal(j, &s); err != nil { - b.Fatal(err) - } - s = "" - } -} +// for i := 0; i < b.N; i++ { +// if err := Unmarshal(j, &s); err != nil { +// b.Fatal(err) +// } +// s = "" +// } +// } -func BenchmarkParseStringEscapeAll(b *testing.B) { - var j = []byte(`"` + strings.Repeat(`\`, 1000) + `"`) - var s string - b.SetBytes(int64(len(j))) +// func BenchmarkParseStringEscapeAll(b *testing.B) { +// var j = []byte(`"` + strings.Repeat(`\`, 1000) + `"`) +// var s string +// b.SetBytes(int64(len(j))) - for i := 0; i < b.N; i++ { - if err := Unmarshal(j, &s); err != nil { - b.Fatal(err) - } - s = "" - } -} +// for i := 0; i < b.N; i++ { +// if err := Unmarshal(j, &s); err != nil { +// b.Fatal(err) +// } +// s = "" +// } +// } diff --git a/internal/encoding/json/token_test.go b/internal/encoding/json/token_test.go index 69759a33..e73ae4e6 100644 --- a/internal/encoding/json/token_test.go +++ b/internal/encoding/json/token_test.go @@ -185,103 +185,103 @@ func TestTokenizer(t *testing.T) { } } -func BenchmarkTokenizer(b *testing.B) { - values := []struct { - scenario string - payload []byte - }{ - { - scenario: "null", - payload: []byte(`null`), - }, +// func BenchmarkTokenizer(b *testing.B) { +// values := []struct { +// scenario string +// payload []byte +// }{ +// { +// scenario: "null", +// payload: []byte(`null`), +// }, - { - scenario: "true", - payload: []byte(`true`), - }, +// { +// scenario: "true", +// payload: []byte(`true`), +// }, - { - scenario: "false", - payload: []byte(`false`), - }, +// { +// scenario: "false", +// payload: []byte(`false`), +// }, - { - scenario: "number", - payload: []byte(`-1.23456789`), - }, +// { +// scenario: "number", +// payload: []byte(`-1.23456789`), +// }, - { - scenario: "string", - payload: []byte(`"1234567890"`), - }, +// { +// scenario: "string", +// payload: []byte(`"1234567890"`), +// }, - { - scenario: "object", - payload: []byte(`{ - "timestamp": "2019-01-09T18:59:57.456Z", - "channel": "server", - "type": "track", - "event": "Test", - "userId": "test-user-whatever", - "messageId": "test-message-whatever", - "integrations": { - "whatever": { - "debugMode": false - }, - "myIntegration": { - "debugMode": true - } - }, - "properties": { - "trait1": 1, - "trait2": "test", - "trait3": true - }, - "settings": { - "apiKey": "1234567890", - "debugMode": false, - "directChannels": [ - "server", - "client" - ], - "endpoint": "https://somewhere.com/v1/integrations/segment" - } -}`), - }, - } +// { +// scenario: "object", +// payload: []byte(`{ +// "timestamp": "2019-01-09T18:59:57.456Z", +// "channel": "server", +// "type": "track", +// "event": "Test", +// "userId": "test-user-whatever", +// "messageId": "test-message-whatever", +// "integrations": { +// "whatever": { +// "debugMode": false +// }, +// "myIntegration": { +// "debugMode": true +// } +// }, +// "properties": { +// "trait1": 1, +// "trait2": "test", +// "trait3": true +// }, +// "settings": { +// "apiKey": "1234567890", +// "debugMode": false, +// "directChannels": [ +// "server", +// "client" +// ], +// "endpoint": "https://somewhere.com/v1/integrations/segment" +// } +// }`), +// }, +// } - benchmarks := []struct { - scenario string - function func(*testing.B, []byte) - }{ - { - scenario: "github.com/segmentio/encoding/json", - function: func(b *testing.B, json []byte) { - t := NewTokenizer(nil) +// benchmarks := []struct { +// scenario string +// function func(*testing.B, []byte) +// }{ +// { +// scenario: "github.com/segmentio/encoding/json", +// function: func(b *testing.B, json []byte) { +// t := NewTokenizer(nil) - for i := 0; i < b.N; i++ { - t.Reset(json) +// for i := 0; i < b.N; i++ { +// t.Reset(json) - for t.Next() { - // Does nothing other than iterating over each token to measure the - // CPU and memory footprint. - } +// for t.Next() { +// // Does nothing other than iterating over each token to measure the +// // CPU and memory footprint. +// } - if t.Err != nil { - b.Error(t.Err) - } - } - }, - }, - } +// if t.Err != nil { +// b.Error(t.Err) +// } +// } +// }, +// }, +// } - for _, bechmark := range benchmarks { - b.Run(bechmark.scenario, func(b *testing.B) { - for _, value := range values { - b.Run(value.scenario, func(b *testing.B) { - bechmark.function(b, value.payload) - }) - } - }) - } -} +// for _, bechmark := range benchmarks { +// b.Run(bechmark.scenario, func(b *testing.B) { +// for _, value := range values { +// b.Run(value.scenario, func(b *testing.B) { +// bechmark.function(b, value.payload) +// }) +// } +// }) +// } +// } diff --git a/internal/fasttemplate/template_timing_test.go b/internal/fasttemplate/template_timing_test.go index 96dd8b0a..6f921573 100644 --- a/internal/fasttemplate/template_timing_test.go +++ b/internal/fasttemplate/template_timing_test.go @@ -1,312 +1,302 @@ package fasttemplate -import ( - "bytes" - "fmt" - "io" - "net/url" - "strings" - "testing" - "text/template" -) +// var ( +// source = "http://{{uid}}.foo.bar.com/?cb={{cb}}{{width}}&width={{width}}&height={{height}}&timeout={{timeout}}&uid={{uid}}&subid={{subid}}&ref={{ref}}&empty={{empty}}" +// result = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http://google.com/aaa/bbb/ccc&empty=" +// resultEscaped = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http%3A%2F%2Fgoogle.com%2Faaa%2Fbbb%2Fccc&empty=" +// resultStd = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http://google.com/aaa/bbb/ccc&empty={{empty}}" +// resultTextTemplate = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http://google.com/aaa/bbb/ccc&empty=" -var ( - source = "http://{{uid}}.foo.bar.com/?cb={{cb}}{{width}}&width={{width}}&height={{height}}&timeout={{timeout}}&uid={{uid}}&subid={{subid}}&ref={{ref}}&empty={{empty}}" - result = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http://google.com/aaa/bbb/ccc&empty=" - resultEscaped = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http%3A%2F%2Fgoogle.com%2Faaa%2Fbbb%2Fccc&empty=" - resultStd = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http://google.com/aaa/bbb/ccc&empty={{empty}}" - resultTextTemplate = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http://google.com/aaa/bbb/ccc&empty=" +// resultBytes = []byte(result) +// resultEscapedBytes = []byte(resultEscaped) +// resultStdBytes = []byte(resultStd) +// resultTextTemplateBytes = []byte(resultTextTemplate) - resultBytes = []byte(result) - resultEscapedBytes = []byte(resultEscaped) - resultStdBytes = []byte(resultStd) - resultTextTemplateBytes = []byte(resultTextTemplate) +// m = map[string]interface{}{ +// "cb": []byte("1234"), +// "width": []byte("1232"), +// "height": []byte("123"), +// "timeout": []byte("123123"), +// "uid": []byte("aaasdf"), +// "subid": []byte("asdfds"), +// "ref": []byte("http://google.com/aaa/bbb/ccc"), +// } +// ) - m = map[string]interface{}{ - "cb": []byte("1234"), - "width": []byte("1232"), - "height": []byte("123"), - "timeout": []byte("123123"), - "uid": []byte("aaasdf"), - "subid": []byte("asdfds"), - "ref": []byte("http://google.com/aaa/bbb/ccc"), - } -) +// func map2slice(m map[string]interface{}) []string { +// var a []string +// for k, v := range m { +// a = append(a, "{{"+k+"}}", string(v.([]byte))) +// } +// return a +// } -func map2slice(m map[string]interface{}) []string { - var a []string - for k, v := range m { - a = append(a, "{{"+k+"}}", string(v.([]byte))) - } - return a -} +// func BenchmarkFmtFprintf(b *testing.B) { +// b.RunParallel(func(pb *testing.PB) { +// var w bytes.Buffer +// for pb.Next() { +// fmt.Fprintf(&w, +// "http://%[5]s.foo.bar.com/?cb=%[1]s%[2]s&width=%[2]s&height=%[3]s&timeout=%[4]s&uid=%[5]s&subid=%[6]s&ref=%[7]s&empty=", +// m["cb"], m["width"], m["height"], m["timeout"], m["uid"], m["subid"], m["ref"]) +// x := w.Bytes() +// if !bytes.Equal(x, resultBytes) { +// b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result) +// } +// w.Reset() +// } +// }) +// } -func BenchmarkFmtFprintf(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - var w bytes.Buffer - for pb.Next() { - fmt.Fprintf(&w, - "http://%[5]s.foo.bar.com/?cb=%[1]s%[2]s&width=%[2]s&height=%[3]s&timeout=%[4]s&uid=%[5]s&subid=%[6]s&ref=%[7]s&empty=", - m["cb"], m["width"], m["height"], m["timeout"], m["uid"], m["subid"], m["ref"]) - x := w.Bytes() - if !bytes.Equal(x, resultBytes) { - b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result) - } - w.Reset() - } - }) -} +// func BenchmarkStringsReplace(b *testing.B) { +// mSlice := map2slice(m) -func BenchmarkStringsReplace(b *testing.B) { - mSlice := map2slice(m) +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// x := source +// for i := 0; i < len(mSlice); i += 2 { +// x = strings.Replace(x, mSlice[i], mSlice[i+1], -1) +// } +// if x != resultStd { +// b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, resultStd) +// } +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - x := source - for i := 0; i < len(mSlice); i += 2 { - x = strings.Replace(x, mSlice[i], mSlice[i+1], -1) - } - if x != resultStd { - b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, resultStd) - } - } - }) -} +// func BenchmarkStringsReplacer(b *testing.B) { +// mSlice := map2slice(m) -func BenchmarkStringsReplacer(b *testing.B) { - mSlice := map2slice(m) +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// r := strings.NewReplacer(mSlice...) +// x := r.Replace(source) +// if x != resultStd { +// b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, resultStd) +// } +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - r := strings.NewReplacer(mSlice...) - x := r.Replace(source) - if x != resultStd { - b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, resultStd) - } - } - }) -} +// func BenchmarkTextTemplate(b *testing.B) { +// s := strings.Replace(source, "{{", "{{.", -1) +// t, err := template.New("test").Parse(s) +// if err != nil { +// b.Fatalf("Error when parsing template: %s", err) +// } -func BenchmarkTextTemplate(b *testing.B) { - s := strings.Replace(source, "{{", "{{.", -1) - t, err := template.New("test").Parse(s) - if err != nil { - b.Fatalf("Error when parsing template: %s", err) - } +// mm := make(map[string]string) +// for k, v := range m { +// mm[k] = string(v.([]byte)) +// } - mm := make(map[string]string) - for k, v := range m { - mm[k] = string(v.([]byte)) - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// var w bytes.Buffer +// for pb.Next() { +// if err := t.Execute(&w, mm); err != nil { +// b.Fatalf("error when executing template: %s", err) +// } +// x := w.Bytes() +// if !bytes.Equal(x, resultTextTemplateBytes) { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultTextTemplateBytes) +// } +// w.Reset() +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - var w bytes.Buffer - for pb.Next() { - if err := t.Execute(&w, mm); err != nil { - b.Fatalf("error when executing template: %s", err) - } - x := w.Bytes() - if !bytes.Equal(x, resultTextTemplateBytes) { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultTextTemplateBytes) - } - w.Reset() - } - }) -} +// func BenchmarkFastTemplateExecuteFunc(b *testing.B) { +// t, err := NewTemplate(source, "{{", "}}") +// if err != nil { +// b.Fatalf("error in template: %s", err) +// } -func BenchmarkFastTemplateExecuteFunc(b *testing.B) { - t, err := NewTemplate(source, "{{", "}}") - if err != nil { - b.Fatalf("error in template: %s", err) - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// var w bytes.Buffer +// for pb.Next() { +// if _, err := t.ExecuteFunc(&w, testTagFunc); err != nil { +// b.Fatalf("unexpected error: %s", err) +// } +// x := w.Bytes() +// if !bytes.Equal(x, resultBytes) { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes) +// } +// w.Reset() +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - var w bytes.Buffer - for pb.Next() { - if _, err := t.ExecuteFunc(&w, testTagFunc); err != nil { - b.Fatalf("unexpected error: %s", err) - } - x := w.Bytes() - if !bytes.Equal(x, resultBytes) { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes) - } - w.Reset() - } - }) -} +// func BenchmarkFastTemplateExecute(b *testing.B) { +// t, err := NewTemplate(source, "{{", "}}") +// if err != nil { +// b.Fatalf("error in template: %s", err) +// } -func BenchmarkFastTemplateExecute(b *testing.B) { - t, err := NewTemplate(source, "{{", "}}") - if err != nil { - b.Fatalf("error in template: %s", err) - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// var w bytes.Buffer +// for pb.Next() { +// if _, err := t.Execute(&w, m); err != nil { +// b.Fatalf("unexpected error: %s", err) +// } +// x := w.Bytes() +// if !bytes.Equal(x, resultBytes) { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes) +// } +// w.Reset() +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - var w bytes.Buffer - for pb.Next() { - if _, err := t.Execute(&w, m); err != nil { - b.Fatalf("unexpected error: %s", err) - } - x := w.Bytes() - if !bytes.Equal(x, resultBytes) { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes) - } - w.Reset() - } - }) -} +// func BenchmarkFastTemplateExecuteStd(b *testing.B) { +// t, err := NewTemplate(source, "{{", "}}") +// if err != nil { +// b.Fatalf("error in template: %s", err) +// } -func BenchmarkFastTemplateExecuteStd(b *testing.B) { - t, err := NewTemplate(source, "{{", "}}") - if err != nil { - b.Fatalf("error in template: %s", err) - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// var w bytes.Buffer +// for pb.Next() { +// if _, err := t.ExecuteStd(&w, m); err != nil { +// b.Fatalf("unexpected error: %s", err) +// } +// x := w.Bytes() +// if !bytes.Equal(x, resultStdBytes) { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultStdBytes) +// } +// w.Reset() +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - var w bytes.Buffer - for pb.Next() { - if _, err := t.ExecuteStd(&w, m); err != nil { - b.Fatalf("unexpected error: %s", err) - } - x := w.Bytes() - if !bytes.Equal(x, resultStdBytes) { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultStdBytes) - } - w.Reset() - } - }) -} +// func BenchmarkFastTemplateExecuteFuncString(b *testing.B) { +// t, err := NewTemplate(source, "{{", "}}") +// if err != nil { +// b.Fatalf("error in template: %s", err) +// } -func BenchmarkFastTemplateExecuteFuncString(b *testing.B) { - t, err := NewTemplate(source, "{{", "}}") - if err != nil { - b.Fatalf("error in template: %s", err) - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// x := t.ExecuteFuncString(testTagFunc) +// if x != result { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result) +// } +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - x := t.ExecuteFuncString(testTagFunc) - if x != result { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result) - } - } - }) -} +// func BenchmarkFastTemplateExecuteString(b *testing.B) { +// t, err := NewTemplate(source, "{{", "}}") +// if err != nil { +// b.Fatalf("error in template: %s", err) +// } -func BenchmarkFastTemplateExecuteString(b *testing.B) { - t, err := NewTemplate(source, "{{", "}}") - if err != nil { - b.Fatalf("error in template: %s", err) - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// x := t.ExecuteString(m) +// if x != result { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result) +// } +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - x := t.ExecuteString(m) - if x != result { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result) - } - } - }) -} +// func BenchmarkFastTemplateExecuteStringStd(b *testing.B) { +// t, err := NewTemplate(source, "{{", "}}") +// if err != nil { +// b.Fatalf("error in template: %s", err) +// } -func BenchmarkFastTemplateExecuteStringStd(b *testing.B) { - t, err := NewTemplate(source, "{{", "}}") - if err != nil { - b.Fatalf("error in template: %s", err) - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// x := t.ExecuteStringStd(m) +// if x != resultStd { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultStd) +// } +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - x := t.ExecuteStringStd(m) - if x != resultStd { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultStd) - } - } - }) -} +// func BenchmarkFastTemplateExecuteTagFunc(b *testing.B) { +// t, err := NewTemplate(source, "{{", "}}") +// if err != nil { +// b.Fatalf("error in template: %s", err) +// } -func BenchmarkFastTemplateExecuteTagFunc(b *testing.B) { - t, err := NewTemplate(source, "{{", "}}") - if err != nil { - b.Fatalf("error in template: %s", err) - } +// mm := make(map[string]interface{}) +// for k, v := range m { +// if k == "ref" { +// vv := v.([]byte) +// v = TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(url.QueryEscape(string(vv)))) }) +// } +// mm[k] = v +// } - mm := make(map[string]interface{}) - for k, v := range m { - if k == "ref" { - vv := v.([]byte) - v = TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(url.QueryEscape(string(vv)))) }) - } - mm[k] = v - } +// b.ResetTimer() +// b.RunParallel(func(pb *testing.PB) { +// var w bytes.Buffer +// for pb.Next() { +// if _, err := t.Execute(&w, mm); err != nil { +// b.Fatalf("unexpected error: %s", err) +// } +// x := w.Bytes() +// if !bytes.Equal(x, resultEscapedBytes) { +// b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultEscapedBytes) +// } +// w.Reset() +// } +// }) +// } - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - var w bytes.Buffer - for pb.Next() { - if _, err := t.Execute(&w, mm); err != nil { - b.Fatalf("unexpected error: %s", err) - } - x := w.Bytes() - if !bytes.Equal(x, resultEscapedBytes) { - b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultEscapedBytes) - } - w.Reset() - } - }) -} +// func BenchmarkNewTemplate(b *testing.B) { +// b.RunParallel(func(pb *testing.PB) { +// for pb.Next() { +// _ = New(source, "{{", "}}") +// } +// }) +// } -func BenchmarkNewTemplate(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - _ = New(source, "{{", "}}") - } - }) -} +// func BenchmarkTemplateReset(b *testing.B) { +// b.RunParallel(func(pb *testing.PB) { +// t := New(source, "{{", "}}") +// for pb.Next() { +// t.Reset(source, "{{", "}}") +// } +// }) +// } -func BenchmarkTemplateReset(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - t := New(source, "{{", "}}") - for pb.Next() { - t.Reset(source, "{{", "}}") - } - }) -} +// func BenchmarkTemplateResetExecuteFunc(b *testing.B) { +// b.RunParallel(func(pb *testing.PB) { +// t := New(source, "{{", "}}") +// var w bytes.Buffer +// for pb.Next() { +// t.Reset(source, "{{", "}}") +// t.ExecuteFunc(&w, testTagFunc) +// w.Reset() +// } +// }) +// } -func BenchmarkTemplateResetExecuteFunc(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - t := New(source, "{{", "}}") - var w bytes.Buffer - for pb.Next() { - t.Reset(source, "{{", "}}") - t.ExecuteFunc(&w, testTagFunc) - w.Reset() - } - }) -} +// func BenchmarkExecuteFunc(b *testing.B) { +// b.RunParallel(func(pb *testing.PB) { +// var bb bytes.Buffer +// for pb.Next() { +// ExecuteFunc(source, "{{", "}}", &bb, testTagFunc) +// bb.Reset() +// } +// }) +// } -func BenchmarkExecuteFunc(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - var bb bytes.Buffer - for pb.Next() { - ExecuteFunc(source, "{{", "}}", &bb, testTagFunc) - bb.Reset() - } - }) -} - -func testTagFunc(w io.Writer, tag string) (int, error) { - if t, ok := m[tag]; ok { - return w.Write(t.([]byte)) - } - return 0, nil -} +// func testTagFunc(w io.Writer, tag string) (int, error) { +// if t, ok := m[tag]; ok { +// return w.Write(t.([]byte)) +// } +// return 0, nil +// } diff --git a/internal/schema/decoder_test.go b/internal/schema/decoder_test.go index 863891f6..0dfe7ff6 100644 --- a/internal/schema/decoder_test.go +++ b/internal/schema/decoder_test.go @@ -389,34 +389,34 @@ func TestAll(t *testing.T) { } } -func BenchmarkAll(b *testing.B) { - v := map[string][]string{ - "f1": {"1"}, - "f2": {"2"}, - "f3": {"31", "32"}, - "f4": {"41", "42"}, - "f5": {"51", "52"}, - "f6": {"61", "62"}, - "f7.f1": {"71", "72"}, - "f8.f8.f7.f1": {"81", "82"}, - "f9": {"9"}, - "f10.0.f10.0.f6": {"101", "102"}, - "f10.0.f10.1.f6": {"103", "104"}, - "f11.0.f11.0.f6": {"111", "112"}, - "f11.0.f11.1.f6": {"113", "114"}, - "f12.0.f12.0.f6": {"121", "122"}, - "f12.0.f12.1.f6": {"123", "124"}, - "f13.0.f13.0.f6": {"131", "132"}, - "f13.0.f13.1.f6": {"133", "134"}, - } +// func BenchmarkAll(b *testing.B) { +// v := map[string][]string{ +// "f1": {"1"}, +// "f2": {"2"}, +// "f3": {"31", "32"}, +// "f4": {"41", "42"}, +// "f5": {"51", "52"}, +// "f6": {"61", "62"}, +// "f7.f1": {"71", "72"}, +// "f8.f8.f7.f1": {"81", "82"}, +// "f9": {"9"}, +// "f10.0.f10.0.f6": {"101", "102"}, +// "f10.0.f10.1.f6": {"103", "104"}, +// "f11.0.f11.0.f6": {"111", "112"}, +// "f11.0.f11.1.f6": {"113", "114"}, +// "f12.0.f12.0.f6": {"121", "122"}, +// "f12.0.f12.1.f6": {"123", "124"}, +// "f13.0.f13.0.f6": {"131", "132"}, +// "f13.0.f13.1.f6": {"133", "134"}, +// } - b.ResetTimer() +// b.ResetTimer() - for i := 0; i < b.N; i++ { - s := &S1{} - _ = NewDecoder().Decode(s, v) - } -} +// for i := 0; i < b.N; i++ { +// s := &S1{} +// _ = NewDecoder().Decode(s, v) +// } +// } // ----------------------------------------------------------------------------