diff --git a/base64/Makefile b/base64/Makefile new file mode 100644 index 0000000..2ba852e --- /dev/null +++ b/base64/Makefile @@ -0,0 +1,7 @@ +build: + go test -c -o base64-bench.exec + +bench: + time ./base64-bench.exec -test.v -test.benchmem -test.bench ^Benchmark -test.count 5 -test.run ^$ > bench.txt + +run: build bench diff --git a/base64/README.md b/base64/README.md new file mode 100644 index 0000000..cfc5d5e --- /dev/null +++ b/base64/README.md @@ -0,0 +1,35 @@ +# base64 + +## Who + +``` +github.com/cristalhq/base64 v0.1.2 +github.com/segmentio/asm v1.1.4 +``` + +## Where + +``` +MacBook Pro (16-inch, 2019) +2,6 GHz 6-Core Intel Core i7 +16 GB 2667 MHz DDR4 +``` + +## How + +```shell script +# build & run benchmark +$ make run + +# or in steps + +# build test executable +$ go test -c -o base64-bench.exec + +# run executable +$ time ./base64-bench.exec -v -benchmem -bench ^Benchmark -count 5 -run ^$ > bench.txt +``` + +## Results + +See [bench.txt](https://github.com/cristalhq/benchmarks/blob/main/base64/bench.txt) diff --git a/base64/bench.txt b/base64/bench.txt new file mode 100644 index 0000000..ec137f5 --- /dev/null +++ b/base64/bench.txt @@ -0,0 +1,22 @@ +goos: darwin +goarch: arm64 +pkg: github.com/cristaloleg/benches/base64 +Benchmark_cristalhq +Benchmark_cristalhq-10 586058 2063 ns/op 3970.22 MB/s 12288 B/op 1 allocs/op +Benchmark_cristalhq-10 591297 2212 ns/op 3703.11 MB/s 12288 B/op 1 allocs/op +Benchmark_cristalhq-10 534670 2254 ns/op 3633.67 MB/s 12288 B/op 1 allocs/op +Benchmark_cristalhq-10 544071 2248 ns/op 3644.62 MB/s 12288 B/op 1 allocs/op +Benchmark_cristalhq-10 534746 2093 ns/op 3913.14 MB/s 12288 B/op 1 allocs/op +Benchmark_golang +Benchmark_golang-10 203595 5799 ns/op 1412.60 MB/s 24576 B/op 2 allocs/op +Benchmark_golang-10 206269 5834 ns/op 1404.17 MB/s 24576 B/op 2 allocs/op +Benchmark_golang-10 205230 5795 ns/op 1413.72 MB/s 24576 B/op 2 allocs/op +Benchmark_golang-10 207208 5828 ns/op 1405.53 MB/s 24576 B/op 2 allocs/op +Benchmark_golang-10 204903 5803 ns/op 1411.58 MB/s 24576 B/op 2 allocs/op +Benchmark_segmentio +Benchmark_segmentio-10 590043 1958 ns/op 4184.40 MB/s 24576 B/op 2 allocs/op +Benchmark_segmentio-10 617035 1956 ns/op 4187.37 MB/s 24576 B/op 2 allocs/op +Benchmark_segmentio-10 610040 1945 ns/op 4211.46 MB/s 24576 B/op 2 allocs/op +Benchmark_segmentio-10 607477 1950 ns/op 4201.10 MB/s 24576 B/op 2 allocs/op +Benchmark_segmentio-10 618952 1972 ns/op 4153.74 MB/s 24576 B/op 2 allocs/op +PASS diff --git a/base64/bench_test.go b/base64/bench_test.go new file mode 100644 index 0000000..69c8a69 --- /dev/null +++ b/base64/bench_test.go @@ -0,0 +1,69 @@ +package base64_test + +import ( + "math/rand" + "testing" + + golang "encoding/base64" + + cristalhq "github.com/cristalhq/base64" + segmentio "github.com/segmentio/asm/base64" +) + +func Benchmark_cristalhq(b *testing.B) { + data := make([]byte, 8192) + b.SetBytes(int64(len(data))) + b.ReportAllocs() + b.ResetTimer() + + var v int64 + for i := 0; i < b.N; i++ { + res := cristalhq.StdEncoding.EncodeToString(data) + v += int64(len(res)) + } + + sinkValue(v) +} + +func Benchmark_golang(b *testing.B) { + data := make([]byte, 8192) + b.SetBytes(int64(len(data))) + b.ReportAllocs() + b.ResetTimer() + + var v int64 + for i := 0; i < b.N; i++ { + res := golang.StdEncoding.EncodeToString(data) + v += int64(len(res)) + } + + sinkValue(v) +} + +func Benchmark_segmentio(b *testing.B) { + data := make([]byte, 8192) + b.SetBytes(int64(len(data))) + b.ReportAllocs() + b.ResetTimer() + + var v int64 + + for i := 0; i < b.N; i++ { + res := segmentio.StdEncoding.EncodeToString(data) + v += int64(len(res)) + } + + sinkValue(v) +} + +func checkErr(tb testing.TB, err error) { + if err != nil { + tb.Fatal(err) + } +} + +func sinkValue(v int64) { + if rand.Float64() > 2 { + panic(v) + } +} diff --git a/base64/go.mod b/base64/go.mod new file mode 100644 index 0000000..464705c --- /dev/null +++ b/base64/go.mod @@ -0,0 +1,10 @@ +module github.com/cristaloleg/benches/base64 + +go 1.17 + +require ( + github.com/cristalhq/base64 v0.1.2 + github.com/segmentio/asm v1.1.4 +) + +require golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 // indirect diff --git a/base64/go.sum b/base64/go.sum new file mode 100644 index 0000000..dfe6bfe --- /dev/null +++ b/base64/go.sum @@ -0,0 +1,6 @@ +github.com/cristalhq/base64 v0.1.2 h1:edsefYyYDiac7Ytdh2xdaiiSSJzcI2f0yIkdGEf1qY0= +github.com/cristalhq/base64 v0.1.2/go.mod h1:sy4+2Hale2KbtSqkzpdMeYTP/IrB+HCvxVHWsh2VSYk= +github.com/segmentio/asm v1.1.4 h1:Q/FKBtrgnmDc0YMrurLROqG9mXE6Ndn276EtDnoWtMM= +github.com/segmentio/asm v1.1.4/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg= +golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 h1:WecRHqgE09JBkh/584XIE6PMz5KKE/vER4izNUi30AQ= +golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=