@@ -19,13 +19,16 @@ import (
1919 "github.com/goadapp/goad/queue"
2020)
2121
22+ const lambdaTimeout = 295
23+
2224func main () {
2325
2426 var (
2527 address string
2628 sqsurl string
2729 concurrencycount int
2830 maxRequestCount int
31+ execTimeout int
2932 timeout string
3033 frequency string
3134 awsregion string
@@ -46,10 +49,15 @@ func main() {
4649
4750 flag .IntVar (& concurrencycount , "c" , 10 , "number of concurrent requests" )
4851 flag .IntVar (& maxRequestCount , "n" , 1000 , "number of total requests to make" )
52+ flag .IntVar (& execTimeout , "N" , 0 , "Maximum execution time in seconds" )
4953
5054 flag .Var (& requestHeaders , "H" , "List of headers" )
5155 flag .Parse ()
5256
57+ if execTimeout <= 0 || execTimeout > lambdaTimeout {
58+ execTimeout = lambdaTimeout
59+ }
60+
5361 clientTimeout , _ := time .ParseDuration (timeout )
5462 fmt .Printf ("Using a timeout of %s\n " , clientTimeout )
5563 reportingFrequency , _ := time .ParseDuration (frequency )
@@ -63,7 +71,7 @@ func main() {
6371 client .Timeout = clientTimeout
6472
6573 fmt .Printf ("Will spawn %d workers making %d requests to %s\n " , concurrencycount , maxRequestCount , address )
66- runLoadTest (client , sqsurl , address , maxRequestCount , concurrencycount , awsregion , reportingFrequency , queueRegion , requestMethod , requestBody , requestHeaders )
74+ runLoadTest (client , sqsurl , address , maxRequestCount , execTimeout , concurrencycount , awsregion , reportingFrequency , queueRegion , requestMethod , requestBody , requestHeaders )
6775}
6876
6977type RequestResult struct {
@@ -80,7 +88,7 @@ type RequestResult struct {
8088 State string `json:"state"`
8189}
8290
83- func runLoadTest (client * http.Client , sqsurl string , url string , totalRequests int , concurrencycount int , awsregion string , reportingFrequency time.Duration , queueRegion string , requestMethod string , requestBody string , requestHeaders []string ) {
91+ func runLoadTest (client * http.Client , sqsurl string , url string , totalRequests int , execTimeout int , concurrencycount int , awsregion string , reportingFrequency time.Duration , queueRegion string , requestMethod string , requestBody string , requestHeaders []string ) {
8492 awsConfig := aws .NewConfig ().WithRegion (queueRegion )
8593 sqsAdaptor := queue .NewSQSAdaptor (awsConfig , sqsurl )
8694 //sqsAdaptor := queue.NewDummyAdaptor(sqsurl)
@@ -102,10 +110,10 @@ func runLoadTest(client *http.Client, sqsurl string, url string, totalRequests i
102110 fmt .Println (" done.\n Waiting for results…" )
103111
104112 ticker := time .NewTicker (reportingFrequency )
105- quit := make ( chan struct {} )
113+ quit := time . NewTimer ( time . Duration ( execTimeout ) * time . Second )
106114 quitting := false
107115
108- for requestsSoFar < totalRequests && ! quitting {
116+ for ( totalRequests == 0 || requestsSoFar < totalRequests ) && ! quitting {
109117 i := 0
110118
111119 var timeToFirstTotal int64
@@ -120,7 +128,7 @@ func runLoadTest(client *http.Client, sqsurl string, url string, totalRequests i
120128 var totalConnectionError int
121129
122130 resetStats := false
123- for requestsSoFar < totalRequests && ! quitting && ! resetStats {
131+ for ( totalRequests == 0 || requestsSoFar < totalRequests ) && ! quitting && ! resetStats {
124132 select {
125133 case r := <- ch :
126134 i ++
@@ -164,12 +172,14 @@ func runLoadTest(client *http.Client, sqsurl string, url string, totalRequests i
164172 statuses [statusStr ]++
165173 }
166174 requestTimeTotal += r .Elapsed
175+
167176 case <- ticker .C :
168177 if i == 0 {
169178 continue
170179 }
171180 resetStats = true
172- case <- quit :
181+
182+ case <- quit .C :
173183 ticker .Stop ()
174184 quitting = true
175185 }
@@ -198,6 +208,7 @@ func runLoadTest(client *http.Client, sqsurl string, url string, totalRequests i
198208 avgRequestTime = 0
199209 }
200210
211+ finished := (totalRequests > 0 && requestsSoFar == totalRequests ) || quitting
201212 fatalError := ""
202213 if (totalTimedOut + totalConnectionError ) > i / 2 {
203214 fatalError = "Over 50% of requests failed, aborting"
@@ -216,6 +227,7 @@ func runLoadTest(client *http.Client, sqsurl string, url string, totalRequests i
216227 fastest ,
217228 awsregion ,
218229 fatalError ,
230+ finished ,
219231 }
220232 sqsAdaptor .SendResult (aggData )
221233 }
@@ -225,7 +237,13 @@ func runLoadTest(client *http.Client, sqsurl string, url string, totalRequests i
225237
226238func fetch (loadTestStartTime time.Time , client * http.Client , address string , requestcount int , jobs <- chan struct {}, ch chan RequestResult , wg * sync.WaitGroup , awsregion string , requestMethod string , requestBody string , requestHeaders []string ) {
227239 defer wg .Done ()
228- for _ = range jobs {
240+ for {
241+ if requestcount > 0 {
242+ _ , ok := <- jobs
243+ if ! ok {
244+ break
245+ }
246+ }
229247 start := time .Now ()
230248 req , err := http .NewRequest (requestMethod , address , bytes .NewBufferString (requestBody ))
231249 if err != nil {
0 commit comments