github.com/midokura/kubeedge@v1.2.0-mido.0/tests/e2e/utils/timer.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package utils
    18  
    19  import (
    20  	"sync"
    21  	"time"
    22  )
    23  
    24  // TestTimer represents a test timer
    25  type TestTimer struct {
    26  	Name      string
    27  	StartTime time.Time
    28  	EndTime   time.Time
    29  }
    30  
    31  // End is used to end the test timer
    32  func (testTimer *TestTimer) End() {
    33  	if !testTimer.IsEnded() {
    34  		testTimer.EndTime = time.Now()
    35  	}
    36  }
    37  
    38  // IsEnded represents if the test timer is ended
    39  func (testTimer *TestTimer) IsEnded() bool {
    40  	return !testTimer.EndTime.IsZero()
    41  }
    42  
    43  // Duration is used to calculate the duration
    44  func (testTimer *TestTimer) Duration() time.Duration {
    45  	endTime := testTimer.EndTime
    46  	if !testTimer.IsEnded() {
    47  		endTime = time.Now()
    48  	}
    49  	return endTime.Sub(testTimer.StartTime)
    50  }
    51  
    52  // PrintResult prints the result of the test timer
    53  func (testTimer *TestTimer) PrintResult() {
    54  	if testTimer.IsEnded() {
    55  		Infof("Test case name: %s start time: %v duration: %v\n",
    56  			testTimer.Name, testTimer.StartTime, testTimer.Duration())
    57  	} else {
    58  		Infof("Test case name: %s start time: %v duration: %v so far\n",
    59  			testTimer.Name, testTimer.StartTime, testTimer.Duration())
    60  	}
    61  }
    62  
    63  // TestTimerGroup includes one or more test timers
    64  type TestTimerGroup struct {
    65  	mutex      sync.Mutex
    66  	testTimers []*TestTimer
    67  }
    68  
    69  // NewTestTimerGroup creates a new test timer group
    70  func NewTestTimerGroup() *TestTimerGroup {
    71  	return &TestTimerGroup{}
    72  }
    73  
    74  // NewTestTimer creates a new test timer
    75  func (group *TestTimerGroup) NewTestTimer(name string) *TestTimer {
    76  	group.mutex.Lock()
    77  	defer group.mutex.Unlock()
    78  	testTimer := &TestTimer{Name: name, StartTime: time.Now()}
    79  	group.testTimers = append(group.testTimers, testTimer)
    80  	return testTimer
    81  }
    82  
    83  // GetTestTimers returns test timers
    84  func (group *TestTimerGroup) GetTestTimers() []*TestTimer {
    85  	return group.testTimers
    86  }
    87  
    88  // PrintResult prints the results of all test timers.
    89  func (group *TestTimerGroup) PrintResult() {
    90  	group.mutex.Lock()
    91  	defer group.mutex.Unlock()
    92  	for _, testTimer := range group.testTimers {
    93  		testTimer.PrintResult()
    94  	}
    95  }