github.com/fleacloud/csv@v0.0.0-20190612094955-fd8b9338afcf/bench_test.go (about) 1 package csv 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "io/ioutil" 7 "os" 8 "testing" 9 ) 10 11 // Benchmark with large CSV data set 12 13 func loadData() []byte { 14 f, err := os.Open("testdata/ercot-dam.csv.gz") 15 if err != nil { 16 panic(err) 17 } 18 defer f.Close() 19 20 gz, err := gzip.NewReader(f) 21 if err != nil { 22 panic(err) 23 } 24 25 data, err := ioutil.ReadAll(gz) 26 if err != nil { 27 panic(err) 28 } 29 30 return data 31 } 32 33 type Price struct { 34 DeliveryDate string 35 HourEnding string 36 BusName string 37 LMP float32 38 DSTFlag bool `true:"Y" false:"N"` 39 } 40 41 func BenchmarkUnmarshal(b *testing.B) { 42 b.StopTimer() 43 data := loadData() 44 b.StartTimer() 45 46 pp := []Price{} 47 48 err := Unmarshal(data, &pp) 49 if err != nil { 50 panic(err) 51 } 52 53 out, err := Marshal(pp) 54 if err != nil { 55 panic(err) 56 } 57 58 if bytes.Equal(data, out) != true { 59 panic("wrong results") 60 } 61 62 b.Logf("Unmarshal %d rows", len(pp)) 63 b.Logf("Marshaled %d bytes", len(out)) 64 }