image_test.go 819 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. id := randomId()
  17. b.StartTimer()
  18. for i := 0; i < b.N; i++ {
  19. NewImage(id, d, StdWidth, StdHeight)
  20. }
  21. }
  22. func BenchmarkImageWriteTo(b *testing.B) {
  23. b.StopTimer()
  24. d := RandomDigits(DefaultLen)
  25. id := randomId()
  26. b.StartTimer()
  27. counter := &byteCounter{}
  28. for i := 0; i < b.N; i++ {
  29. img := NewImage(id, d, StdWidth, StdHeight)
  30. img.WriteTo(counter)
  31. b.SetBytes(counter.n)
  32. counter.n = 0
  33. }
  34. }