init
This commit is contained in:
commit
53faef0e96
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1068
Cargo.lock
generated
Normal file
1068
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "web_test"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||||
83
src/main.rs
Normal file
83
src/main.rs
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
use reqwest;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
pub struct IncrementalStats {
|
||||||
|
count: u64,
|
||||||
|
mean: f64,
|
||||||
|
m2: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IncrementalStats {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
IncrementalStats {
|
||||||
|
count: 0,
|
||||||
|
mean: 0.0,
|
||||||
|
m2: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add(&mut self, value: f64) {
|
||||||
|
self.count += 1;
|
||||||
|
let delta = value - self.mean;
|
||||||
|
self.mean += delta / self.count as f64;
|
||||||
|
let delta2 = value - self.mean;
|
||||||
|
self.m2 += delta * delta2;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn average(&self) -> f64 {
|
||||||
|
self.mean
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn variance(&self) -> f64 {
|
||||||
|
if self.count < 2 {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
self.m2 / self.count as f64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let url = std::env::args()
|
||||||
|
.nth(1)
|
||||||
|
.unwrap_or("https://www.youtube.com".to_owned()); // 你可以替换为你想要测量的网址
|
||||||
|
// let mut time_rec = Vec::new();
|
||||||
|
let iter_num: usize = std::env::args()
|
||||||
|
.nth(2)
|
||||||
|
.unwrap_or_else(|| "20".to_owned())
|
||||||
|
.parse()
|
||||||
|
.expect("second parameter should interger");
|
||||||
|
let mut stats = IncrementalStats::new();
|
||||||
|
for i in 0..iter_num {
|
||||||
|
let start = Instant::now();
|
||||||
|
match reqwest::blocking::get(&url) {
|
||||||
|
Ok(response) => {
|
||||||
|
if response.status().is_success() {
|
||||||
|
let duration = start.elapsed();
|
||||||
|
let r = response.bytes().unwrap();
|
||||||
|
println!(
|
||||||
|
"第{i}次测试, 访问 {} 花费了 {:?} 毫秒",
|
||||||
|
url,
|
||||||
|
duration.as_millis()
|
||||||
|
);
|
||||||
|
// time_rec.push(duration);
|
||||||
|
stats.add(duration.as_millis() as f64);
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"第{i}次测试, 访问 {} 失败,状态码:{}",
|
||||||
|
url,
|
||||||
|
response.status()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
println!("第{i}次测试, 请求错误:{}", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!(
|
||||||
|
"平均: {:?}ms, 方差: {:?}",
|
||||||
|
stats.average(),
|
||||||
|
stats.variance()
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user