Skip to content

Instantly share code, notes, and snippets.

@rayyildiz
Created May 12, 2024 19:00
Show Gist options
  • Select an option

  • Save rayyildiz/2d72717b8a5a964ad7f169127dd2efea to your computer and use it in GitHub Desktop.

Select an option

Save rayyildiz/2d72717b8a5a964ad7f169127dd2efea to your computer and use it in GitHub Desktop.

Axum Framework Performance Testing Demo Project

This project is a demonstration of the Axum web application framework and its performance capabilities. It utilizes the Rust programming language to test the performance and efficiency of Axum.

Requirements

  • Rust 1.74 or later

Setup

To set up this project on your machine, first, you need to install Rust. Here are the steps to do that:

  • Install Rustup: Rustup is a toolchain installer for the Rust programming language. You can install it by running the following command in your terminal:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

This will also install Rust, Cargo (Rust's package manager), and other necessary tools for Rust.

  • Verify the installation: Once the above command completes its execution, restart your terminal and then run the following command:

rustc --version

cargo new http-req-count

About the Project

This project is primarily aimed at testing the performance of the Axum framework. The demo project sets up a simple HTTP API with a "/up" route that increments a counter held in shared application state. The demo highlights some of the core capabilities of the Axum framework, especially as it comes to managing shared application state across routes in the context of a multithreaded server environment. This performance testing project is a great way to explore the features of the Axum framework and get an understanding of how it performs under different workloads.

Run Test

  • Before starting the load test, ensure your application is running. You can start your application (make sure you're in the right directory) with:

cargo run

  • Open a new terminal window.
  • Start a wrk test with the following command:

wrk -t12 -c400 -d30s http://127.0.0.1:3000/up

This instructs wrk to use 12 threads, keep 400 HTTP connections open, and run the test for 30 seconds. You should adjust these according to your requirements or hardware capabilities.

After the test finishes, wrk will give you a report with details about how many HTTP requests were made, latency information, the number of requests per second, and more.

PLEASE NOTE: Load testing an application can put significant stress on your server and network. ONLY do this on servers and networks you control, and ONLY after understanding the implications. This can have implications on your network and service availability. Use responsibly.

[package]
name = "http-req-count"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = { version = "0.7" }
tokio = { version = "1.37", features = ["macros", "rt-multi-thread", "rt"] }
use std::sync::{Arc, Mutex};
use axum::extract::State;
use axum::response::IntoResponse;
use axum::Router;
use axum::routing::get;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let s = AppState {
count: 0,
};
let app = Router::new()
.route("/up", get(handler_up))
.with_state(Arc::new(Mutex::new(s)));
let listener = TcpListener::bind("0.0.0.0:3000")
.await
.unwrap();
axum::serve(listener, app)
.await
.unwrap_or_else(|e| panic!("can not start server {}", e));
Ok(())
}
async fn handler_up(State(state): State<Arc<Mutex<AppState>>>) -> impl IntoResponse {
let mut state = state.lock().unwrap();
state.count += 1;
format!("{}", state.count)
}
struct AppState {
count: i32,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment