Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
shzy2012 committed Apr 7, 2022
1 parent 75728d8 commit f525701
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

run:
cargo run -- -t 3 -c 10 -n 100000 http://localhost:8000/h
cargo run -- -c 100 -n 11750 https://www.baidu.com

crazy:
cargo run -- crazy http://172.16.0.116:8000/healthz

build:
cargo build --release
Expand All @@ -9,4 +12,8 @@ install:build
cp target/release/yc /usr/local/bin/

test:
./target/release/yc -c 1 -n 2000 http://localhost:8000/h
./target/release/yc -c 1 -n 2000 http://localhost:8000/h

ls:
# 统计已连接上的,状态为“established
netstat -na|grep ESTABLISHED|wc -l
7 changes: 7 additions & 0 deletions src/libs/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use reqwest::Url;
use std::env;
pub struct Args {
pub crazy: bool,
// 时间秒
pub t: u32,
// 请求次数
Expand All @@ -14,6 +15,7 @@ pub struct Args {
impl Args {
pub fn new() -> Self {
Self {
crazy: false,
t: 0,
c: 1, //默认
n: 10, //默认
Expand Down Expand Up @@ -46,6 +48,10 @@ impl Args {
Err(_) => panic!("-t need number"),
}
}
"crazy" => {
i += 1;
self.crazy = true
}
_ => {
// 解析url
if self.url == "" {
Expand All @@ -69,6 +75,7 @@ impl Args {
};
}
Self {
crazy: self.crazy,
t: self.t,
c: self.c,
n: self.n,
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}

if args.t > 0 {
crazy::run(args.t as u64, url).await;
if args.crazy {
crazy::run(args.t as u64, url)
.await
.expect("crazy mode ERROR");
} else {
comm::run(args.n as usize, args.c as usize, url).await;
comm::run(args.n as usize, args.c as usize, url)
.await
.expect("comm mode ERROR");
}

Ok(())
Expand Down
7 changes: 5 additions & 2 deletions src/tester/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ n: 请求次数
# Examples
*/
pub async fn run(n: usize, c: usize, url: &str) {
pub async fn run(n: usize, c: usize, url: &str) -> Result<(), Box<dyn std::error::Error>> {
// 初始化channle
let (tx, rx) = mpsc::channel(10);

Expand All @@ -25,7 +25,9 @@ pub async fn run(n: usize, c: usize, url: &str) {
});

// 创建任务
let client = Client::new();
let client = Client::builder()
.timeout(time::Duration::from_secs(9))
.build()?;
let reqs = vec![url; n];

let tasks = stream::iter(reqs)
Expand Down Expand Up @@ -64,6 +66,7 @@ pub async fn run(n: usize, c: usize, url: &str) {
drop(tx);

if let Ok(_) = t1.await {}
Ok(())
}

pub async fn format(n: usize, c: usize, url: String, mut rx: mpsc::Receiver<(u32, u128, usize)>) {
Expand Down
21 changes: 13 additions & 8 deletions src/tester/crazy.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use crate::libs::tools::pretty;
use reqwest::{Client, Url};
use std::error::Error;
use std::time;
use tokio::sync::mpsc;
use tokio::task;

// 疯狂模式
pub async fn run(t: u64, url: &str) {
let now = time::Instant::now();
let client = Client::new();
let (tx, rx) = mpsc::channel(100);
pub async fn run(t: u64, url: &str) -> Result<(), Box<dyn Error>> {
let client = Client::builder()
.timeout(time::Duration::from_secs(10))
.build()?;
let (tx, rx) = mpsc::channel(1000);
let u = url.to_string();
let t1 = task::spawn(async move {
format(t, u, rx).await;
});

for i in 0.. {
if now.elapsed().as_secs() >= t {
// 单机6万
if i >= 60000 {
println!("发送总数: {}", i);
break;
}
Expand Down Expand Up @@ -45,6 +48,7 @@ pub async fn run(t: u64, url: &str) {
}
}
Err(_e) => {
println!("{}", _e);
if let Err(e) = tx_x.send((400, 0u128, 0)).await {
panic!("{}", e);
}
Expand All @@ -55,6 +59,7 @@ pub async fn run(t: u64, url: &str) {
drop(tx);

if let Ok(_) = t1.await {}
Ok(())
}

pub async fn format(t: u64, url: String, mut rx: mpsc::Receiver<(u32, u128, usize)>) {
Expand Down Expand Up @@ -83,14 +88,14 @@ pub async fn format(t: u64, url: String, mut rx: mpsc::Receiver<(u32, u128, usiz

// 输出统计
let stop = time::Instant::now();
let min = (stop - start).as_micros() as f64 / 1000000f64;
let used_min = (stop - start).as_micros() as f64 / 1000000f64;
let sent_total = (ok_count + failed_count) as f64;
let avg_time = (times.iter().sum::<u128>()
/ if sent_total == 0f64 { 1f64 } else { sent_total } as u128) as f32;
let avg_size = (data_len / if sent_total == 0f64 { 1f64 } else { sent_total }) as f64;

println!();
if min > 0f64 {
if used_min > 0f64 {
println!(
" 耗时: {:.2} s",
((stop - start).as_micros() as f64 / 1000000f64)
Expand All @@ -100,7 +105,7 @@ pub async fn format(t: u64, url: String, mut rx: mpsc::Receiver<(u32, u128, usiz
};
println!(
"请求/秒: {:.2} 次/秒",
sent_total as f64 / if min == 0f64 { 1f64 } else { min }
sent_total as f64 / if used_min == 0f64 { 1f64 } else { used_min }
);

println!(" 成功: {}", ok_count);
Expand Down

0 comments on commit f525701

Please sign in to comment.