diff --git a/qrcode/Makefile b/qrcode/Makefile new file mode 100644 index 0000000..93ba68a --- /dev/null +++ b/qrcode/Makefile @@ -0,0 +1,7 @@ +build: + go test -c -o qrcode-bench.exec + +bench: + time ./qrcode-bench.exec -test.v -test.benchmem -test.bench ^Benchmark -test.count 5 -test.run ^$ > bench.txt + +run: build bench diff --git a/qrcode/bench.txt b/qrcode/bench.txt new file mode 100644 index 0000000..0217bba --- /dev/null +++ b/qrcode/bench.txt @@ -0,0 +1,34 @@ +goos: darwin +goarch: arm64 +pkg: github.com/cristalhq/benchmarks/qrcode +Benchmark_cristalhq_qrcode +Benchmark_cristalhq_qrcode-10 81516 12597 ns/op 23648 B/op 23 allocs/op +Benchmark_cristalhq_qrcode-10 94460 12476 ns/op 23648 B/op 23 allocs/op +Benchmark_cristalhq_qrcode-10 96574 12447 ns/op 23648 B/op 23 allocs/op +Benchmark_cristalhq_qrcode-10 96430 12513 ns/op 23648 B/op 23 allocs/op +Benchmark_cristalhq_qrcode-10 95835 12467 ns/op 23648 B/op 23 allocs/op +Benchmark_rsc_qr +Benchmark_rsc_qr-10 90033 13231 ns/op 25629 B/op 32 allocs/op +Benchmark_rsc_qr-10 90102 13233 ns/op 25628 B/op 32 allocs/op +Benchmark_rsc_qr-10 90963 13283 ns/op 25628 B/op 32 allocs/op +Benchmark_rsc_qr-10 90224 13259 ns/op 25628 B/op 32 allocs/op +Benchmark_rsc_qr-10 91130 13277 ns/op 25629 B/op 32 allocs/op +Benchmark_yeqown_go_qrcode +Benchmark_yeqown_go_qrcode-10 1558 763854 ns/op 2946564 B/op 3377 allocs/op +Benchmark_yeqown_go_qrcode-10 1568 771234 ns/op 2946561 B/op 3377 allocs/op +Benchmark_yeqown_go_qrcode-10 1534 776270 ns/op 2946558 B/op 3377 allocs/op +Benchmark_yeqown_go_qrcode-10 1532 771639 ns/op 2946423 B/op 3377 allocs/op +Benchmark_yeqown_go_qrcode-10 1531 771569 ns/op 2946441 B/op 3377 allocs/op +Benchmark_boombuler_barcode +Benchmark_boombuler_barcode-10 685 1726112 ns/op 129302 B/op 755 allocs/op +Benchmark_boombuler_barcode-10 696 1728505 ns/op 129313 B/op 755 allocs/op +Benchmark_boombuler_barcode-10 690 1724337 ns/op 129316 B/op 755 allocs/op +Benchmark_boombuler_barcode-10 694 1724463 ns/op 129312 B/op 755 allocs/op +Benchmark_boombuler_barcode-10 690 1728233 ns/op 129309 B/op 755 allocs/op +Benchmark_skip2_go_qrcode +Benchmark_skip2_go_qrcode-10 1945 604560 ns/op 355200 B/op 8268 allocs/op +Benchmark_skip2_go_qrcode-10 1976 602861 ns/op 355200 B/op 8268 allocs/op +Benchmark_skip2_go_qrcode-10 1981 603096 ns/op 355200 B/op 8268 allocs/op +Benchmark_skip2_go_qrcode-10 1963 602700 ns/op 355200 B/op 8268 allocs/op +Benchmark_skip2_go_qrcode-10 1971 602855 ns/op 355202 B/op 8268 allocs/op +PASS diff --git a/qrcode/bench_test.go b/qrcode/bench_test.go new file mode 100644 index 0000000..548a66f --- /dev/null +++ b/qrcode/bench_test.go @@ -0,0 +1,123 @@ +package qrcode + +import ( + "math/rand" + "testing" + + boombuler_barcode "github.com/boombuler/barcode/qr" + cristalhq_qrcode "github.com/cristalhq/qrcode" + skip2_go_qrcode "github.com/skip2/go-qrcode" + yeqown_go_qrcode "github.com/yeqown/go-qrcode" + rsc_qr "rsc.io/qr" +) + +var benchURL = "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + +func Benchmark_cristalhq_qrcode(b *testing.B) { + content := benchURL + bitmap := make([]byte, 2500) + + b.ReportAllocs() + b.ResetTimer() + + var sink int64 + + for i := 0; i < b.N; i++ { + qr, err := cristalhq_qrcode.EncodeInto(bitmap, content, cristalhq_qrcode.M) + checkErr(b, err) + + c := qr.Image().At(10, 10) + r, g, b, a := c.RGBA() + sink += int64(r + g + b + a) + } + sinkValue(sink) +} + +func Benchmark_rsc_qr(b *testing.B) { + content := benchURL + + b.ReportAllocs() + b.ResetTimer() + + var sink int64 + + for i := 0; i < b.N; i++ { + qr, err := rsc_qr.Encode(content, rsc_qr.M) + checkErr(b, err) + + c := qr.Image().At(10, 10) + r, g, b, a := c.RGBA() + sink += int64(r + g + b + a) + } + sinkValue(sink) +} + +func Benchmark_yeqown_go_qrcode(b *testing.B) { + content := benchURL + + b.ReportAllocs() + b.ResetTimer() + + var sink int64 + + for i := 0; i < b.N; i++ { + code, err := yeqown_go_qrcode.New(content) + checkErr(b, err) + + _ = code + // c := img.At(10, 10) + // r, g, b, a := c.RGBA() + // sink += int64(r + g + b + a) + } + sinkValue(sink) +} + +func Benchmark_boombuler_barcode(b *testing.B) { + content := benchURL + + b.ReportAllocs() + b.ResetTimer() + + var sink int64 + + for i := 0; i < b.N; i++ { + img, err := boombuler_barcode.Encode(content, boombuler_barcode.M, boombuler_barcode.Auto) + checkErr(b, err) + + c := img.At(10, 10) + r, g, b, a := c.RGBA() + sink += int64(r + g + b + a) + } + sinkValue(sink) +} + +func Benchmark_skip2_go_qrcode(b *testing.B) { + content := benchURL + + b.ReportAllocs() + b.ResetTimer() + + var sink int64 + + for i := 0; i < b.N; i++ { + qr, err := skip2_go_qrcode.New(content, skip2_go_qrcode.Medium) + checkErr(b, err) + + c := qr.Image(256).At(10, 10) + r, g, b, a := c.RGBA() + sink += int64(r + g + b + a) + } + sinkValue(sink) +} + +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/qrcode/go.mod b/qrcode/go.mod new file mode 100644 index 0000000..04f2d8a --- /dev/null +++ b/qrcode/go.mod @@ -0,0 +1,20 @@ +module github.com/cristalhq/benchmarks/qrcode + +go 1.17 + +require ( + github.com/boombuler/barcode v1.0.1 + github.com/cristalhq/qrcode v0.0.0-00010101000000-000000000000 + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e + github.com/yeqown/go-qrcode v1.5.10 + rsc.io/qr v0.2.0 +) + +require ( + github.com/fogleman/gg v1.3.0 // indirect + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect + github.com/yeqown/reedsolomon v1.0.0 // indirect + golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect +) + +replace github.com/cristalhq/qrcode => /Users/olegkovalov/code/hq/qrcode diff --git a/qrcode/go.sum b/qrcode/go.sum new file mode 100644 index 0000000..cf6aee3 --- /dev/null +++ b/qrcode/go.sum @@ -0,0 +1,27 @@ +github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs= +github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= +github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yeqown/go-qrcode v1.5.10 h1:87GCtypY9oOadB7yGRW4qlgAoDOop8G4JEdqOQwu1WI= +github.com/yeqown/go-qrcode v1.5.10/go.mod h1:0FVyJ3MV9fF5lfAgTr0INcy+3rupmJhjp0mL3Z9eYXk= +github.com/yeqown/reedsolomon v1.0.0 h1:x1h/Ej/uJnNu8jaX7GLHBWmZKCAWjEJTetkqaabr4B0= +github.com/yeqown/reedsolomon v1.0.0/go.mod h1:P76zpcn2TCuL0ul1Fso373qHRc69LKwAw/Iy6g1WiiM= +golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckHoFxwt8USGY1ajP1ZF5lM= +golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY= +rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=