Skip to content

Commit 757e6ea

Browse files
committed
chore: add load test to example-axum-otlp to monitor memory leak
1 parent b0ead85 commit 757e6ea

File tree

6 files changed

+96
-3
lines changed

6 files changed

+96
-3
lines changed

.mise.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rust = '1.80.0'
1212
just = '1'
1313
grpcurl = '1.9'
1414
protoc = '29.2'
15+
k6 = '0.43'
1516
# grpc-health-probe = "*"
1617
# sccache = "0.5"
1718
"aqua:cargo-bins/cargo-binstall" = "1" # do not use cargo-binstall (it's a special name used by mise)

examples/axum-otlp/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ init-tracing-opentelemetry = { path = "../../init-tracing-opentelemetry", featur
1414
"otlp",
1515
"tracing_subscriber_ext",
1616
] }
17+
memory-stats = "1.1"
1718
opentelemetry = { workspace = true }
1819
opentelemetry-otlp = { workspace = true, default-features = false, features = [
1920
"reqwest-rustls",

examples/axum-otlp/README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,28 @@ content-type: application/json
7777
content-length: 15
7878
date: Wed, 28 Dec 2022 17:14:07 GMT
7979

80-
{"status":"UP"}
80+
{"physical_mem":16056320,"status":"UP","virtual_mem":1130790912}
8181
```
82+
83+
## Load test
84+
85+
on terminal 1: launch the server: `just run_example_axum-otlp_server`
86+
on terminal 2: periodically print the memory usage: `just run_example_axum-otlp_load_client`
87+
on terminal 3: launch the load test script `just run_example_axum-otlp_load`
88+
89+
```txt
90+
...
91+
{"physical_mem":16633856,"status":"UP","virtual_mem":1130790912}
92+
{"physical_mem":16633856,"status":"UP","virtual_mem":1130790912}
93+
{"physical_mem":18382848,"status":"UP","virtual_mem":1130790912}
94+
{"physical_mem":19562496,"status":"UP","virtual_mem":1130790912}
95+
{"physical_mem":19689472,"status":"UP","virtual_mem":1130790912}
96+
{"physical_mem":20312064,"status":"UP","virtual_mem":1130790912}
97+
{"physical_mem":20529152,"status":"UP","virtual_mem":1130790912}
98+
{"physical_mem":21012480,"status":"UP","virtual_mem":1130790912}
99+
{"physical_mem":21258240,"status":"UP","virtual_mem":1130790912}
100+
{"physical_mem":21286912,"status":"UP","virtual_mem":1130790912}
101+
...
102+
```
103+
104+
Alternatively, you can use the `examples/load`.

examples/axum-otlp/k6-script.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { sleep } from "k6";
2+
import http from "k6/http";
3+
4+
export const options = {
5+
// A number specifying the number of VUs to run concurrently.
6+
vus: 10,
7+
// A string specifying the total duration of the test run.
8+
duration: "120s",
9+
10+
// The following section contains configuration options for execution of this
11+
// test script in Grafana Cloud.
12+
//
13+
// See https://grafana.com/docs/grafana-cloud/k6/get-started/run-cloud-tests-from-the-cli/
14+
// to learn about authoring and running k6 test scripts in Grafana k6 Cloud.
15+
//
16+
// cloud: {
17+
// // The ID of the project to which the test is assigned in the k6 Cloud UI.
18+
// // By default tests are executed in default project.
19+
// projectID: "",
20+
// // The name of the test in the k6 Cloud UI.
21+
// // Test runs with the same name will be grouped.
22+
// name: "script.js"
23+
// },
24+
25+
// Uncomment this section to enable the use of Browser API in your tests.
26+
//
27+
// See https://grafana.com/docs/k6/latest/using-k6-browser/running-browser-tests/ to learn more
28+
// about using Browser API in your test scripts.
29+
//
30+
// scenarios: {
31+
// // The scenario name appears in the result summary, tags, and so on.
32+
// // You can give the scenario any name, as long as each name in the script is unique.
33+
// ui: {
34+
// // Executor is a mandatory parameter for browser-based tests.
35+
// // Shared iterations in this case tells k6 to reuse VUs to execute iterations.
36+
// //
37+
// // See https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/ for other executor types.
38+
// executor: 'shared-iterations',
39+
// options: {
40+
// browser: {
41+
// // This is a mandatory parameter that instructs k6 to launch and
42+
// // connect to a chromium-based browser, and use it to run UI-based
43+
// // tests.
44+
// type: 'chromium',
45+
// },
46+
// },
47+
// },
48+
// }
49+
};
50+
51+
// The function that defines VU logic.
52+
//
53+
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
54+
// about authoring k6 scripts.
55+
//
56+
export default function () {
57+
http.get("http://127.0.0.1:3003/");
58+
sleep(0.2);
59+
}

examples/axum-otlp/src/main.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn app() -> Router {
2828
// build our application with a route
2929
Router::new()
3030
.route(
31-
"/proxy/:service/*path",
31+
"/proxy/{service}/{*path}",
3232
get(proxy_handler).post(proxy_handler),
3333
)
3434
.route("/", get(index)) // request processed inside span
@@ -40,7 +40,10 @@ fn app() -> Router {
4040
}
4141

4242
async fn health() -> impl IntoResponse {
43-
axum::Json(json!({ "status" : "UP" }))
43+
let memory_stats = memory_stats::memory_stats();
44+
axum::Json(
45+
json!({ "status" : "UP", "physical_mem": memory_stats.map(|s| s.physical_mem), "virtual_mem": memory_stats.map(|s| s.virtual_mem) }),
46+
)
4447
}
4548

4649
#[tracing::instrument]

justfile

+6
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,9 @@ run_example_http_client:
126126

127127
run_example_load:
128128
cd examples/load; cargo run --release 2>/dev/null
129+
130+
run_example_axum-otlp_load:
131+
cd examples/axum-otlp; k6 run k6-script.js
132+
133+
run_example_axum-otlp_load_client:
134+
while true; do curl -S http://127.0.0.1:3003/health; echo ""; sleep 3; done

0 commit comments

Comments
 (0)