|
| 1 | +//! Actix-web benchmark server for comparison |
| 2 | +//! |
| 3 | +//! Run with: cargo run --release -p actix-bench-server |
| 4 | +//! Then test with: hey -n 100000 -c 50 http://127.0.0.1:8081/ |
| 5 | +
|
| 6 | +use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | + |
| 9 | +// ============================================ |
| 10 | +// Response types (same as RustAPI) |
| 11 | +// ============================================ |
| 12 | + |
| 13 | +#[derive(Serialize)] |
| 14 | +struct HelloResponse { |
| 15 | + message: String, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Serialize)] |
| 19 | +struct UserResponse { |
| 20 | + id: i64, |
| 21 | + name: String, |
| 22 | + email: String, |
| 23 | + created_at: String, |
| 24 | + is_active: bool, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Serialize)] |
| 28 | +struct UsersListResponse { |
| 29 | + users: Vec<UserResponse>, |
| 30 | + total: usize, |
| 31 | + page: usize, |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Serialize)] |
| 35 | +struct PostResponse { |
| 36 | + user_id: i64, |
| 37 | + post_id: i64, |
| 38 | + title: String, |
| 39 | + content: String, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Deserialize)] |
| 43 | +struct CreateUser { |
| 44 | + name: String, |
| 45 | + email: String, |
| 46 | +} |
| 47 | + |
| 48 | +// ============================================ |
| 49 | +// Handlers |
| 50 | +// ============================================ |
| 51 | + |
| 52 | +#[get("/")] |
| 53 | +async fn hello() -> impl Responder { |
| 54 | + "Hello, World!" |
| 55 | +} |
| 56 | + |
| 57 | +#[get("/json")] |
| 58 | +async fn json_hello() -> impl Responder { |
| 59 | + HttpResponse::Ok().json(HelloResponse { |
| 60 | + message: "Hello, World!".to_string(), |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +#[get("/users/{id}")] |
| 65 | +async fn get_user(path: web::Path<i64>) -> impl Responder { |
| 66 | + let id = path.into_inner(); |
| 67 | + HttpResponse::Ok().json(UserResponse { |
| 68 | + id, |
| 69 | + name: format!("User {}", id), |
| 70 | + email: format!("user{}@example.com", id), |
| 71 | + created_at: "2024-01-01T00:00:00Z".to_string(), |
| 72 | + is_active: true, |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +#[get("/users/{user_id}/posts/{post_id}")] |
| 77 | +async fn get_user_post(path: web::Path<(i64, i64)>) -> impl Responder { |
| 78 | + let (user_id, post_id) = path.into_inner(); |
| 79 | + HttpResponse::Ok().json(PostResponse { |
| 80 | + user_id, |
| 81 | + post_id, |
| 82 | + title: "Benchmark Post".to_string(), |
| 83 | + content: "This is a test post for benchmarking".to_string(), |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +#[post("/users")] |
| 88 | +async fn create_user(body: web::Json<CreateUser>) -> impl Responder { |
| 89 | + HttpResponse::Ok().json(UserResponse { |
| 90 | + id: 1, |
| 91 | + name: body.name.clone(), |
| 92 | + email: body.email.clone(), |
| 93 | + created_at: "2024-01-01T00:00:00Z".to_string(), |
| 94 | + is_active: true, |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +#[get("/users")] |
| 99 | +async fn list_users() -> impl Responder { |
| 100 | + let users: Vec<UserResponse> = (1..=10) |
| 101 | + .map(|id| UserResponse { |
| 102 | + id, |
| 103 | + name: format!("User {}", id), |
| 104 | + email: format!("user{}@example.com", id), |
| 105 | + created_at: "2024-01-01T00:00:00Z".to_string(), |
| 106 | + is_active: id % 2 == 0, |
| 107 | + }) |
| 108 | + .collect(); |
| 109 | + |
| 110 | + HttpResponse::Ok().json(UsersListResponse { |
| 111 | + total: 100, |
| 112 | + page: 1, |
| 113 | + users, |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +// ============================================ |
| 118 | +// Main |
| 119 | +// ============================================ |
| 120 | + |
| 121 | +#[actix_web::main] |
| 122 | +async fn main() -> std::io::Result<()> { |
| 123 | + println!("🚀 Actix-web Benchmark Server (for comparison)"); |
| 124 | + println!("═══════════════════════════════════════════════════════════"); |
| 125 | + println!(); |
| 126 | + println!("📊 Benchmark Endpoints:"); |
| 127 | + println!(" GET / - Plain text (baseline)"); |
| 128 | + println!(" GET /json - Simple JSON"); |
| 129 | + println!(" GET /users/:id - JSON + path param"); |
| 130 | + println!(" GET /users/:uid/posts/:pid - JSON + 2 path params"); |
| 131 | + println!(" POST /users - JSON parsing"); |
| 132 | + println!(" GET /users - Large JSON (10 users)"); |
| 133 | + println!(); |
| 134 | + println!("🔧 Load Test Commands:"); |
| 135 | + println!(" hey -n 100000 -c 50 http://127.0.0.1:8081/"); |
| 136 | + println!(" hey -n 100000 -c 50 http://127.0.0.1:8081/json"); |
| 137 | + println!(); |
| 138 | + println!("═══════════════════════════════════════════════════════════"); |
| 139 | + println!("🌐 Server running at: http://127.0.0.1:8081"); |
| 140 | + println!(); |
| 141 | + |
| 142 | + HttpServer::new(|| { |
| 143 | + App::new() |
| 144 | + .service(hello) |
| 145 | + .service(json_hello) |
| 146 | + .service(get_user) |
| 147 | + .service(get_user_post) |
| 148 | + .service(create_user) |
| 149 | + .service(list_users) |
| 150 | + }) |
| 151 | + .bind("127.0.0.1:8081")? |
| 152 | + .run() |
| 153 | + .await |
| 154 | +} |
0 commit comments