generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
579 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package benchmark provides utilities for running benchmarks. | ||
package benchmark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package benchmark | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// Run benchmarks fn. | ||
func Run( | ||
b *testing.B, | ||
setup func(context.Context) error, | ||
before func(context.Context) error, | ||
fn func(context.Context) error, | ||
after func(context.Context) error, | ||
) { | ||
b.StopTimer() | ||
checkIterationThreshold(b) | ||
|
||
if setup != nil { | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
|
||
err := setup(ctx) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
|
||
for i := 0; i < b.N; i++ { | ||
if before != nil { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
err := before(ctx) | ||
cancel() | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
|
||
b.StartTimer() | ||
err := fn(ctx) | ||
b.StopTimer() | ||
|
||
cancel() | ||
|
||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
if after != nil { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
err := after(ctx) | ||
cancel() | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// checkIterationThreshold skips the benchmark if the number of iterations is | ||
// too high. This usually occurs when the benchmarking framework is unable to | ||
// measure the duration of each iteration, typically because the benchmarked | ||
// code is "too fast". | ||
func checkIterationThreshold(b *testing.B) { | ||
const threshold = 1_000_000 | ||
if b.N >= threshold { | ||
b.Skipf("benchmark skipped, too many iterations (%d); benchmarked code is likely too fast to measure meaningfully", b.N) | ||
} | ||
} |
Oops, something went wrong.