image_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package captcha
  15. import (
  16. "testing"
  17. "github.com/astaxie/beego/utils"
  18. )
  19. type byteCounter struct {
  20. n int64
  21. }
  22. func (bc *byteCounter) Write(b []byte) (int, error) {
  23. bc.n += int64(len(b))
  24. return len(b), nil
  25. }
  26. func BenchmarkNewImage(b *testing.B) {
  27. b.StopTimer()
  28. d := utils.RandomCreateBytes(challengeNums, defaultChars...)
  29. b.StartTimer()
  30. for i := 0; i < b.N; i++ {
  31. NewImage(d, stdWidth, stdHeight)
  32. }
  33. }
  34. func BenchmarkImageWriteTo(b *testing.B) {
  35. b.StopTimer()
  36. d := utils.RandomCreateBytes(challengeNums, defaultChars...)
  37. b.StartTimer()
  38. counter := &byteCounter{}
  39. for i := 0; i < b.N; i++ {
  40. img := NewImage(d, stdWidth, stdHeight)
  41. img.WriteTo(counter)
  42. b.SetBytes(counter.n)
  43. counter.n = 0
  44. }
  45. }