-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_base.go
84 lines (74 loc) · 2.07 KB
/
client_base.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
package main
import (
_ "ChubbyGo/Config"
"ChubbyGo/Connect"
"fmt"
"log"
"strconv"
"time"
)
/*
* TODO 目前来说这个测试函数是有问题的,集群启动以后的只有第一次执行是ok的,除非每次重设clientID(全局唯一ID)
* 因为上一次的值还在服务器中,但是第二次重启的客户端中clientID和上次一样,但seq却为0,所以会出现很多的重复值。
* 目前并没有什么好的解决方案,因为这是flake算法的局限,暂时不想为测试文件重改flake
*/
const(
get = iota
fastGet
)
const GetStrategy = get
func main(){
n := 60
Sem := make(Connect.Semaphore, n)
SemNumber := 0
clientConfigs := make([]*Connect.ClientConfig,n)
flags := make([]bool, n)
for i := 0; i < n; i++ {
clientConfigs[i] = Connect.CreateClient()
err := clientConfigs[i].StartClient()
if err != nil {
log.Println(err.Error())
} else { // 显然连接成功以后才可以
clientConfigs[i].SetUniqueFlake(uint64(i+n*1)) // 想多次重试OK就每次把这里的0每次递增1就ok
flags[i] = true
}
}
start := time.Now()
for i := 0; i < n; i++{
if !flags[i]{
continue
}
SemNumber++
go func(cliID int){
defer Sem.P(1)
for j := 0; j < 8; j++ {
nv := "x " + strconv.Itoa(cliID) + " " + strconv.Itoa(j) + " y"
clientConfigs[cliID].Put(strconv.Itoa(cliID),nv)
fmt.Println(cliID," : put 成功, ", nv)
var res string
if GetStrategy == get {
res = clientConfigs[cliID].Get(strconv.Itoa(cliID))
} else if GetStrategy == fastGet{
res = clientConfigs[cliID].FastGet(strconv.Itoa(cliID))
} else {
log.Println("Error Get Strategy.")
return
}
if res != nv {
fmt.Printf("%d : expected: %s, now : %s\n",cliID, nv, res)
} else {
fmt.Println(cliID," : Get 成功")
}
}
}(i)
}
Sem.V(SemNumber)
cost := time.Since(start)
fmt.Printf("%d个线程 : %d 个请求花费了 %s.\n",n, n*20, cost)
for i:=0 ;i < len(flags);i++{
if flags[i]{ // 至少一台服务器连接成功并执行完才算是PASS
fmt.Println("PASS!")
return
}
}
}