github.com/sandwich-go/boost@v1.3.29/xtime/cop_test.go (about)

     1  package xtime
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func BenchmarkTimeUnixWithSystem(b *testing.B) {
    11  	for i := 0; i < b.N; i++ {
    12  		_ = time.Now().Unix()
    13  	}
    14  }
    15  
    16  func BenchmarkTimeUnixWithCop(b *testing.B) {
    17  	for i := 0; i < b.N; i++ {
    18  		_ = Unix()
    19  	}
    20  }
    21  
    22  func BenchmarkCompareSystemAndCop(b *testing.B) {
    23  	var wg sync.WaitGroup
    24  
    25  	for i := 0; i < 1000; i++ {
    26  		wg.Add(1)
    27  		go func() {
    28  			// Decrement the counter when the goroutine completes.
    29  			defer wg.Done()
    30  
    31  			for i := 0; i < b.N; i++ {
    32  				n := Unix()
    33  				if n > (time.Now().Unix()+1) || n < (time.Now().Unix()-1) {
    34  					fmt.Println("Error Cop:", n, "time.Now().Unix():", time.Now().Unix())
    35  					b.Fail()
    36  					return
    37  				}
    38  				time.Sleep(1 * time.Millisecond)
    39  			}
    40  		}()
    41  	}
    42  
    43  	wg.Wait()
    44  }