...

Source file src/github.com/emissary-ingress/emissary/v3/pkg/debug/timer_test.go

Documentation: github.com/emissary-ingress/emissary/v3/pkg/debug

     1  package debug_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/emissary-ingress/emissary/v3/pkg/debug"
    10  )
    11  
    12  func TestTimer(t *testing.T) {
    13  	clock := time.Now()
    14  	timer := debug.NewTimerWithClock(func() time.Time {
    15  		return clock
    16  	})
    17  
    18  	timer.Time(func() {
    19  		clock = clock.Add(250 * time.Millisecond)
    20  	})
    21  	timer.Time(func() {
    22  		clock = clock.Add(250 * time.Millisecond)
    23  	})
    24  	timer.Time(func() {
    25  		clock = clock.Add(500 * time.Millisecond)
    26  	})
    27  	timer.Time(func() {
    28  		clock = clock.Add(500 * time.Millisecond)
    29  	})
    30  	timer.Time(func() {
    31  		clock = clock.Add(500 * time.Millisecond)
    32  	})
    33  
    34  	assert.Equal(t, 5, timer.Count())
    35  	assert.Equal(t, 250*time.Millisecond, timer.Min())
    36  	assert.Equal(t, 500*time.Millisecond, timer.Max())
    37  	assert.Equal(t, 400*time.Millisecond, timer.Average())
    38  }
    39  
    40  func TestConcurrentTiming(t *testing.T) {
    41  	clock := time.Now()
    42  	timer := debug.NewTimerWithClock(func() time.Time {
    43  		return clock
    44  	})
    45  
    46  	// Two simultaneous starts
    47  	stop1 := timer.Start()
    48  	stop2 := timer.Start()
    49  
    50  	// First one stops after 250 milliseconds.
    51  	clock = clock.Add(250 * time.Millisecond)
    52  	stop1()
    53  
    54  	// Second one stops after 750 milliseconds (the prior 250 + an additional 500).
    55  	clock = clock.Add(500 * time.Millisecond)
    56  	stop2()
    57  
    58  	assert.Equal(t, 2, timer.Count())
    59  	assert.Equal(t, 250*time.Millisecond, timer.Min())
    60  	assert.Equal(t, 750*time.Millisecond, timer.Max())
    61  	assert.Equal(t, 500*time.Millisecond, timer.Average())
    62  }
    63  
    64  func TestAverageZero(t *testing.T) {
    65  	assert.Equal(t, 0*time.Second, debug.NewTimer().Average())
    66  }
    67  

View as plain text