From fb08c2374ebbd7fed2e931fadb441033ff12526e Mon Sep 17 00:00:00 2001 From: Vaibhav Kamra Date: Fri, 31 Mar 2023 10:29:40 -0700 Subject: [PATCH] Add benchmark tests --- src/cmd/jsondebug/arrayread/read.go | 4 ++++ src/cmd/jsondebug/arrayread/read_test.go | 9 +++++++++ src/cmd/jsondebug/goread/read.go | 4 ++++ src/cmd/jsondebug/goread/read_test.go | 9 +++++++++ src/cmd/jsondebug/jsonparser/read.go | 15 ++++++++++++++- src/cmd/jsondebug/jsonparser/read_test.go | 14 ++++++++++++++ 6 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/cmd/jsondebug/arrayread/read_test.go create mode 100644 src/cmd/jsondebug/goread/read_test.go create mode 100644 src/cmd/jsondebug/jsonparser/read_test.go diff --git a/src/cmd/jsondebug/arrayread/read.go b/src/cmd/jsondebug/arrayread/read.go index 5998b1e1d..cbfc92eb3 100644 --- a/src/cmd/jsondebug/arrayread/read.go +++ b/src/cmd/jsondebug/arrayread/read.go @@ -9,6 +9,10 @@ import ( ) func main() { + readData() +} + +func readData() { f, err := os.Open(common.FileName) if err != nil { fmt.Printf("Error opening input file: %v\n", err) diff --git a/src/cmd/jsondebug/arrayread/read_test.go b/src/cmd/jsondebug/arrayread/read_test.go new file mode 100644 index 000000000..5e9756d84 --- /dev/null +++ b/src/cmd/jsondebug/arrayread/read_test.go @@ -0,0 +1,9 @@ +package main + +import "testing" + +func Benchmark_readData(b *testing.B) { + for i := 0; i < b.N; i++ { + readData() + } +} diff --git a/src/cmd/jsondebug/goread/read.go b/src/cmd/jsondebug/goread/read.go index 4accfe539..4f28925df 100644 --- a/src/cmd/jsondebug/goread/read.go +++ b/src/cmd/jsondebug/goread/read.go @@ -9,6 +9,10 @@ import ( ) func main() { + readData() +} + +func readData() { f, err := os.Open(common.FileName) if err != nil { fmt.Printf("Error opening input file: %v\n", err) diff --git a/src/cmd/jsondebug/goread/read_test.go b/src/cmd/jsondebug/goread/read_test.go new file mode 100644 index 000000000..5e9756d84 --- /dev/null +++ b/src/cmd/jsondebug/goread/read_test.go @@ -0,0 +1,9 @@ +package main + +import "testing" + +func Benchmark_readData(b *testing.B) { + for i := 0; i < b.N; i++ { + readData() + } +} diff --git a/src/cmd/jsondebug/jsonparser/read.go b/src/cmd/jsondebug/jsonparser/read.go index 58528a318..8c8364c36 100644 --- a/src/cmd/jsondebug/jsonparser/read.go +++ b/src/cmd/jsondebug/jsonparser/read.go @@ -29,13 +29,26 @@ func main() { } }() + d, err := readFile() + if err != nil { + return + } + + parseData(d) +} + +func readFile() ([]byte, error) { common.PrintMemUsage() data, err := ioutil.ReadFile(common.FileName) if err != nil { fmt.Printf("Error reading file: %v\n", err) - return + return nil, err } + return data, nil +} + +func parseData(data []byte) { common.PrintMemUsage() diff --git a/src/cmd/jsondebug/jsonparser/read_test.go b/src/cmd/jsondebug/jsonparser/read_test.go new file mode 100644 index 000000000..1711fa36e --- /dev/null +++ b/src/cmd/jsondebug/jsonparser/read_test.go @@ -0,0 +1,14 @@ +package main + +import "testing" + +func Benchmark_parseData(b *testing.B) { + d, err := readFile() + if err != nil { + return + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + parseData(d) + } +}