bench_iteration_test.go 959 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package goquery
  2. import (
  3. "testing"
  4. )
  5. func BenchmarkEach(b *testing.B) {
  6. var tmp, n int
  7. b.StopTimer()
  8. sel := DocW().Find("td")
  9. f := func(i int, s *Selection) {
  10. tmp++
  11. }
  12. b.StartTimer()
  13. for i := 0; i < b.N; i++ {
  14. sel.Each(f)
  15. if n == 0 {
  16. n = tmp
  17. }
  18. }
  19. if n != 59 {
  20. b.Fatalf("want 59, got %d", n)
  21. }
  22. }
  23. func BenchmarkMap(b *testing.B) {
  24. var tmp, n int
  25. b.StopTimer()
  26. sel := DocW().Find("td")
  27. f := func(i int, s *Selection) string {
  28. tmp++
  29. return string(tmp)
  30. }
  31. b.StartTimer()
  32. for i := 0; i < b.N; i++ {
  33. sel.Map(f)
  34. if n == 0 {
  35. n = tmp
  36. }
  37. }
  38. if n != 59 {
  39. b.Fatalf("want 59, got %d", n)
  40. }
  41. }
  42. func BenchmarkEachWithBreak(b *testing.B) {
  43. var tmp, n int
  44. b.StopTimer()
  45. sel := DocW().Find("td")
  46. f := func(i int, s *Selection) bool {
  47. tmp++
  48. return tmp < 10
  49. }
  50. b.StartTimer()
  51. for i := 0; i < b.N; i++ {
  52. tmp = 0
  53. sel.EachWithBreak(f)
  54. if n == 0 {
  55. n = tmp
  56. }
  57. }
  58. if n != 10 {
  59. b.Fatalf("want 10, got %d", n)
  60. }
  61. }