-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpbench_test.go
80 lines (70 loc) · 2.51 KB
/
httpbench_test.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
package httpbench
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestCreateHTTPClient(t *testing.T) {
testCases := []struct {
name string
timeout int64
keepalives bool
compression bool
proxyAddress string
proxyUser string
proxyPass string
skipSSLValidation bool
}{
{name: "KeepAlivesAndCompression", timeout: 10, keepalives: true, compression: true, proxyAddress: "", proxyUser: "", proxyPass: "", skipSSLValidation: false},
{name: "KeepAlivesAndNoCompression", timeout: 10, keepalives: true, compression: false, proxyAddress: "", proxyUser: "", proxyPass: "", skipSSLValidation: false},
{name: "NoKeepAlivesAndCompression", timeout: 10, keepalives: false, compression: true, proxyAddress: "", proxyUser: "", proxyPass: "", skipSSLValidation: false},
{name: "NoKeepAlivesAndNoCompression", timeout: 10, keepalives: false, compression: false, proxyAddress: "", proxyUser: "", proxyPass: "", skipSSLValidation: false},
{name: "NoKeepAlivesAndNoCompressionSkipSSLValidation", timeout: 10, keepalives: false, compression: false, proxyAddress: "", proxyUser: "", proxyPass: "", skipSSLValidation: true},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
got := createHTTPClient(int64(test.timeout), test.keepalives, test.compression, "", "", "", true)
expectedDuration := time.Duration(test.timeout) * time.Second
if got.Timeout != expectedDuration {
t.Errorf("expected: %v\ngot: %v", test.timeout, got.Timeout)
}
})
}
}
func TestDispatcher(t *testing.T) {
duration := 2
rps := 5 // Requests per second
expectedTotalRequests := duration * rps
reqChan := make(chan *http.Request, expectedTotalRequests)
server := createTestServer()
// in the future, we might want to test different configurations using table driven tests.
// Here's an example for a single configuration.
dispatcher := Dispatcher{
ReqChan: reqChan,
Duration: duration,
RPS: rps,
URL: server.URL,
Method: "GET",
Body: nil,
Headers: "",
Username: "",
Password: "",
}
if err := dispatcher.Dispatch(); err != nil {
t.Fatal(err)
}
count := 0
for range reqChan {
count++
}
if count != expectedTotalRequests {
t.Errorf("Expected %d requests, but got %d", expectedTotalRequests, count)
}
}
func createTestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, Gopher!"))
}))
}