-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.go
49 lines (40 loc) · 1.04 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"sync"
"github.com/davidleitw/Sliding/pkg/slidingwindow"
)
var cnt = 0
var wg sync.WaitGroup
func upload(counter int, metadata map[string]int) {
cnt += int(counter)
fmt.Println(counter, metadata)
}
func thread(slw *slidingwindow.Slw, tid int) {
for i := 0; i < 4000000; i++ {
slw.Sync().AtomicWindowCounterAdd(1).AtomicWindowMetaDataAdd(fmt.Sprintf("thread %d", tid), 2)
// time.Sleep(3 * time.Microsecond)
}
defer wg.Done()
}
func main() {
// Ten windows, each has 100 ms.
slw := slidingwindow.NewSlidingWindows(250, 8, slidingwindow.WrapUploadFunc(upload))
slw.SetDefaultMetaKv("thread 0", 0).SetDefaultMetaKv("thread 1", 0).SetDefaultMetaKv("thread 2", 0).SetDefaultMetaKv("thread 3", 0)
fmt.Println(slw)
wg.Add(4)
go thread(slw, 0)
go thread(slw, 1)
go thread(slw, 2)
go thread(slw, 3)
wg.Wait()
fmt.Println(cnt)
wincnt := slw.PrintInfo()
fmt.Println(cnt + wincnt)
defer func(slw *slidingwindow.Slw) {
if err := recover(); err != nil {
fmt.Println("Panic catch!")
slw.PrintInfo()
}
}(slw)
}