# stress.go -rw-r--r-- 1.8 KiB View raw
                                                                                
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
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strconv"
	"sync"
	"sync/atomic"
	"time"
)

var query string = `
query {
	me {
		id
		canonicalName
		sshKeys {
			cursor
			results {
				fingerprint
			}
		}
	}
}
`

var errors sync.Map

func doReqs(wg *sync.WaitGroup, tok string, nsuccess, nfail *int64, duration *int64) {
	defer wg.Done()
	start := time.Now()
	defer func() {
		elapsed := time.Now().Sub(start)
		atomic.AddInt64(duration, elapsed.Milliseconds())
	}()

	client := http.Client{}
	body, err := json.Marshal(&struct{
		Query string `json:"query"`
	}{
		Query: query,
	})
	if err != nil {
		panic(err)
	}
	req, err := http.NewRequest("POST", "http://localhost:5100/query",
		bytes.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Add("Authorization", "Bearer " + tok)
	req.Header.Add("Content-Type", "application/json")
	if resp, err := client.Do(req); err != nil {
		atomic.AddInt64(nfail, 1)
		errors.Store(fmt.Sprintf("%v", err), nil)
	} else {
		defer resp.Body.Close()
		if resp.StatusCode == 200 {
			atomic.AddInt64(nsuccess, 1)
		} else {
			body, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				return
			}
			errors.Store(string(body), nil)
			atomic.AddInt64(nfail, 1)
		}
	}
}

func main() {
	batch, _ := strconv.Atoi(os.Args[2])
	log.Printf("Sending batch of %d requests", batch)
	var nsuccess, nfail, duration int64
	var wg sync.WaitGroup
	wg.Add(batch)
	for i := 0; i < batch; i += 1 {
		go doReqs(&wg, os.Args[1], &nsuccess, &nfail, &duration)
	}
	time.Sleep(1 * time.Second)
	wg.Wait()
	log.Printf("%d success, %d failure; %dms avg",
		nsuccess, nfail, duration / int64(batch))
	errors.Range(func(key, value interface{}) bool {
		body, _ := key.(string)
		log.Printf("Sample error: %s", body)
		return true
	})
}