]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/csv: update and add CSV reading benchmarks
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 4 Oct 2016 23:34:01 +0000 (23:34 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 5 Oct 2016 04:29:07 +0000 (04:29 +0000)
Benchmarks broken off from https://golang.org/cl/24723 and modified to
allocate less in the places we're not trying to measure.

Updates #16791

Change-Id: I508e4cfeac60322d56f1d71ff1912f6a6f183a63
Reviewed-on: https://go-review.googlesource.com/30357
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/csv/reader_test.go

index be1002d034a1768c8b5e47ee225e01f0222f3142..7b3aca4c5f3771cbec52beb09d6e1174b3b0ec14 100644 (file)
@@ -5,6 +5,7 @@
 package csv
 
 import (
+       "io"
        "reflect"
        "strings"
        "testing"
@@ -292,8 +293,52 @@ func TestRead(t *testing.T) {
        }
 }
 
-func BenchmarkRead(b *testing.B) {
-       data := `x,y,z,w
+// nTimes is an io.Reader which yields the string s n times.
+type nTimes struct {
+       s   string
+       n   int
+       off int
+}
+
+func (r *nTimes) Read(p []byte) (n int, err error) {
+       for {
+               if r.n <= 0 || r.s == "" {
+                       return n, io.EOF
+               }
+               n0 := copy(p, r.s[r.off:])
+               p = p[n0:]
+               n += n0
+               r.off += n0
+               if r.off == len(r.s) {
+                       r.off = 0
+                       r.n--
+               }
+               if len(p) == 0 {
+                       return
+               }
+       }
+}
+
+// benchmarkRead measures reading the provided CSV rows data.
+// initReader, if non-nil, modifies the Reader before it's used.
+func benchmarkRead(b *testing.B, initReader func(*Reader), rows string) {
+       b.ReportAllocs()
+       r := NewReader(&nTimes{s: rows, n: b.N})
+       if initReader != nil {
+               initReader(r)
+       }
+       for {
+               _, err := r.Read()
+               if err == io.EOF {
+                       break
+               }
+               if err != nil {
+                       b.Fatal(err)
+               }
+       }
+}
+
+const benchmarkCSVData = `x,y,z,w
 x,y,z,
 x,y,,
 x,,,
@@ -305,11 +350,22 @@ x,,,
 "","","",""
 `
 
-       for i := 0; i < b.N; i++ {
-               _, err := NewReader(strings.NewReader(data)).ReadAll()
+func BenchmarkRead(b *testing.B) {
+       benchmarkRead(b, nil, benchmarkCSVData)
+}
 
-               if err != nil {
-                       b.Fatalf("could not read data: %s", err)
-               }
-       }
+func BenchmarkReadWithFieldsPerRecord(b *testing.B) {
+       benchmarkRead(b, func(r *Reader) { r.FieldsPerRecord = 4 }, benchmarkCSVData)
+}
+
+func BenchmarkReadWithoutFieldsPerRecord(b *testing.B) {
+       benchmarkRead(b, func(r *Reader) { r.FieldsPerRecord = -1 }, benchmarkCSVData)
+}
+
+func BenchmarkReadLargeFields(b *testing.B) {
+       benchmarkRead(b, nil, strings.Repeat(`xxxxxxxxxxxxxxxx,yyyyyyyyyyyyyyyy,zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww,vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
+xxxxxxxxxxxxxxxxxxxxxxxx,yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww,vvvv
+,,zzzz,wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww,vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv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