| 1 |
# benchtest |
| 2 |
|
| 3 |
Package benchtest is used to benchmark the [github.com/charlievieth/strcase](https://pkg.go.dev/github.com/charlievieth/strcase) |
| 4 |
package against the Go standard library's [strings](https://pkg.go.dev/strings) package. |
| 5 |
|
| 6 |
The benchtest package is not part of the strcase package since some of the |
| 7 |
benchmarks here are not entirely relevant to strcase (none require case |
| 8 |
conversions). Instead they are a useful measure of the overhead / raw |
| 9 |
performance of strcase compared to the stdlib's strings package. |
| 10 |
|
| 11 |
## Usage |
| 12 |
|
| 13 |
By default benchtest runs benchmarks using the |
| 14 |
[strcase](https://pkg.go.dev/github.com/charlievieth/strcase) |
| 15 |
package. |
| 16 |
|
| 17 |
benchtest defines two flags that control how the benchmarks are run |
| 18 |
(by default the strcase is used): |
| 19 |
* `-stdlib`: use the [`strings`](https://pkg.go.dev/strings) package |
| 20 |
* `-stdlib-case`: use the [`strings`](https://pkg.go.dev/strings) package |
| 21 |
and convert text with [`strings.ToUpper`](https://pkg.go.dev/strings#ToUpper) |
| 22 |
this mimics how most Go projects currently perform case-insensitive searches |
| 23 |
(note: `ToUpper` is used because our test strings are lower-case) |
| 24 |
* e.g. `strings.Index(strings.ToUpper(s), strings.ToUpper(sep))` |
| 25 |
|
| 26 |
For convenience a [Makefile](./Makefile) is provided that has targets for: |
| 27 |
`strcase`, `stdlib`, and `stdlib-case`. The `release` make target can be used |
| 28 |
to generate a comprehensive comparison between strcase and strings (note: |
| 29 |
it is very slow). |
| 30 |
|
| 31 |
Run benchmarks using the [`strcase`](https://pkg.go.dev/github.com/charlievieth/strcase) |
| 32 |
package: |
| 33 |
|
| 34 |
```sh |
| 35 |
$ go test -bench . # or `make strcase` |
| 36 |
``` |
| 37 |
|
| 38 |
Run benchmarks using the [`strings`](https://pkg.go.dev/strings) package: |
| 39 |
|
| 40 |
```sh |
| 41 |
$ go test -bench . -stdlib # or `make stdlib` |
| 42 |
``` |
| 43 |
|
| 44 |
Run benchmarks using the [`strings`](https://pkg.go.dev/strings) package and |
| 45 |
convert the case of the strings being searched for with |
| 46 |
[`strings.ToUpper`](https://pkg.go.dev/strings#ToUpper) on each invocation: |
| 47 |
|
| 48 |
```sh |
| 49 |
$ go test -bench . -stdlib-case # or `make stdlib-case` |
| 50 |
``` |
| 51 |
|
| 52 |
Comparing benchmarks with [`benchstat`](https://pkg.go.dev/golang.org/x/[email protected]/cmd/benchstat): |
| 53 |
|
| 54 |
```sh |
| 55 |
$ # NOTE: `make release` will also perform the following commands |
| 56 |
$ go install golang.org/x/perf/cmd/benchstat@latest # install benchstat |
| 57 |
$ go test -bench . -count 5 -stdlib | tee stdlib.txt # generate stdlib bench report |
| 58 |
$ go test -bench . -count 5 | tee strcase.txt # generate strcase bench report |
| 59 |
$ benchstat stdlib.txt strcase.txt # compare strcase to stlib performance |
| 60 |
``` |
| 61 |
|
| 62 |
**Note:** A count value of 5 is usually sufficient for benchstat. The timing |
| 63 |
of these benchmarks are pretty consistent (low variance) so using a count of 1 |
| 64 |
is fine for a quick comparison, but if using a count of 1 the deprecated |
| 65 |
[benchcmp](https://pkg.go.dev/golang.org/x/[email protected]/cmd/benchcmp) command |
| 66 |
should be used since benchstat will complain that it needs more runs to |
| 67 |
generate a statistically significant report. |
| 68 |
|
| 69 |
Additionally, these benchmarks can take a long time to run so the `-timeout` |
| 70 |
flag may be needed (`-timeout=30m` is usually sufficient) to prevent `go test` |
| 71 |
from timing out (the Makefile sets this by default). |
| 72 |
|
| 73 |
## Generating a benchmark report |
| 74 |
|
| 75 |
Use the make `release` target to generate a comprehensive comparison between |
| 76 |
`strcase` and `strings` (note: it is very slow). It should be used when cutting |
| 77 |
new releases. |