regen_benchmarks_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2014 Zachary Klippenstein
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package regen
  14. import (
  15. "math/rand"
  16. "testing"
  17. )
  18. const BigFancyRegexp = `
  19. POST (/[-a-zA-Z0-9_.]{3,12}){3,6}
  20. Content-Length: [0-9]{2,3}
  21. X-Auth-Token: [a-zA-Z0-9+/]{64}
  22. ([A-Za-z0-9+/]{64}
  23. ){3,15}[A-Za-z0-9+/]{60}([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)
  24. `
  25. var rngSource = rand.NewSource(42)
  26. // Benchmarks the code that creates generators.
  27. // Doesn't actually run the generators.
  28. func BenchmarkComplexCreation(b *testing.B) {
  29. // Create everything here to save allocations in the loop.
  30. //args := &GeneratorArgs{rngSource, 0, NewSerialExecutor()}
  31. args := &GeneratorArgs{
  32. RngSource: rngSource,
  33. Flags: 0,
  34. }
  35. for i := 0; i < b.N; i++ {
  36. NewGenerator(BigFancyRegexp, args)
  37. }
  38. }
  39. func BenchmarkLargeRepeatCreateSerial(b *testing.B) {
  40. for i := 0; i < b.N; i++ {
  41. NewGenerator(`a{999}`, &GeneratorArgs{
  42. RngSource: rand.NewSource(0),
  43. })
  44. }
  45. }
  46. func BenchmarkComplexGeneration(b *testing.B) {
  47. args := &GeneratorArgs{
  48. RngSource: rngSource,
  49. }
  50. generator, err := NewGenerator(BigFancyRegexp, args)
  51. if err != nil {
  52. panic(err)
  53. }
  54. b.ResetTimer()
  55. for i := 0; i < b.N; i++ {
  56. generator.Generate()
  57. }
  58. }
  59. func BenchmarkLargeRepeatGenerateSerial(b *testing.B) {
  60. generator, err := NewGenerator(`a{999}`, &GeneratorArgs{
  61. RngSource: rand.NewSource(0),
  62. })
  63. if err != nil {
  64. b.Fatal(err)
  65. }
  66. b.ResetTimer()
  67. for i := 0; i < b.N; i++ {
  68. generator.Generate()
  69. }
  70. }