github.com/loov/hrtime@v1.0.3/stopwatch_test.go (about)

     1  package hrtime_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/loov/hrtime"
     9  )
    10  
    11  func ExampleStopwatch() {
    12  	const numberOfExperiments = 4096
    13  	bench := hrtime.NewStopwatch(numberOfExperiments)
    14  	for i := 0; i < numberOfExperiments; i++ {
    15  		go func() {
    16  			lap := bench.Start()
    17  			defer bench.Stop(lap)
    18  
    19  			time.Sleep(1000 * time.Nanosecond)
    20  		}()
    21  	}
    22  	bench.Wait()
    23  	fmt.Println(bench.Histogram(10))
    24  }
    25  
    26  func ExampleStopwatchTSC() {
    27  	const numberOfExperiments = 4096
    28  	bench := hrtime.NewStopwatchTSC(numberOfExperiments)
    29  	for i := 0; i < numberOfExperiments; i++ {
    30  		go func() {
    31  			lap := bench.Start()
    32  			defer bench.Stop(lap)
    33  
    34  			time.Sleep(1000 * time.Nanosecond)
    35  		}()
    36  	}
    37  	bench.Wait()
    38  	fmt.Println(bench.Histogram(10))
    39  }
    40  
    41  func TestStopwatch(t *testing.T) {
    42  	bench := hrtime.NewStopwatch(8)
    43  	for i := 0; i < 8; i++ {
    44  		go func() {
    45  			lap := bench.Start()
    46  			defer bench.Stop(lap)
    47  
    48  			time.Sleep(1000 * time.Nanosecond)
    49  		}()
    50  	}
    51  	bench.Wait()
    52  	t.Log(bench.Histogram(10))
    53  }
    54  
    55  func TestStopwatchTSC(t *testing.T) {
    56  	bench := hrtime.NewStopwatchTSC(8)
    57  	for i := 0; i < 8; i++ {
    58  		go func() {
    59  			lap := bench.Start()
    60  			defer bench.Stop(lap)
    61  
    62  			time.Sleep(1000 * time.Nanosecond)
    63  		}()
    64  	}
    65  	bench.Wait()
    66  	t.Log(bench.Histogram(10))
    67  }