forked from mrkschan/c10k-experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
178 lines (158 loc) · 3.28 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
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
package main
import (
"encoding/binary"
"flag"
"fmt"
"log"
"net"
"os"
"strconv"
"time"
)
func usage() {
msg := "Usage: %s [--workers=WORKERS --verbose] REQUESTS"
fmt.Println(msg, os.Args[0])
os.Exit(0)
}
func argparse() (int, int, bool) {
var (
workers int
verbose bool
)
flag.IntVar(&workers, "workers", 1,
"Number of workers to generate requests in parallel")
flag.BoolVar(&verbose, "verbose", false, "Log error to stderr")
flag.Parse()
requests, err := strconv.Atoi(flag.Arg(0))
if err != nil {
usage()
}
return workers, requests, verbose
}
func send_request(verbose bool) int64 {
const (
DNS_ERR = -1
SOCK_ERR = -2
WRITE_ERR = -3
READ_ERR = -4
SVR_ERR = -5
)
addr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8000")
if err != nil {
if verbose {
log.Println(err)
}
return DNS_ERR
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
if verbose {
log.Println(err)
}
return SOCK_ERR
}
defer conn.Close()
start := time.Now().UnixNano()
epoch := time.Now().Unix()
payload := make([]byte, 8)
binary.PutVarint(payload, epoch)
_, err = conn.Write(payload)
if err != nil {
if verbose {
log.Println(err)
}
return WRITE_ERR
}
buffer := make([]byte, 8)
_, err = conn.Read(buffer)
if err != nil {
if verbose {
log.Println(err)
}
return READ_ERR
}
reply, _ := binary.Varint(buffer)
if reply != epoch {
if verbose {
msg := "Integrity error - reply: %d, epoch: %d"
log.Printf(msg, reply, epoch)
}
return SVR_ERR
}
finish := time.Now().UnixNano()
return (finish - start) / 1e6 // in ms.
}
func main() {
workers, requests, verbose := argparse()
queue := make(chan int, requests)
semaphore := make(chan int, requests)
results := make(chan int64, requests)
// Spawn workers
for i := 0; i < workers; i++ {
go func() {
for {
<-queue // Dequeue task
results <- send_request(verbose)
semaphore <- 1 // Mark request as finished
}
}()
}
// Start sending requests
start := time.Now().UnixNano()
for i := 0; i < requests; i++ {
queue <- i
}
// Wait for all requests to be finished
for i := 0; i < requests; i++ {
<-semaphore
}
finish := time.Now().UnixNano()
var (
overall int64 = 0
succeeds int = 0
errors int = 0
dns_errors int = 0
sock_errors int = 0
read_errors int = 0
write_errors int = 0
svr_errors int = 0
avg float32
rps float32
time_spent float32
)
for i := 0; i < requests; i++ {
result := <-results
if result < 0 {
errors += 1
switch result {
case -1:
dns_errors += 1
case -2:
sock_errors += 1
case -3:
write_errors += 1
case -4:
read_errors += 1
case -5:
svr_errors += 1
}
} else {
overall += result
succeeds += 1
}
}
if succeeds > 0 {
avg = float32(overall) / float32(succeeds)
} else {
avg = 0
}
time_spent = float32(finish-start) / 1e+9 // in sec.
rps = float32(requests) / float32(time_spent)
fmt.Printf("Errors: %d, Succeeds: %d\n", errors, succeeds)
fmt.Printf("DNS: %d, Socket: %d, Write: %d, Read: %d, Server: %d\n",
dns_errors, sock_errors, write_errors, read_errors, svr_errors)
fmt.Printf("Response time (avg.): %f ms\n", avg)
fmt.Printf("Requests per second (avg.): %f req/s\n", rps)
fmt.Printf("Time spent: %f s\n", time_spent)
os.Exit(0)
}