report_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }