-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_printserver.go
205 lines (128 loc) · 3.19 KB
/
test_printserver.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
203
204
205
/*
*
* Program to do a load test, and validate the response team of our api
*
* @package main
* @author @jeffotoni
* @size 28/07/2017
*
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
type Ping struct {
Msg string `json:"msg"`
}
var cont = 0
func main() {
cont = 0
var ping = &Ping{}
vetUrl := make(map[int]string)
url := "http://localhost:9001/ping"
vetUrl[0] = url
min := 0
max := 1
// time.Sleep(1 * time.Second)
c1 := make(chan string)
c2 := make(chan string)
startingTime := time.Now().UTC()
fmt.Println("Time start")
i := 0
for i = 0; i < 10; i++ {
time.Sleep(time.Duration(300) * time.Millisecond)
SendPing2(ping, url, 1)
}
endingTime := time.Now().UTC()
var duration time.Duration = endingTime.Sub(startingTime)
fmt.Println("Duration: ", duration, "request: ", i)
os.Exit(1)
/** ########################################### **/
/** ########################################### **/
/** ################# test #################### **/
/** ########################################### **/
/** ########################################### **/
go func() {
sleep := 100
time.Sleep(time.Duration(sleep) * time.Millisecond)
SendPing(ping, vetUrl, min, max, 1, 10, c1)
}()
go func() {
sleep := 100
time.Sleep(time.Duration(sleep) * time.Millisecond)
SendPing(ping, vetUrl, min, max, 2, 20, c2)
// c2 <- "two"
}()
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received", msg1)
case msg2 := <-c2:
fmt.Println("received", msg2)
}
}
}
func SendPing2(ping *Ping, url string, gorutine int) {
MsgTmp := ""
response, err := http.Get(url)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
json.Unmarshal([]byte(string(contents)), &ping)
MsgTmp = strings.TrimSpace(ping.Msg)
MsgTmp = strings.Trim(MsgTmp, " ")
if len(MsgTmp) > 0 {
fmt.Println("gorutine one: ", fmt.Sprintf("%d", gorutine), " :: ", MsgTmp)
} else {
fmt.Println("gorutine 2: ", fmt.Sprintf("%d", gorutine), " :: ", "error")
// fmt.Println(string(contents))
}
ping.Msg = ""
}
func SendPing(ping *Ping, vetUrl map[int]string, min int, max int, gorutine int, sleep int, c chan string) {
i := 1
MsgTmp := ""
for i < sleep {
i += i
cont++
time.Sleep(time.Duration(500) * time.Millisecond)
seed := Seed(min, max)
response, err := http.Get(vetUrl[seed])
if err != nil {
fmt.Println(err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
json.Unmarshal([]byte(string(contents)), &ping)
MsgTmp = strings.TrimSpace(ping.Msg)
MsgTmp = strings.Trim(MsgTmp, " ")
if len(MsgTmp) > 0 {
fmt.Println("cont: ", cont, "gorutine one: ", fmt.Sprintf("%d", gorutine), " :: ", MsgTmp)
} else {
fmt.Println("cont: ", cont, "gorutine 2: ", fmt.Sprintf("%d", gorutine), " :: ", "error")
}
ping.Msg = ""
}
}
c <- fmt.Sprintf("%d", gorutine)
}
func Seed(min int, max int) int {
return (min + rand.Intn(max-min))
}