Add performance test phase timing export.

This commit is contained in:
Marcin Owsiany
2017-11-02 12:48:02 +01:00
parent 115bd09ff9
commit 96089e0b79
8 changed files with 372 additions and 41 deletions

View File

@@ -0,0 +1,92 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package timer
import (
"testing"
"time"
. "github.com/onsi/gomega"
)
var currentTime time.Time
func init() {
setCurrentTimeSinceEpoch(0)
now = func() time.Time { return currentTime }
}
func setCurrentTimeSinceEpoch(duration time.Duration) {
currentTime = time.Unix(0, duration.Nanoseconds())
}
func testUsageWithDefer(timer *TestPhaseTimer) {
defer timer.StartPhase(33, "two").End()
setCurrentTimeSinceEpoch(6*time.Second + 500*time.Millisecond)
}
func TestTimer(t *testing.T) {
RegisterTestingT(t)
timer := NewTestPhaseTimer()
setCurrentTimeSinceEpoch(1 * time.Second)
phaseOne := timer.StartPhase(1, "one")
setCurrentTimeSinceEpoch(3 * time.Second)
testUsageWithDefer(timer)
Expect(timer.PrintJSON()).To(MatchJSON(`{
"version": "v1",
"dataItems": [
{
"data": {
"001-one": 5.5,
"033-two": 3.5
},
"unit": "s",
"labels": {
"test": "phases",
"ended": "false"
}
}
]
}`))
Expect(timer.PrintHumanReadable()).To(Equal(`Phase 001-one: 5.5s so far
Phase 033-two: 3.5s
`))
setCurrentTimeSinceEpoch(7*time.Second + 500*time.Millisecond)
phaseOne.End()
Expect(timer.PrintJSON()).To(MatchJSON(`{
"version": "v1",
"dataItems": [
{
"data": {
"001-one": 6.5,
"033-two": 3.5
},
"unit": "s",
"labels": {
"test": "phases"
}
}
]
}`))
Expect(timer.PrintHumanReadable()).To(Equal(`Phase 001-one: 6.5s
Phase 033-two: 3.5s
`))
}