-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
234 lines (182 loc) · 4.99 KB
/
main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"runtime"
"time"
)
// RequestSimple - implement HTTP Request function
func RequestSimple(url string) []byte {
resp, err := http.Get(url)
if err != nil {
return nil
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}
// RequestFunc - implement function with goroutine witch return responce into channel
func RequestFunc(url string) <-chan []byte {
c := make(chan []byte, 1)
go func() {
var body []byte
defer func() {
c <- body
}()
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
}()
return c
}
// RequestFeatureFunc - implement function with goroutine witch return closure as responce witch return channel
func RequestFeatureFunc(url string) func() ([]byte, error) {
var body []byte
var err error
c := make(chan struct{}, 1)
go func() {
defer close(c)
var resp *http.Response
resp, err = http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
}()
return func() ([]byte, error) {
<-c
return body, err
}
}
// func Feature(f func(interface{}, error)) func() (interface{}, error) {
// var result interface{}
// var err error
// c := make(chan struct{}, 1)
// go func() {
// defer close(c)
// result, err = f()
// }()
// return func() (interface{}, error) {
// <-c
// return result, err
// }
// }
var version = flag.Int("v", 1, "version v=1")
func main() {
flag.Parse()
runtime.GOMAXPROCS(4)
switch *version {
case 1:
start := time.Now()
// simple way
body := RequestSimple("http://www.dmv.com")
body2 := RequestSimple("http://www.wealthprep.ca")
body3 := RequestSimple("http://www.carros.com.do")
body4 := RequestSimple("http://www.mbhs.com/")
elapsed := time.Since(start)
fmt.Println("version #1")
fmt.Println("length =", len(body))
fmt.Println("length2 =", len(body2))
fmt.Println("length3 =", len(body3))
fmt.Println("length4 =", len(body4))
fmt.Println(elapsed)
break
case 2:
start := time.Now()
// concurrent way
page := RequestFunc("http://www.dmv.com")
page2 := RequestFunc("http://www.wealthprep.ca")
page3 := RequestFunc("http://www.carros.com.do")
page4 := RequestFunc("http://www.mbhs.com")
body := <-page
body2 := <-page2
body3 := <-page3
body4 := <-page4
elapsed := time.Since(start)
fmt.Println("version #2")
fmt.Println("length =", len(body))
fmt.Println("length2 =", len(body2))
fmt.Println("length3 =", len(body3))
fmt.Println("length4 =", len(body4))
fmt.Println(elapsed)
break
case 3:
start := time.Now()
page := RequestFeatureFunc("http://www.dmv.com")
page2 := RequestFeatureFunc("http://www.wealthprep.ca")
page3 := RequestFeatureFunc("http://www.carros.com.do")
page4 := RequestFeatureFunc("http://www.mbhs.com")
body, err := page()
body2, err := page2()
body3, err := page3()
body4, err := page4()
elapsed := time.Since(start)
fmt.Println("version #3")
fmt.Printf("error %v\n", err)
fmt.Println("length =", len(body))
fmt.Println("length2 =", len(body2))
fmt.Println("length3 =", len(body3))
fmt.Println("length4 =", len(body4))
fmt.Println(elapsed)
break
case 4:
start := time.Now()
body := RequestSimple("http://www.dmv.com")
body2 := RequestSimple("http://www.wealthprep.ca")
body3 := RequestSimple("http://www.carros.com.do")
body4 := RequestSimple("http://www.mbhs.com")
body5 := RequestSimple("http://chatter.ru/chat")
body6 := RequestSimple("http://ad.nl")
body7 := RequestSimple("http://www.adweek.com")
body8 := RequestSimple("http://upwork.com")
elapsed := time.Since(start)
fmt.Println("version #4")
fmt.Println("length =", len(body))
fmt.Println("length2 =", len(body2))
fmt.Println("length3 =", len(body3))
fmt.Println("length4 =", len(body4))
fmt.Println("length5 =", len(body5))
fmt.Println("length6 =", len(body6))
fmt.Println("length7 =", len(body7))
fmt.Println("length8 =", len(body8))
fmt.Println(elapsed)
break
case 5:
start := time.Now()
// concurrent way
page := RequestFunc("http://www.dmv.com")
page2 := RequestFunc("http://www.wealthprep.ca")
page3 := RequestFunc("http://www.carros.com.do")
page4 := RequestFunc("http://www.mbhs.com")
page5 := RequestFunc("http://chatter.ru/chat")
page6 := RequestFunc("http://ad.nl")
page7 := RequestFunc("http://www.adweek.com")
page8 := RequestFunc("http://upwork.com")
body := <-page
body2 := <-page2
body3 := <-page3
body4 := <-page4
body5 := <-page5
body6 := <-page6
body7 := <-page7
body8 := <-page8
elapsed := time.Since(start)
fmt.Println("version #5")
fmt.Println("length =", len(body))
fmt.Println("length2 =", len(body2))
fmt.Println("length3 =", len(body3))
fmt.Println("length4 =", len(body4))
fmt.Println("length5 =", len(body5))
fmt.Println("length6 =", len(body6))
fmt.Println("length7 =", len(body7))
fmt.Println("length8 =", len(body8))
fmt.Println(elapsed)
break
}
}