-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.go
202 lines (168 loc) · 3.82 KB
/
master.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"flag"
"fmt"
"io"
"net"
"os"
"runtime"
"strconv"
"sync"
"time"
)
//最大goroutine数量
var maxRoutineNum = 300
//锁打开文件
var lock = make(chan int, maxRoutineNum)
//锁连接数
var lock_conn = make(chan int, maxRoutineNum)
//最大连接数
var maxConn int
//循环次数
var maxLoop = 10
//测试响应时间
var tcp_times []float64
//测试响应百分位数
var tcp_times_bfw []float64
var wg sync.WaitGroup
func cnn_tcp(filePath string, i int) {
defer wg.Done()
//获取文件属性
fileInfo, err := os.Stat(filePath)
fileName := fileInfo.Name()
if err != nil {
fmt.Printf("os.Stat() failed %v\n", err)
return
}
var conn net.Conn
//计时开始
start := time.Now().UnixNano()
//建立TCP连接
lock_conn <- 1
if i%3 == 0 {
tcpaddr1, err := net.ResolveTCPAddr("tcp", "vm1:30001")
if err != nil {
fmt.Println("resolve failed, err:", err)
return
}
conn, err := net.DialTCP("tcp", nil, tcpaddr1)
if err != nil {
fmt.Println("dial failed, err:", err)
return
}
trans_file(conn, fileName, filePath)
} else if i%3 == 1 {
tcpaddr2, err := net.ResolveTCPAddr("tcp", "vm2:30001")
if err != nil {
fmt.Println("resolve failed, err:", err)
return
}
conn, err = net.DialTCP("tcp", nil, tcpaddr2)
if err != nil {
fmt.Println("dial failed, err:", err)
return
}
trans_file(conn, fileName, filePath)
} else {
tcpaddr3, err := net.ResolveTCPAddr("tcp", "vm3:30001")
if err != nil {
fmt.Println("resolve failed, err:", err)
return
}
conn, err = net.DialTCP("tcp", nil, tcpaddr3)
if err != nil {
fmt.Println("dial failed, err:", err)
return
}
trans_file(conn, fileName, filePath)
}
<- lock_conn
//计算时间
end := time.Now().UnixNano()
tcp_times[i] = float64(end-start) / 1000000.0
tcp_times_bfw[i] += float64(end-start) / 1000000.0
}
func trans_file(conn net.Conn, fileName string, filePath string) {
//发送文件名给接收端
_, err := conn.Write([]byte(fileName))
if err != nil {
fmt.Printf("conn.Write(fileName) failed %v\n", err)
return
}
//读取服务器回发数据go run
buf := make([]byte, 128)
n, err := conn.Read(buf)
if err != nil {
fmt.Printf("conn.Read(buf) failed %v\n", err)
return
}
if string(buf[:n]) == "ok" {
lock <- 1
//只读打开文件
file, err := os.Open(filePath)
if err != nil {
fmt.Printf("os.Open() failed %v\n", err)
return
}
defer file.Close()
buf2 := make([]byte, 4096)
for {
//从本地文件中读数据,写给网络接收端,读多少,写多少
n, err := file.Read(buf2) //file读到buf2
if err != nil {
if err != io.EOF {
fmt.Printf("file.Read() failed %v\n", err)
}
break
}
//写到网络socket中
_, err = conn.Write(buf2[:n])
if err != nil {
fmt.Printf("Write conn failed! err:%v\n", err)
}
}
<-lock
}
}
var res = make([]float64, 10, 20)
func test(filePath string, i int) {
runtime.GOMAXPROCS(runtime.NumCPU())
wg.Add(maxConn)
//filePath := "/gopath/src/dis_test_file/master/emm.txt"
for i := 0; i < maxConn; i++ {
go cnn_tcp(filePath, i)
}
var t float64 = 0
for _, v := range tcp_times {
t += v
}
wg.Wait()
//记录每次循环的平均响应时间
res[i] = t / float64(maxConn)
}
func main() {
flag.Parse()
s := flag.Args()
filePath := s[0]
maxConn0, err := strconv.Atoi(s[1])
if err != nil {
fmt.Println("please reviewer you parameter!")
return
}
maxConn = maxConn0
//测试响应时间
tcp_times = make([]float64, maxConn, maxConn)
//测试响应百分位数
tcp_times_bfw = make([]float64, maxConn, maxConn)
var t float64
for i := 0; i < maxLoop; i++ {
test(filePath, i)
t += res[i]
}
//所有循环次数的平均响应时间的平均值
fmt.Println(t/float64(maxLoop), "ms")
for _, v := range tcp_times_bfw {
v /= 10
fmt.Println(v)
}
}