Crates.io | actix-web-isucon-measured |
lib.rs | actix-web-isucon-measured |
version | 0.1.2 |
source | src |
created_at | 2022-04-04 09:27:11.72156 |
updated_at | 2022-04-04 16:04:16.959869 |
description | A middleware to measure request processing time for ISUCON. |
homepage | https://github.com/sengine-xyz/actix-web-isucon-measured |
repository | https://github.com/sengine-xyz/actix-web-isucon-measured |
max_upload_size | |
id | 561906 |
size | 40,638 |
A middleware to measure request processing time for ISUCON.
use actix_web_isucon_measured::{Measured, SortOptions};
use actix_web::middleware::Logger;
use actix_web::{get, web, App, HttpServer, Responder};
use std::time::Duration;
#[get("hello/{id}/{name}")]
async fn hello(params: web::Path<(u32, String)>) -> impl Responder {
let (id, name) = params.into_inner();
actix_web::rt::time::sleep(Duration::from_millis(rand::random::<u8>() as u64)).await;
format!("Hello {}! id: {}\n", name, id)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=debug");
env_logger::init();
let measured = Measured::default();
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(measured.clone()))
.wrap(Logger::default())
.wrap(measured.clone())
.service(hello)
.service(web::resource("/measured_tsv").route(web::get().to(|measured: web::Data<Measured>| async move {
measured.tsv(SortOptions::SUM)
})))
.service(web::resource("/measured_reset").route(web::get().to(|measured: web::Data<Measured>| async move {
measured.clear();
"Reset OK!.\n"
})))
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}