report_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2017 The etcd Authors
  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 report
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "time"
  21. )
  22. func TestPercentiles(t *testing.T) {
  23. nums := make([]float64, 100)
  24. nums[99] = 1 // 99-percentile (1 out of 100)
  25. data := percentiles(nums)
  26. if data[len(pctls)-2] != 1 {
  27. t.Fatalf("99-percentile expected 1, got %f", data[len(pctls)-2])
  28. }
  29. nums = make([]float64, 1000)
  30. nums[999] = 1 // 99.9-percentile (1 out of 1000)
  31. data = percentiles(nums)
  32. if data[len(pctls)-1] != 1 {
  33. t.Fatalf("99.9-percentile expected 1, got %f", data[len(pctls)-1])
  34. }
  35. }
  36. func TestReport(t *testing.T) {
  37. r := NewReportSample("%f")
  38. go func() {
  39. start := time.Now()
  40. for i := 0; i < 5; i++ {
  41. end := start.Add(time.Second)
  42. r.Results() <- Result{Start: start, End: end}
  43. start = end
  44. }
  45. r.Results() <- Result{Start: start, End: start.Add(time.Second), Err: fmt.Errorf("oops")}
  46. close(r.Results())
  47. }()
  48. stats := <-r.Stats()
  49. stats.TimeSeries = nil // ignore timeseries since it uses wall clock
  50. wStats := Stats{
  51. AvgTotal: 5.0,
  52. Fastest: 1.0,
  53. Slowest: 1.0,
  54. Average: 1.0,
  55. Stddev: 0.0,
  56. Total: stats.Total,
  57. RPS: 5.0 / stats.Total.Seconds(),
  58. ErrorDist: map[string]int{"oops": 1},
  59. Lats: []float64{1.0, 1.0, 1.0, 1.0, 1.0},
  60. }
  61. if !reflect.DeepEqual(stats, wStats) {
  62. t.Fatalf("got %+v, want %+v", stats, wStats)
  63. }
  64. wstrs := []string{
  65. "Stddev:\t0",
  66. "Average:\t1.0",
  67. "Slowest:\t1.0",
  68. "Fastest:\t1.0",
  69. }
  70. ss := <-r.Run()
  71. for i, ws := range wstrs {
  72. if !strings.Contains(ss, ws) {
  73. t.Errorf("#%d: stats string missing %s", i, ws)
  74. }
  75. }
  76. }
  77. func TestWeightedReport(t *testing.T) {
  78. r := NewWeightedReport(NewReport("%f"), "%f")
  79. go func() {
  80. start := time.Now()
  81. for i := 0; i < 5; i++ {
  82. end := start.Add(time.Second)
  83. r.Results() <- Result{Start: start, End: end, Weight: 2.0}
  84. start = end
  85. }
  86. r.Results() <- Result{Start: start, End: start.Add(time.Second), Err: fmt.Errorf("oops")}
  87. close(r.Results())
  88. }()
  89. stats := <-r.Stats()
  90. stats.TimeSeries = nil // ignore timeseries since it uses wall clock
  91. wStats := Stats{
  92. AvgTotal: 10.0,
  93. Fastest: 0.5,
  94. Slowest: 0.5,
  95. Average: 0.5,
  96. Stddev: 0.0,
  97. Total: stats.Total,
  98. RPS: 10.0 / stats.Total.Seconds(),
  99. ErrorDist: map[string]int{"oops": 1},
  100. Lats: []float64{0.5, 0.5, 0.5, 0.5, 0.5},
  101. }
  102. if !reflect.DeepEqual(stats, wStats) {
  103. t.Fatalf("got %+v, want %+v", stats, wStats)
  104. }
  105. }