timer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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 timer
  14. import (
  15. "time"
  16. "bytes"
  17. "fmt"
  18. "k8s.io/kubernetes/test/e2e/framework"
  19. "k8s.io/kubernetes/test/e2e/perftype"
  20. "sync"
  21. )
  22. var now = time.Now
  23. // Phase represents a phase of a test. Phases can overlap.
  24. type Phase struct {
  25. sequenceNumber int
  26. name string
  27. startTime time.Time
  28. endTime time.Time
  29. }
  30. func (phase *Phase) ended() bool {
  31. return !phase.endTime.IsZero()
  32. }
  33. // End marks the phase as ended, unless it had already been ended before.
  34. func (phase *Phase) End() {
  35. if !phase.ended() {
  36. phase.endTime = now()
  37. }
  38. }
  39. func (phase *Phase) label() string {
  40. return fmt.Sprintf("%03d-%s", phase.sequenceNumber, phase.name)
  41. }
  42. func (phase *Phase) duration() time.Duration {
  43. endTime := phase.endTime
  44. if !phase.ended() {
  45. endTime = now()
  46. }
  47. return endTime.Sub(phase.startTime)
  48. }
  49. func (phase *Phase) humanReadable() string {
  50. if phase.ended() {
  51. return fmt.Sprintf("Phase %s: %v\n", phase.label(), phase.duration())
  52. }
  53. return fmt.Sprintf("Phase %s: %v so far\n", phase.label(), phase.duration())
  54. }
  55. // A TestPhaseTimer groups phases and provides a way to export their measurements as JSON or human-readable text.
  56. // It is safe to use concurrently.
  57. type TestPhaseTimer struct {
  58. lock sync.Mutex
  59. phases []*Phase
  60. }
  61. // NewTestPhaseTimer creates a new TestPhaseTimer.
  62. func NewTestPhaseTimer() *TestPhaseTimer {
  63. return &TestPhaseTimer{}
  64. }
  65. // StartPhase starts a new phase.
  66. // sequenceNumber is an integer prepended to phaseName in the output, such that lexicographic sorting
  67. // of phases in perfdash reconstructs the order of execution. Unfortunately it needs to be
  68. // provided manually, since a simple incrementing counter would have the effect that inserting
  69. // a new phase would renumber subsequent phases, breaking the continuity of historical records.
  70. func (timer *TestPhaseTimer) StartPhase(sequenceNumber int, phaseName string) *Phase {
  71. timer.lock.Lock()
  72. defer timer.lock.Unlock()
  73. newPhase := &Phase{sequenceNumber: sequenceNumber, name: phaseName, startTime: now()}
  74. timer.phases = append(timer.phases, newPhase)
  75. return newPhase
  76. }
  77. // SummaryKind returns the summary of test summary.
  78. func (timer *TestPhaseTimer) SummaryKind() string {
  79. return "TestPhaseTimer"
  80. }
  81. // PrintHumanReadable returns durations of all phases.
  82. func (timer *TestPhaseTimer) PrintHumanReadable() string {
  83. buf := bytes.Buffer{}
  84. timer.lock.Lock()
  85. defer timer.lock.Unlock()
  86. for _, phase := range timer.phases {
  87. buf.WriteString(phase.humanReadable())
  88. }
  89. return buf.String()
  90. }
  91. // PrintJSON returns durations of all phases with JSON format.
  92. func (timer *TestPhaseTimer) PrintJSON() string {
  93. data := perftype.PerfData{
  94. Version: "v1",
  95. DataItems: []perftype.DataItem{{
  96. Unit: "s",
  97. Labels: map[string]string{"test": "phases"},
  98. Data: make(map[string]float64)}}}
  99. timer.lock.Lock()
  100. defer timer.lock.Unlock()
  101. for _, phase := range timer.phases {
  102. data.DataItems[0].Data[phase.label()] = phase.duration().Seconds()
  103. if !phase.ended() {
  104. data.DataItems[0].Labels["ended"] = "false"
  105. }
  106. }
  107. return framework.PrettyPrintJSON(data)
  108. }