-
Notifications
You must be signed in to change notification settings - Fork 0
/
agg_test.go
102 lines (86 loc) · 2.32 KB
/
agg_test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package aggregateIncrWrite
import (
"context"
"fmt"
"github.com/go-redis/redis"
. "github.com/smartystreets/goconvey/convey"
"math/rand"
"sync/atomic"
"testing"
"time"
)
func Test_NewAgg(t *testing.T) {
New(&Config{}, SetOptionStore(NewLocalStore()), SetOptionSaveHandler(func(id string, aggIncr int64) error {
return nil
}), SetOptionFailHandler(nil))
}
func Test_AggLocalIncr(t *testing.T) {
ctx := context.TODO()
Convey("test local incr", t, func() {
times := 300000
var total int64
agg := New(
&Config{ConcurrencyBuffer: 10, SaveConcurrency: 10},
SetOptionStore(NewLocalStore()),
SetOptionSaveHandler(func(id string, aggIncr int64) error {
time.Sleep(time.Millisecond)
fmt.Printf("local: id: %s, val: %d\n", id, aggIncr)
atomic.AddInt64(&total, aggIncr)
return nil
}),
SetOptionFailHandler(nil))
for i := 0; i < times; i ++ {
agg.Incr(ctx, fmt.Sprintf("%d", rand.Intn(times)), 1)
time.Sleep(5000*time.Nanosecond)
}
done := make(chan bool)
go func() {
for i := 0; i < times; i ++ {
agg.Incr(ctx, fmt.Sprintf("%d", rand.Intn(1000)), 1)
}
done <- true
for i := 0; i < times/100; i ++ {
agg.Incr(ctx, fmt.Sprintf("%d", rand.Intn(1000)), 1)
}
}()
<- done
agg.Stop(ctx)
So(total, ShouldEqual, times + times + times/100)
})
}
func Test_AggRedisIncr(t *testing.T) {
ctx := context.TODO()
Convey("test redis incr", t, func() {
times := 300000
var total int64
agg := New(
&Config{ConcurrencyBuffer: 10, SaveConcurrency: 100},
SetOptionStore(NewRedisStore("test", redis.NewClient(&redis.Options{Addr:"127.0.0.1:6379"}))),
SetOptionSaveHandler(func(id string, aggIncr int64) error {
fmt.Printf("redis: id: %s, val: %d\n", id, aggIncr)
time.Sleep(time.Millisecond)
atomic.AddInt64(&total, aggIncr)
return nil
}),
SetOptionFailHandler(nil))
for i := 0; i < times; i ++ {
agg.Incr(ctx, fmt.Sprintf("%d", rand.Intn(times)), 1)
//time.Sleep(55*time.Millisecond)
}
done := make(chan bool)
go func() {
for i := 0; i < times; i ++ {
if i % 50 == 0 {
go agg.Incr(ctx, fmt.Sprintf("%d", rand.Intn(1000)), 1)
continue
}
agg.Incr(ctx, fmt.Sprintf("%d", rand.Intn(1000)), 1)
}
done <- true
}()
<- done
time.Sleep(2*time.Second)
agg.Stop(ctx)
So(total, ShouldEqual, times + times)
})
}