image_test.go 775 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2011 Dmitry Chestnykh. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package captcha
  5. import "testing"
  6. type byteCounter struct {
  7. n int64
  8. }
  9. func (bc *byteCounter) Write(b []byte) (int, error) {
  10. bc.n += int64(len(b))
  11. return len(b), nil
  12. }
  13. func BenchmarkNewImage(b *testing.B) {
  14. b.StopTimer()
  15. d := RandomDigits(DefaultLen)
  16. b.StartTimer()
  17. for i := 0; i < b.N; i++ {
  18. NewImage(d, StdWidth, StdHeight)
  19. }
  20. }
  21. func BenchmarkImageWriteTo(b *testing.B) {
  22. b.StopTimer()
  23. d := RandomDigits(DefaultLen)
  24. b.StartTimer()
  25. counter := &byteCounter{}
  26. for i := 0; i < b.N; i++ {
  27. img := NewImage(d, StdWidth, StdHeight)
  28. img.WriteTo(counter)
  29. b.SetBytes(counter.n)
  30. counter.n = 0
  31. }
  32. }