Skip to content

Commit

Permalink
Benchmark across Parse options, add memory benchmarking. (#264)
Browse files Browse the repository at this point in the history
This change updates the Parse benchmark to operate across useful ParseOptions, adds memory benchmarking, and updates some benchmark params. 

For example, we can take a look at the CPU / memory differences across the new ParseOptions for one of the test dicoms:
```
BenchmarkParse/NoOptions/4.dcm-4                                     	      10	 106876945 ns/op	120572543 B/op	    2160 allocs/op
BenchmarkParse/SkipPixelData/4.dcm-4                                 	      10	    531193 ns/op	   34900 B/op	    2144 allocs/op
BenchmarkParse/SkipProcessingPixelDataValue/4.dcm-4                  	      10	   1807256 ns/op	 7573518 B/op	    2146 allocs/op
```
  • Loading branch information
suyashkumar authored Feb 26, 2023
1 parent cb27a34 commit 75975a5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/bench_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ jobs:
go install golang.org/x/perf/cmd/benchstat@latest
echo "New Commit:"
git log -1 --format="%H"
go test -bench=. -benchtime=.75s -count=8 > $HOME/new.txt
go test -bench=. -benchmem -benchtime=10x -count=7 > $HOME/new.txt
git reset --hard HEAD
git checkout $GITHUB_BASE_REF
echo "Base Commit:"
git log -1 --format="%H"
go test -bench=. -benchtime=.75s -count=8 > $HOME/old.txt
go test -bench=. -benchmem -benchtime=10x -count=7 > $HOME/old.txt
$GOBIN/benchstat $HOME/old.txt $HOME/new.txt
4 changes: 2 additions & 2 deletions .github/workflows/bench_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
go install golang.org/x/perf/cmd/benchstat@latest
echo "New Commit:"
git log -1 --format="%H"
go test -bench=. -benchtime=.75s -count=8 > $HOME/new.txt
go test -bench=. -benchmem -benchtime=10x -count=7 > $HOME/new.txt
git reset --hard HEAD
git checkout HEAD~1
echo "Base Commit:"
git log -1 --format="%H"
go test -bench=. -benchtime=.75s -count=8 > $HOME/old.txt
go test -bench=. -benchmem -benchtime=10x -count=7 > $HOME/old.txt
$GOBIN/benchstat $HOME/old.txt $HOME/new.txt
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ release:
tar -zcvf ${BINARY}-darwin-amd64.tar.gz ${BINARY}-darwin-amd64; \
zip -r ${BINARY}-windows-amd64.exe.zip ${BINARY}-windows-amd64.exe;

.PHONY: bench
bench:
go test -bench . -benchmem -benchtime=10x

bench-diff:
go test -bench . -benchtime=50x -count 5 > bench_current.txt
go test -bench . -benchmem -benchtime=10x -count 5 > bench_current.txt
git checkout main
go test -bench . -benchtime=50x -count 5 > bench_main.txt
go test -bench . -benchmem -benchtime=10x -count 5 > bench_main.txt
benchstat bench_main.txt bench_current.txt
git checkout -
63 changes: 43 additions & 20 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,30 +153,53 @@ func TestParseFile_SkipProcessingPixelDataValue(t *testing.T) {

// BenchmarkParse runs sanity benchmarks over the sample files in testdata.
func BenchmarkParse(b *testing.B) {
files, err := ioutil.ReadDir("./testdata")
if err != nil {
b.Fatalf("unable to read testdata/: %v", err)
cases := []struct {
name string
opts []dicom.ParseOption
}{
{
name: "NoOptions",
},
{
name: "SkipPixelData",
opts: []dicom.ParseOption{dicom.SkipPixelData()},
},
{
name: "SkipProcessingPixelDataValue",
opts: []dicom.ParseOption{dicom.SkipProcessingPixelDataValue()},
},
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
b.Run(f.Name(), func(b *testing.B) {
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
b.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
files, err := ioutil.ReadDir("./testdata")
if err != nil {
b.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
b.Run(f.Name(), func(b *testing.B) {

data, err := ioutil.ReadAll(dcm)
if err != nil {
b.Errorf("Unable to read file into memory for benchmark: %v", err)
}
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
b.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()

data, err := ioutil.ReadAll(dcm)
if err != nil {
b.Errorf("Unable to read file into memory for benchmark: %v", err)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, _ = dicom.Parse(bytes.NewBuffer(data), int64(len(data)), nil, tc.opts...)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = dicom.Parse(bytes.NewBuffer(data), int64(len(data)), nil)
})
}
})
}
}
})
}
}

Expand Down

0 comments on commit 75975a5

Please sign in to comment.