github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/id/snowid/snowid_test.go (about)

     1  package snowid
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestDefaultIdGenerator(t *testing.T) {
    10  	i1, i2 := NextId(), NextId()
    11  	if i1 == i2 {
    12  		t.Fail()
    13  	}
    14  }
    15  
    16  func TestNewIdGenerator(t *testing.T) {
    17  	var options = NewIdGeneratorOptions(1)
    18  	options.BaseTime = time.Now().Add(time.Hour*-1).UnixNano() / 1e6
    19  	options.WorkerIdBitLength = 9
    20  	options.SeqBitLength = 9
    21  	var times = 1000000 // 与 WorkerIdBitLength 有关系
    22  
    23  	SetDefaultIdGenerator(options)
    24  
    25  	// start benchmark test
    26  	t1 := time.Now()
    27  
    28  	for i := 0; i < times; i++ {
    29  		NextId()
    30  	}
    31  
    32  	t2 := time.Now()
    33  	ts := t2.Sub(t1)
    34  	qps := times * 1000 / int(math.Max(float64(1), float64(ts.Milliseconds())))
    35  	t.Logf("Take time %s and %d qps, total %d times", ts, qps, times)
    36  }