Skip to content

Commit afe5885

Browse files
Emi Gutekanstvdavid
authored andcommitted
Use the new URLs
1 parent 2bcd400 commit afe5885

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

cmd/src/gateway_benchmark.go

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/gorilla/websocket"
15+
1416
"github.com/sourcegraph/src-cli/internal/cmderrors"
1517
)
1618

@@ -42,8 +44,9 @@ Examples:
4244
flagSet := flag.NewFlagSet("benchmark", flag.ExitOnError)
4345

4446
var (
45-
requestCount = flagSet.Int("requests", 1000, "Number of requests to make per endpoint")
46-
csvOutput = flagSet.String("csv", "", "Export results to CSV file (provide filename)")
47+
requestCount = flagSet.Int("requests", 1000, "Number of requests to make per endpoint")
48+
csvOutput = flagSet.String("csv", "", "Export results to CSV file (provide filename)")
49+
gatewayEndpoint = flagSet.String("gateway", "https://cody-gateway.sourcegraph.com", "Cody Gateway endpoint")
4750
)
4851

4952
handler := func(args []string) error {
@@ -55,26 +58,49 @@ Examples:
5558
return cmderrors.Usage("additional arguments not allowed")
5659
}
5760

58-
// Create HTTP client with TLS skip verify
59-
client := &http.Client{Transport: &http.Transport{}}
60-
61-
endpoints := map[string]string{
62-
"HTTP": fmt.Sprintf("%s/gateway", cfg.Endpoint),
63-
"HTTP then WebSocket": fmt.Sprintf("%s/gateway/http-then-websocket", cfg.Endpoint),
61+
var (
62+
gatewayWebsocket, sourcegraphWebsocket *websocket.Conn
63+
err error
64+
httpClient = &http.Client{}
65+
endpoints = map[string]any{} // Values: URL `string`s or `*websocket.Conn`s
66+
)
67+
if *gatewayEndpoint != "" {
68+
wsURL := strings.Replace(fmt.Sprint(*gatewayEndpoint, "/v2/websocket"), "http", "ws", 1)
69+
gatewayWebsocket, _, err = websocket.DefaultDialer.Dial(wsURL, nil)
70+
if err != nil {
71+
return fmt.Errorf("WebSocket dial(%s): %v", wsURL, err)
72+
}
73+
endpoints["ws(s): gateway"] = gatewayWebsocket
74+
endpoints["http(s): gateway"] = fmt.Sprint(*gatewayEndpoint, "/v2/http")
75+
}
76+
if cfg.Endpoint != "" {
77+
wsURL := strings.Replace(fmt.Sprint(cfg.Endpoint, "/.api/gateway/websocket"), "http", "ws", 1)
78+
sourcegraphWebsocket, _, err = websocket.DefaultDialer.Dial(wsURL, nil)
79+
if err != nil {
80+
return fmt.Errorf("WebSocket dial(%s): %v", wsURL, err)
81+
}
82+
endpoints["ws(s): sourcegraph"] = sourcegraphWebsocket
83+
endpoints["http(s): sourcegraph"] = fmt.Sprint(*gatewayEndpoint, "/.api/gateway/http")
6484
}
6585

6686
fmt.Printf("Starting benchmark with %d requests per endpoint...\n", *requestCount)
6787

6888
var results []endpointResult
69-
70-
for name, url := range endpoints {
89+
for name, clientOrURL := range endpoints {
7190
durations := make([]time.Duration, 0, *requestCount)
7291
fmt.Printf("\nTesting %s...", name)
7392

7493
for i := 0; i < *requestCount; i++ {
75-
duration := benchmarkEndpoint(client, url)
76-
if duration > 0 {
77-
durations = append(durations, duration)
94+
if ws, ok := clientOrURL.(*websocket.Conn); ok {
95+
duration := benchmarkEndpointWebSocket(ws)
96+
if duration > 0 {
97+
durations = append(durations, duration)
98+
}
99+
} else if url, ok := clientOrURL.(string); ok {
100+
duration := benchmarkEndpointHTTP(httpClient, url)
101+
if duration > 0 {
102+
durations = append(durations, duration)
103+
}
78104
}
79105
}
80106
fmt.Println()
@@ -133,15 +159,15 @@ type endpointResult struct {
133159
successful int
134160
}
135161

136-
func benchmarkEndpoint(client *http.Client, url string) time.Duration {
162+
func benchmarkEndpointHTTP(client *http.Client, url string) time.Duration {
137163
start := time.Now()
138164
resp, err := client.Get(url)
139165
if err != nil {
140166
fmt.Printf("Error calling %s: %v\n", url, err)
141167
return 0
142168
}
143-
defer func(Body io.ReadCloser) {
144-
err := Body.Close()
169+
defer func(body io.ReadCloser) {
170+
err := body.Close()
145171
if err != nil {
146172
fmt.Printf("Error closing response body: %v\n", err)
147173
}
@@ -152,10 +178,42 @@ func benchmarkEndpoint(client *http.Client, url string) time.Duration {
152178
fmt.Printf("Error reading response body: %v\n", err)
153179
return 0
154180
}
181+
if resp.StatusCode != http.StatusOK {
182+
fmt.Printf("non-200 response: %v\n", resp.Status)
183+
return 0
184+
}
185+
body, err := io.ReadAll(resp.Body)
186+
if err != nil {
187+
fmt.Printf("Error reading response body: %v\n", err)
188+
return 0
189+
}
190+
if string(body) != "pong" {
191+
fmt.Printf("Expected 'pong' response, got: %q\n", string(body))
192+
return 0
193+
}
155194

156195
return time.Since(start)
157196
}
158197

198+
func benchmarkEndpointWebSocket(conn *websocket.Conn) time.Duration {
199+
start := time.Now()
200+
err := conn.WriteMessage(websocket.TextMessage, []byte("ping"))
201+
if err != nil {
202+
fmt.Printf("WebSocket write error: %v\n", err)
203+
return 0
204+
}
205+
_, message, err := conn.ReadMessage()
206+
if err != nil {
207+
fmt.Printf("WebSocket read error: %v\n", err)
208+
return 0
209+
}
210+
if string(message) != "pong" {
211+
fmt.Printf("Expected 'pong' response, got: %q\n", string(message))
212+
return 0
213+
}
214+
return time.Since(start)
215+
}
216+
159217
func calculateStats(durations []time.Duration) Stats {
160218
if len(durations) == 0 {
161219
return Stats{0, 0, 0, 0, 0, 0, 0}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ require (
4949
require (
5050
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751 // indirect
5151
github.com/google/s2a-go v0.1.4 // indirect
52+
github.com/gorilla/websocket v1.5.3 // indirect
5253
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
5354
github.com/jackc/pgconn v1.14.3 // indirect
5455
github.com/jackc/pgio v1.0.0 // indirect
@@ -196,4 +197,5 @@ require (
196197

197198
// See: https://github.com/ghodss/yaml/pull/65
198199
replace github.com/ghodss/yaml => github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152
200+
199201
replace github.com/sourcegraph/sourcegraph/lib => github.com/sourcegraph/sourcegraph-public-snapshot/lib v0.0.0-20240709083501-1af563b61442

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qK
211211
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
212212
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
213213
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
214+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
215+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
214216
github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db h1:7aN5cccjIqCLTzedH7MZzRZt5/lsAHch6Z3L2ZGn5FA=
215217
github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db/go.mod h1:M5qHK+eWfAv8VR/265dIuEpL3fNfeC21tXXp9itM24A=
216218
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=

0 commit comments

Comments
 (0)