我希望使用异步客户端对从铁锈到特定服务的请求进行基准测试,并为此创建了异步基准测试标记。
此函数应该在指定的持续时间内运行指定数量的并发线程(实际上是并行的期货链),并报告实现的迭代计数。
use futures::future;
use futures::prelude::*;
use std::error::Error;
use std::time::{Duration, Instant};
use std::{cell, io, rc};
use tokio::runtime::current_thread::Runtime;
use tokio::timer;
struct Config {
workers: u32,
duration: Duration,
}
/// Build infinitely repeating future
fn cycle<'a, F: Fn() -> P + 'a, P: Future + 'a>(
f: F,
) -> Box<dyn Future<Item = (), Error = P::Error> + 'a> {
Box::new(f().and_then(move |_| cycle(f)))
}
fn benchmark<'a, F: Fn() -> P + 'a, P: Future<Error = io::Error> + 'a>(
config: Config,
f: F,
) -> impl Future<Item = u32, Error = io::Error> + 'a {
let counter = rc::Rc::new(cell::Cell::new(0u32));
let f = rc::Rc::new(f);
future::select_all((0..config.workers).map({
let counter = rc::Rc::clone(&counter);
move |_| {
let counter = rc::Rc::clone(&counter);
let f = rc::Rc::clone(&f);
cycle(move || {
let counter = rc::Rc::clone(&counter);
f().map(move |_| {
counter.set(counter.get() + 1);
})
})
}
}))
.map(|((), _, _)| ())
.map_err(|(err, _, _)| err)
.select(
timer::Delay::new(Instant::now() + config.duration)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.description())),
)
.map(move |((), _)| counter.get())
.map_err(|(err, _)| err)
}
fn main() {
let duration = std::env::args()
.skip(1)
.next()
.expect("Please provide duration in seconds")
.parse()
.expect("Duration must be integer number");
let ms = Duration::from_millis(1);
let mut rt = Runtime::new().expect("Could not create runtime");
loop {
let iters = rt
.block_on(
benchmark(
Config {
workers: 65536,
duration: Duration::from_secs(duration),
},
|| {
/// Substitute actual benchmarked call
timer::Delay::new(Instant::now() + ms)
.map_err(|err| panic!("Failed to set delay: {:?}", err))
},
)
.map_err(|err| panic!("Benchamrking error: {:?}", err)),
)
.expect("Runtime error");
println!("{} iters/sec", iters as u64 / duration);
}
}然而,这个基准测试报告和内存消耗随着基准持续时间的增加而下降,例如在我的pc上:
cargo run --release 1 ~900 k迭代/秒
cargo run --release 2 ~700 k迭代/秒
cargo run --release 10 ~330 k迭代/秒
此外,随着基准函数的运行,内存使用量也会迅速增长。我尝试使用valgrind查找内存泄漏,但它只报告仍然可以到达所有分配的内存。
我怎样才能解决这个问题?
发布于 2019-07-11 20:14:58
因此,事实证明cycle确实是格雷戈里怀疑的罪魁祸首。我在期货箱:fn中找到了这个有用的函数,并使用它重写了cycle:
/// Build infinitely repeating future
fn cycle<'a, F: Fn() -> P + 'a, P: Future + 'a>(
f: F,
) -> impl Future<Item = (), Error = P::Error> + 'a {
future::loop_fn((), move |_| f().map(|_| future::Loop::Continue(())))
}其余代码保持不变。现在,它以稳定的锈蚀方式编译,甚至每秒报告的迭代次数几乎是建议的夜间未来解决方案的两倍(因为这个合成测试的价值)。
发布于 2019-07-11 14:08:22
看起来,直到Box结束时,cycle返回的benchmark才会被释放,并且内存分配/去分配需要越来越多的时间。
我用async_await重写了您的程序,没有Box,结果现在是一致的:
#![feature(async_await)]
use futures::{compat::Future01CompatExt, future, prelude::*, select};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use tokio::timer;
struct Config {
workers: u32,
duration: Duration,
}
// Build infinitely repeating future
async fn cycle<'a, F: Fn() -> P + 'a, P: Future<Output = ()> + 'a>(f: F) {
loop {
f().await;
}
}
async fn benchmark<'a, F: Fn() -> P + 'a, P: Future<Output = ()> + 'a>(
config: Config,
f: F,
) -> usize {
let counter = AtomicUsize::new(0);
let infinite_counter = future::select_all((0..config.workers).map(|_| {
cycle(|| {
f().map(|_| {
counter.fetch_add(1, Ordering::SeqCst);
})
})
.boxed_local()
}));
let timer = timer::Delay::new(Instant::now() + config.duration)
.compat()
.unwrap_or_else(|_| panic!("Boom !"));
select! {
a = infinite_counter.fuse() => (),
b = timer.fuse() => (),
};
counter.load(Ordering::SeqCst)
}
fn main() {
let duration = std::env::args()
.skip(1)
.next()
.expect("Please provide duration in seconds")
.parse()
.expect("Duration must be integer number");
let ms = Duration::from_millis(1);
// Use actix_rt runtime instead of vanilla tokio because I want
// to restrict to one OS thread and avoid needing async primitives
let mut rt = actix_rt::Runtime::new().expect("Could not create runtime");;
loop {
let iters = rt
.block_on(
benchmark(
Config {
workers: 65536,
duration: Duration::from_secs(duration),
},
|| {
// Substitute actual benchmarked call
timer::Delay::new(Instant::now() + ms)
.compat()
.unwrap_or_else(|_| panic!("Boom !"))
},
)
.boxed_local()
.unit_error()
.compat(),
)
.expect("Runtime error");
println!("{} iters/sec", iters as u64 / duration);
}
}这是我第一次使用期货0.3,所以我没有真正得到一些部分,如select!语法,或boxed_local,但它有效!
编辑:下面是来自Cargo.toml的依赖项块
[dependencies]
futures-preview = { version = "0.3.0-alpha", features = ["nightly", "compat", "async-await"] }
tokio = "0.1.22"
actix-rt = "0.2.3"https://stackoverflow.com/questions/56979727
复制相似问题