1
1
package loader
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
6
+ "github.com/gophergala/gopherling/models"
5
7
"github.com/gorilla/websocket"
6
8
"net/http"
7
9
"sync"
8
10
"time"
9
- "github.com/gophergala/gopherling/models"
10
11
)
11
12
12
13
type feedback struct {
@@ -15,40 +16,62 @@ type feedback struct {
15
16
Duration time.Duration `json:"duration"`
16
17
}
17
18
18
- type loader struct {
19
- n int
20
- c int
21
-
22
- requests []* http. Request
23
- ws * websocket. Conn
19
+ type Task struct {
20
+ method string
21
+ host string
22
+ path string
23
+ headers []models. Header
24
+ rawBody string
24
25
}
25
26
26
- func New (n , c int , ws * websocket.Conn ) * loader {
27
- requests := make ([]* http.Request , 0 )
28
- return & loader {n , c , requests , ws }
29
- }
27
+ func (t * Task ) Request () * http.Request {
28
+ body := bytes .NewBufferString (t .rawBody )
30
29
31
- func (l * loader ) AddTasks (method string , host string , path string , headers []models.Header ) {
32
- req , err := http .NewRequest (method , host + "/" + path , nil )
30
+ req , err := http .NewRequest (t .method , t .host + "/" + t .path , body )
33
31
34
- for _ , header := range headers {
32
+ for _ , header := range t . headers {
35
33
req .Header .Set (header .Field , header .Value )
36
34
}
37
35
38
36
if err != nil {
39
37
fmt .Println ("something went wrong !" )
40
38
}
41
39
42
- l .requests = append (l .requests , req )
40
+ return req
41
+ }
42
+
43
+ type loader struct {
44
+ n int
45
+ c int
46
+
47
+ tasks []* Task
48
+ ws * websocket.Conn
49
+ }
50
+
51
+ func New (n , c int , ws * websocket.Conn ) * loader {
52
+ tasks := make ([]* Task , 0 )
53
+ return & loader {n , c , tasks , ws }
54
+ }
55
+
56
+ func (l * loader ) AddTasks (method string , host string , path string , headers []models.Header , rawBody string ) {
57
+ t := & Task {method : method ,
58
+ host : host ,
59
+ path : path ,
60
+ headers : headers ,
61
+ rawBody : rawBody ,
62
+ }
63
+
64
+ l .tasks = append (l .tasks , t )
43
65
}
44
66
45
- func (l * loader ) spawnClient (wg * sync.WaitGroup , queue chan []* http. Request ) {
67
+ func (l * loader ) spawnClient (wg * sync.WaitGroup , queue chan []* Task ) {
46
68
// this is one out of c clients
47
69
client := & http.Client {}
48
70
49
71
// if we're not busy, get a tasks list from the queue channel
50
- for requests := range queue {
51
- for i , req := range requests {
72
+ for tasks := range queue {
73
+ for i , task := range tasks {
74
+ req := task .Request ()
52
75
start := time .Now ()
53
76
// Send the request
54
77
res , err := client .Do (req )
@@ -75,7 +98,7 @@ func (l *loader) Run() {
75
98
wg .Add (l .n )
76
99
77
100
// The queue channel will contain an array of tasks (up to n time)
78
- queue := make (chan []* http. Request , l .n )
101
+ queue := make (chan []* Task , l .n )
79
102
80
103
// Spawn all our clients
81
104
for i := 0 ; i < l .c ; i ++ {
@@ -86,7 +109,7 @@ func (l *loader) Run() {
86
109
87
110
// Populate our channel with tasks arrays
88
111
for i := 0 ; i < l .n ; i ++ {
89
- queue <- l .requests
112
+ queue <- l .tasks
90
113
}
91
114
92
115
// Close our channel
0 commit comments