github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/cache/fastcache/timeline_test.go (about)

     1  package fastcache
     2  
     3  import (
     4  	"github.com/angenalZZZ/gofunc/data"
     5  	"github.com/angenalZZZ/gofunc/data/random"
     6  	"github.com/angenalZZZ/gofunc/f"
     7  	"path/filepath"
     8  	"regexp"
     9  	"sort"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestTimelineInit(t *testing.T) {
    15  	tl := NewTimeline(time.Now(), time.Now().Add(50*time.Second), 5*time.Second, data.RootDir, 1024)
    16  	t.Logf("%3d: %s", tl.Index, f.NowLocalString(true))
    17  	for index := int64(0); tl.Index != -1; {
    18  		for index == tl.Index {
    19  			time.Sleep(time.Microsecond)
    20  		}
    21  		if tl.Index != -1 {
    22  			t.Logf("%3d: %s", tl.Index, f.NowLocalString(true))
    23  			index = tl.Index
    24  		} else {
    25  			t.Logf("end: %s", f.NowLocalString(true))
    26  		}
    27  	}
    28  }
    29  
    30  func TestTimelineReader(t *testing.T) {
    31  	oldFiles, _ := filepath.Glob(filepath.Join(data.RootDir, "*"))
    32  	sort.Strings(oldFiles)
    33  	for _, oldFile := range oldFiles {
    34  		_, f := filepath.Split(oldFile)
    35  		if ok, _ := regexp.MatchString(`^\d{10,}\.\d+`, f); !ok {
    36  			continue
    37  		}
    38  		t.Log(oldFile)
    39  		//s := strings.Split(f, ".")
    40  		//start, _ := strconv.ParseInt(s[0], 10, 0)
    41  		//index, _ := strconv.ParseInt(s[1], 10, 0)
    42  		//cache, err := fastcache.LoadFromFile(oldFile)
    43  		//if err != nil || cache == nil {
    44  		//	continue
    45  		//}
    46  		//writer := &CacheWriter{
    47  		//	Cache: cache,
    48  		//	Start: start,
    49  		//	Index: uint32(index),
    50  		//}
    51  	}
    52  }
    53  
    54  // go test -v -cpu=4 -benchtime=10s -benchmem -bench=^BenchmarkTimelineWriter -run ^none$ github.com/angenalZZZ/gofunc/data/cache/fastcache
    55  // go test -c -o %TEMP%\t01.exe github.com/angenalZZZ/gofunc/data/cache/fastcache && %TEMP%\t01.exe -test.v -test.bench ^BenchmarkTimelineWriter -test.run ^none$
    56  func BenchmarkTimelineWriter(b *testing.B) {
    57  	b.StopTimer()
    58  	tl := NewTimeline(time.Now(), time.Now().Add(50*time.Second), 5*time.Second, data.RootDir, 1024)
    59  	//l := 5120 // every time 5kB data request: cpu=4 1200k/qps 0.8ms/op
    60  	//l := 2018 // every time 2kB data request: cpu=4 1800k/qps 0.5ms/op
    61  	//l := 1024 // every time 1kB data request: cpu=4 2400k/qps 0.4ms/op
    62  	l := 128 // every time 128B data request: cpu=4 3200k/qps 0.3ms/op
    63  	p := []byte(random.AlphaNumberLower(l))
    64  	b.StartTimer()
    65  	for i := 0; i < b.N; i++ {
    66  		_, _ = tl.Write(p)
    67  	}
    68  }