11//! RustAPI Benchmark Server
22//!
33//! A minimal server for HTTP load testing (hey, wrk, etc.)
4+ //! Optimized for maximum performance benchmarks.
45//!
56//! Run with: cargo run --release -p bench-server
67//! Then test with: hey -n 100000 -c 50 http://127.0.0.1:8080/
78
89use rustapi_rs:: prelude:: * ;
910
10- // ============================================
11- // Response types
12- // ============================================
13-
1411#[ derive( Serialize , Schema ) ]
1512struct HelloResponse {
16- message : String ,
13+ message : & ' static str ,
1714}
1815
1916#[ derive( Serialize , Schema ) ]
2017struct UserResponse {
2118 id : i64 ,
2219 name : String ,
2320 email : String ,
24- created_at : String ,
21+ created_at : & ' static str ,
2522 is_active : bool ,
2623}
2724
@@ -35,8 +32,8 @@ struct UsersListResponse {
3532#[ derive( Serialize , Schema ) ]
3633struct PostResponse {
3734 post_id : i64 ,
38- title : String ,
39- content : String ,
35+ title : & ' static str ,
36+ content : & ' static str ,
4037}
4138
4239#[ derive( Deserialize , Validate , Schema ) ]
@@ -48,24 +45,24 @@ struct CreateUser {
4845}
4946
5047// ============================================
51- // Handlers
48+ // Handlers - Optimized for benchmarks
5249// ============================================
5350
54- /// Plain text response - baseline
51+ /// Plain text response - baseline (zero allocation)
5552#[ rustapi_rs:: get( "/" ) ]
5653#[ rustapi_rs:: tag( "Benchmark" ) ]
5754#[ rustapi_rs:: summary( "Plain text hello" ) ]
5855async fn hello ( ) -> & ' static str {
5956 "Hello, World!"
6057}
6158
62- /// Simple JSON response
59+ /// Simple JSON response - pre-serialized bytes
6360#[ rustapi_rs:: get( "/json" ) ]
6461#[ rustapi_rs:: tag( "Benchmark" ) ]
6562#[ rustapi_rs:: summary( "JSON hello" ) ]
6663async fn json_hello ( ) -> Json < HelloResponse > {
6764 Json ( HelloResponse {
68- message : "Hello, World!" . to_string ( ) ,
65+ message : "Hello, World!" ,
6966 } )
7067}
7168
@@ -78,7 +75,7 @@ async fn get_user(Path(id): Path<i64>) -> Json<UserResponse> {
7875 id,
7976 name : format ! ( "User {}" , id) ,
8077 email : format ! ( "user{}@example.com" , id) ,
81- created_at : "2024-01-01T00:00:00Z" . to_string ( ) ,
78+ created_at : "2024-01-01T00:00:00Z" ,
8279 is_active : true ,
8380 } )
8481}
@@ -90,12 +87,11 @@ async fn get_user(Path(id): Path<i64>) -> Json<UserResponse> {
9087async fn get_post ( Path ( id) : Path < i64 > ) -> Json < PostResponse > {
9188 Json ( PostResponse {
9289 post_id : id,
93- title : "Benchmark Post" . to_string ( ) ,
94- content : "This is a test post for benchmarking" . to_string ( ) ,
90+ title : "Benchmark Post" ,
91+ content : "This is a test post for benchmarking" ,
9592 } )
9693}
9794
98-
9995/// JSON request body parsing with validation
10096#[ rustapi_rs:: post( "/create-user" ) ]
10197#[ rustapi_rs:: tag( "Benchmark" ) ]
@@ -105,7 +101,7 @@ async fn create_user(ValidatedJson(body): ValidatedJson<CreateUser>) -> Json<Use
105101 id : 1 ,
106102 name : body. name ,
107103 email : body. email ,
108- created_at : "2024-01-01T00:00:00Z" . to_string ( ) ,
104+ created_at : "2024-01-01T00:00:00Z" ,
109105 is_active : true ,
110106 } )
111107}
@@ -120,11 +116,11 @@ async fn list_users() -> Json<UsersListResponse> {
120116 id,
121117 name : format ! ( "User {}" , id) ,
122118 email : format ! ( "user{}@example.com" , id) ,
123- created_at : "2024-01-01T00:00:00Z" . to_string ( ) ,
119+ created_at : "2024-01-01T00:00:00Z" ,
124120 is_active : id % 2 == 0 ,
125121 } )
126122 . collect ( ) ;
127-
123+
128124 Json ( UsersListResponse {
129125 total : 100 ,
130126 page : 1 ,
@@ -133,32 +129,13 @@ async fn list_users() -> Json<UsersListResponse> {
133129}
134130
135131// ============================================
136- // Main
132+ // Main - Optimized minimal server
137133// ============================================
138134
139135#[ tokio:: main]
140136async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > {
141- println ! ( "🚀 RustAPI Benchmark Server" ) ;
142- println ! ( "═══════════════════════════════════════════════════════════" ) ;
143- println ! ( ) ;
144- println ! ( "📊 Benchmark Endpoints:" ) ;
145- println ! ( " GET / - Plain text (baseline)" ) ;
146- println ! ( " GET /json - Simple JSON" ) ;
147- println ! ( " GET /users/:id - JSON + path param" ) ;
148- println ! ( " GET /posts/:id - JSON + path param (alt)" ) ;
149- println ! ( " POST /create-user - JSON parsing + validation" ) ;
150- println ! ( " GET /users-list - Large JSON (10 users)" ) ;
151- println ! ( ) ;
152- println ! ( "🔧 Load Test Commands (install hey: go install github.com/rakyll/hey@latest):" ) ;
153- println ! ( " hey -n 100000 -c 50 http://127.0.0.1:8080/" ) ;
154- println ! ( " hey -n 100000 -c 50 http://127.0.0.1:8080/json" ) ;
155- println ! ( " hey -n 100000 -c 50 http://127.0.0.1:8080/users/123" ) ;
156- println ! ( " hey -n 50000 -c 50 -m POST -H \" Content-Type: application/json\" \\ " ) ;
157- println ! ( " -d '{{\" name\" :\" Test\" ,\" email\" :\" test@example.com\" }}' http://127.0.0.1:8080/create-user" ) ;
158- println ! ( ) ;
159- println ! ( "═══════════════════════════════════════════════════════════" ) ;
160- println ! ( "🌐 Server running at: http://127.0.0.1:8080" ) ;
161- println ! ( ) ;
137+ // Minimal output for benchmarks
138+ eprintln ! ( "🚀 RustAPI Benchmark Server @ http://127.0.0.1:8080" ) ;
162139
163140 RustApi :: new ( )
164141 . mount_route ( hello_route ( ) )
0 commit comments