-
Notifications
You must be signed in to change notification settings - Fork 96
feat: app for testing concurrent sampling #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+161
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # sampling-test | ||
|
|
||
| Minimal HTTP server used by the Keploy `--enable-sampling` CI pipeline | ||
| (`.github/workflows/sampling-test.yml` in the keploy/keploy repo). | ||
|
|
||
| `GET /work` sleeps `HANDLER_DELAY_MS` (default 500ms) before responding so a | ||
| burst of concurrent requests overlaps inside the Keploy proxy. With | ||
| `--enable-sampling=K`, the proxy only captures K concurrent in-flight | ||
| requests; the rest are forwarded transparently without being recorded. | ||
|
|
||
| The companion `curl.sh` fires `TOTAL_REQUESTS` (default 20) curls in | ||
| parallel — each one its own TCP connection — to exceed the sampling | ||
| budget and exercise the bypass path. | ||
|
|
||
| After the burst, the workflow asserts: | ||
|
|
||
| - every curl saw a 2xx response (clients are never broken by bypass), and | ||
| - `K <= captured_test_cases < TOTAL_REQUESTS`. | ||
|
|
||
| ## Run locally | ||
|
|
||
| ```bash | ||
| go build -o sampling-test | ||
| ./sampling-test & | ||
| TOTAL_REQUESTS=20 bash curl.sh | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #!/usr/bin/env bash | ||
| # Fire $TOTAL_REQUESTS concurrent GET /work calls against the sample app. | ||
| # Each curl uses its own TCP connection (no -K, no session reuse), so the | ||
| # Keploy proxy sees TOTAL_REQUESTS connection-acquire attempts inside the | ||
| # server-side handler delay window — far more than --enable-sampling slots, | ||
| # forcing the bypass path on most of them. | ||
| # | ||
| # Writes one status-code line per request to $RESULTS_DIR/req-<i>.status. | ||
| # Caller (workflow / verify script) inspects those files to assert that | ||
| # every client got a 2xx response (no 502s, no timeouts) regardless of | ||
| # whether the request was captured. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| TOTAL_REQUESTS="${TOTAL_REQUESTS:-20}" | ||
| TARGET_URL="${TARGET_URL:-http://localhost:8080/work}" | ||
| RESULTS_DIR="${RESULTS_DIR:-./curl-results}" | ||
| CONNECT_TIMEOUT="${CONNECT_TIMEOUT:-5}" | ||
| MAX_TIME="${MAX_TIME:-15}" | ||
|
|
||
| mkdir -p "$RESULTS_DIR" | ||
| rm -f "$RESULTS_DIR"/req-*.status "$RESULTS_DIR"/req-*.body | ||
|
|
||
| echo "Firing $TOTAL_REQUESTS concurrent requests to $TARGET_URL" | ||
|
|
||
| pids=() | ||
| for i in $(seq 1 "$TOTAL_REQUESTS"); do | ||
| ( | ||
| # -o body file, -w status code, -s silent, --no-keepalive so each | ||
| # curl invocation opens its own TCP connection. | ||
| status=$(curl -s -o "$RESULTS_DIR/req-${i}.body" \ | ||
| -w "%{http_code}" \ | ||
| --no-keepalive \ | ||
| --connect-timeout "$CONNECT_TIMEOUT" \ | ||
| --max-time "$MAX_TIME" \ | ||
| "$TARGET_URL" || echo "000") | ||
| echo "$status" > "$RESULTS_DIR/req-${i}.status" | ||
| ) & | ||
| pids+=("$!") | ||
| done | ||
|
|
||
| # Wait for every concurrent curl to finish. | ||
| fail=0 | ||
| for pid in "${pids[@]}"; do | ||
| if ! wait "$pid"; then | ||
| fail=$((fail + 1)) | ||
| fi | ||
| done | ||
|
|
||
| ok=0 | ||
| nonok=0 | ||
| for f in "$RESULTS_DIR"/req-*.status; do | ||
| code=$(cat "$f") | ||
| if [[ "$code" =~ ^2[0-9][0-9]$ ]]; then | ||
| ok=$((ok + 1)) | ||
| else | ||
| nonok=$((nonok + 1)) | ||
| echo "non-2xx response from $(basename "$f"): $code" | ||
| fi | ||
| done | ||
|
|
||
| echo "curl summary: total=$TOTAL_REQUESTS ok=$ok non2xx=$nonok wait_failures=$fail" | ||
|
|
||
| # Any non-2xx is a client-visible failure (proxy must never break clients | ||
| # just because the request was bypassed for sampling). Fail loudly. | ||
| if [[ "$nonok" -ne 0 || "$fail" -ne 0 ]]; then | ||
| exit 1 | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module github.com/keploy/samples-go/sampling-test | ||
|
|
||
| go 1.24.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Package main is a minimal HTTP server used by the Keploy sampling-test | ||
| // pipeline. The /work handler sleeps for HANDLER_DELAY (default 500ms) so a | ||
|
kapishupadhyay22 marked this conversation as resolved.
|
||
| // burst of concurrent requests overlaps inside the Keploy proxy, exercising | ||
| // the --enable-sampling slot semaphore in pkg/agent/proxy/incoming. | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "strconv" | ||
| "sync/atomic" | ||
| "time" | ||
| ) | ||
|
|
||
| var counter atomic.Uint64 | ||
|
|
||
| func parseDelay() time.Duration { | ||
| if v := os.Getenv("HANDLER_DELAY_MS"); v != "" { | ||
| if ms, err := strconv.Atoi(v); err == nil && ms >= 0 { | ||
| return time.Duration(ms) * time.Millisecond | ||
| } | ||
| } | ||
| return 500 * time.Millisecond | ||
| } | ||
|
|
||
| func main() { | ||
| delay := parseDelay() | ||
| port := os.Getenv("PORT") | ||
| if port == "" { | ||
| port = "8080" | ||
| } | ||
|
|
||
| mux := http.NewServeMux() | ||
|
|
||
| mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| _, _ = w.Write([]byte(`{"status":"ok"}`)) | ||
| }) | ||
|
|
||
| mux.HandleFunc("/work", func(w http.ResponseWriter, r *http.Request) { | ||
| id := counter.Add(1) | ||
| time.Sleep(delay) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(map[string]any{ | ||
| "id": id, | ||
| "path": r.URL.Path, | ||
| "delayMs": delay.Milliseconds(), | ||
| "clientAddr": r.RemoteAddr, | ||
| }) | ||
| }) | ||
|
|
||
| addr := ":" + port | ||
| log.Printf("sampling-test listening on %s (handler delay %s)", addr, delay) | ||
| srv := &http.Server{ | ||
| Addr: addr, | ||
| Handler: mux, | ||
| ReadHeaderTimeout: 5 * time.Second, | ||
| } | ||
| if err := srv.ListenAndServe(); err != nil { | ||
| log.Fatalf("server: %v", err) | ||
|
kapishupadhyay22 marked this conversation as resolved.
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.