image_test.go 789 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 (
  6. "os"
  7. "testing"
  8. )
  9. type byteCounter struct {
  10. n int64
  11. }
  12. func (bc *byteCounter) Write(b []byte) (int, os.Error) {
  13. bc.n += int64(len(b))
  14. return len(b), nil
  15. }
  16. func BenchmarkNewImage(b *testing.B) {
  17. b.StopTimer()
  18. d := RandomDigits(DefaultLen)
  19. b.StartTimer()
  20. for i := 0; i < b.N; i++ {
  21. NewImage(d, StdWidth, StdHeight)
  22. }
  23. }
  24. func BenchmarkImageWriteTo(b *testing.B) {
  25. b.StopTimer()
  26. d := RandomDigits(DefaultLen)
  27. b.StartTimer()
  28. counter := &byteCounter{}
  29. for i := 0; i < b.N; i++ {
  30. img := NewImage(d, StdWidth, StdHeight)
  31. img.WriteTo(counter)
  32. b.SetBytes(counter.n)
  33. counter.n = 0
  34. }
  35. }