-
Notifications
You must be signed in to change notification settings - Fork 2
/
conrenttest.go
60 lines (53 loc) · 1.07 KB
/
conrenttest.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
package utils
import (
"fmt"
"sync"
"sync/atomic"
"time"
)
var (
tps int64 = 0
total int64
goroutine_count int = 1000
ops_count int = 1000
)
type TestFun func() error
func CurrentTest(do TestFun, args ...int) {
fmt.Println("======== START ======")
if len(args) > 0 && args[0] > 0 {
goroutine_count = args[0]
}
if len(args) > 1 && args[1] > 0 {
ops_count = args[1]
}
fmt.Printf("Concurrent=%v Each send=%v\n", goroutine_count, ops_count)
wg := sync.WaitGroup{}
wg.Add(goroutine_count)
for cs := 0; cs < goroutine_count; cs++ {
go func() {
for ps := 0; ps < ops_count; ps++ {
if err := do(); err != nil {
fmt.Errorf("err=%v", err)
break
}
atomic.AddInt64(&tps, 1)
}
wg.Done()
}()
}
go listen()
wg.Wait()
fmt.Println("======== END ======")
}
func listen() {
ticket := time.NewTicker(time.Second)
for range ticket.C {
tp := atomic.SwapInt64(&tps, 0)
fmt.Println("tps=", tp)
atomic.AddInt64(&total, tp)
if tp == 0 && total > 0 {
fmt.Println("========end total=", total)
break
}
}
}