Commit 2a4fb537 authored by Igor Drozdov's avatar Igor Drozdov

Improve test report for performance_test.go

parent cb7f2d58
package parser package parser
import ( import (
"fmt"
"os" "os"
"runtime" "runtime"
"testing" "testing"
...@@ -14,23 +13,22 @@ func BenchmarkGenerate(b *testing.B) { ...@@ -14,23 +13,22 @@ func BenchmarkGenerate(b *testing.B) {
tmpDir := filePath + ".tmp" tmpDir := filePath + ".tmp"
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
m := measureMemory(func() { var memoryUsage float64
file, err := os.Open(filePath) for i := 0; i < b.N; i++ {
require.NoError(b, err) memoryUsage += measureMemory(func() {
file, err := os.Open(filePath)
require.NoError(b, err)
p, err := NewParser(file, Config{ProcessReferences: true}) p, err := NewParser(file, Config{ProcessReferences: true})
require.NoError(b, err) require.NoError(b, err)
_, err = p.ZipReader() _, err = p.ZipReader()
require.NoError(b, err) require.NoError(b, err)
require.NoError(b, p.Close()) require.NoError(b, p.Close())
}) })
}
// Golang 1.13 has `func (*B) ReportMetric` b.ReportMetric(memoryUsage/float64(b.N), "MiB/op")
// It makes sense to replace fmt.Printf with
// b.ReportMetric(m, "MiB/op")
fmt.Printf("BenchmarkGenerate: %f MiB/op\n", m)
} }
func measureMemory(f func()) float64 { func measureMemory(f func()) float64 {
...@@ -40,6 +38,7 @@ func measureMemory(f func()) float64 { ...@@ -40,6 +38,7 @@ func measureMemory(f func()) float64 {
f() f()
runtime.ReadMemStats(&m1) runtime.ReadMemStats(&m1)
runtime.GC()
return float64(m1.Alloc-m.Alloc) / 1024 / 1024 return float64(m1.Alloc-m.Alloc) / 1024 / 1024
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment