-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
79 lines (59 loc) · 1.05 KB
/
client.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
package main
import (
"flag"
"fmt"
"net"
"sync"
"time"
)
var success int
var failure int
var count int
func request(mul int, wg *sync.WaitGroup) {
defer func() { count++ }()
defer wg.Done()
strHello := []byte("Hello")
strBye := []byte("Bye!!")
reply := make([]byte, 256)
conn, err := net.DialTimeout("tcp", "localhost:8080", 6*time.Second)
if err != nil {
failure++
return
}
defer conn.Close()
for i := 0; i < mul; i++ {
_, err = conn.Write(strHello)
if err != nil {
failure++
return
}
_, err = conn.Read(reply)
if err != nil {
failure++
return
}
}
_, err = conn.Write(strBye)
if err != nil {
failure++
return
}
success++
}
func main() {
var cnt int
var mul int
var wg sync.WaitGroup
count = 0
success = 0
failure = 0
flag.IntVar(&cnt, "n", 10, "Number of client")
flag.IntVar(&mul, "m", 10, "Number of echo request per client")
flag.Parse()
wg.Add(cnt)
for i := 0; i < cnt; i++ {
go request(mul, &wg)
}
wg.Wait()
fmt.Printf("All: %d, Success: %d, Failure: %d \n", count, success, failure)
}