@@ -13,12 +13,13 @@ import (
13
13
"testing"
14
14
"time"
15
15
16
+ "github.com/hashicorp/nomad/nomad/mock"
16
17
"github.com/hashicorp/nomad/nomad/structs"
17
18
"github.com/hashicorp/nomad/testutil"
18
19
)
19
20
20
21
type TestServer struct {
21
- T * testing.T
22
+ T testing.TB
22
23
Dir string
23
24
Agent * Agent
24
25
Server * HTTPServer
@@ -30,9 +31,25 @@ func (s *TestServer) Cleanup() {
30
31
os .RemoveAll (s .Dir )
31
32
}
32
33
33
- func makeHTTPServer (t * testing.T , cb func (c * Config )) * TestServer {
34
+ // makeHTTPServerNoLogs returns a test server with full logging.
35
+ func makeHTTPServer (t testing.TB , cb func (c * Config )) * TestServer {
36
+ return makeHTTPServerWithWriter (t , nil , cb )
37
+ }
38
+
39
+ // makeHTTPServerNoLogs returns a test server which only prints agent logs and
40
+ // no http server logs
41
+ func makeHTTPServerNoLogs (t testing.TB , cb func (c * Config )) * TestServer {
42
+ return makeHTTPServerWithWriter (t , ioutil .Discard , cb )
43
+ }
44
+
45
+ // makeHTTPServerWithWriter returns a test server whose logs will be written to
46
+ // the passed writer. If the writer is nil, the logs are written to stderr.
47
+ func makeHTTPServerWithWriter (t testing.TB , w io.Writer , cb func (c * Config )) * TestServer {
34
48
dir , agent := makeAgent (t , cb )
35
- srv , err := NewHTTPServer (agent , agent .config , agent .logOutput )
49
+ if w == nil {
50
+ w = agent .logOutput
51
+ }
52
+ srv , err := NewHTTPServer (agent , agent .config , w )
36
53
if err != nil {
37
54
t .Fatalf ("err: %v" , err )
38
55
}
@@ -45,6 +62,37 @@ func makeHTTPServer(t *testing.T, cb func(c *Config)) *TestServer {
45
62
return s
46
63
}
47
64
65
+ func BenchmarkHTTPRequests (b * testing.B ) {
66
+ s := makeHTTPServerNoLogs (b , func (c * Config ) {
67
+ c .Client .Enabled = false
68
+ })
69
+ defer s .Cleanup ()
70
+
71
+ job := mock .Job ()
72
+ var allocs []* structs.Allocation
73
+ count := 1000
74
+ for i := 0 ; i < count ; i ++ {
75
+ alloc := mock .Alloc ()
76
+ alloc .Job = job
77
+ alloc .JobID = job .ID
78
+ alloc .Name = fmt .Sprintf ("my-job.web[%d]" , i )
79
+ allocs = append (allocs , alloc )
80
+ }
81
+
82
+ handler := func (resp http.ResponseWriter , req * http.Request ) (interface {}, error ) {
83
+ return allocs [:count ], nil
84
+ }
85
+ b .ResetTimer ()
86
+
87
+ b .RunParallel (func (pb * testing.PB ) {
88
+ for pb .Next () {
89
+ resp := httptest .NewRecorder ()
90
+ req , _ := http .NewRequest ("GET" , "/v1/kv/key" , nil )
91
+ s .Server .wrap (handler )(resp , req )
92
+ }
93
+ })
94
+ }
95
+
48
96
func TestSetIndex (t * testing.T ) {
49
97
resp := httptest .NewRecorder ()
50
98
setIndex (resp , 1000 )
0 commit comments