image_test.go 877 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2014 The Gogs Authors. 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. "testing"
  7. "github.com/astaxie/beego/utils"
  8. )
  9. type byteCounter struct {
  10. n int64
  11. }
  12. func (bc *byteCounter) Write(b []byte) (int, error) {
  13. bc.n += int64(len(b))
  14. return len(b), nil
  15. }
  16. func BenchmarkNewImage(b *testing.B) {
  17. b.StopTimer()
  18. d := utils.RandomCreateBytes(challengeNums, defaultChars...)
  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 := utils.RandomCreateBytes(challengeNums, defaultChars...)
  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. }