Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asynchronous rust异步程序中的性能和内存问题_Asynchronous_Rust_Future_Rust Tokio - Fatal编程技术网

Asynchronous rust异步程序中的性能和内存问题

Asynchronous rust异步程序中的性能和内存问题,asynchronous,rust,future,rust-tokio,Asynchronous,Rust,Future,Rust Tokio,我想使用异步客户机对从rust到特定服务的请求进行基准测试,并为此创建了异步基准测试程序 此函数应在指定的持续时间内运行指定数量的并发线程(实际上是并行的未来链),并报告已实现的迭代次数 使用期货::未来; 使用期货::前奏::*; 使用std::error::error; 使用std::time::{Duration,Instant}; 使用std::{cell,io,rc}; 使用tokio::runtime::current_thread::runtime; 使用tokio::定时器; 结

我想使用异步客户机对从rust到特定服务的请求进行基准测试,并为此创建了异步基准测试程序

此函数应在指定的持续时间内运行指定数量的并发线程(实际上是并行的未来链),并报告已实现的迭代次数

使用期货::未来;
使用期货::前奏::*;
使用std::error::error;
使用std::time::{Duration,Instant};
使用std::{cell,io,rc};
使用tokio::runtime::current_thread::runtime;
使用tokio::定时器;
结构配置{
工人:u32,
持续时间:持续时间,
}
///构建无限重复的未来
fn循环{
框::新建(f(),然后(移动| |循环(f)))
}

fn benchmark看起来,
循环
返回的
直到
benchmark
结束时才被释放,内存分配/释放需要越来越多的时间

我已经用
async_wait
重写了您的程序,没有使用
,结果现在是一致的:

#![功能(异步等待)]
使用未来:{compat::Future01CompatExt,future,prelude::*,select};
使用std::sync::atomic::{AtomicUsize,Ordering};
使用std::time::{Duration,Instant};
使用tokio::定时器;
结构配置{
工人:u32,
持续时间:持续时间,
}
//构建无限重复的未来
异步fn周期P+'a,P:Future+'a>(
config:config,
f:f,
)->使用{
让计数器=原子化::新建(0);
让无限_counter=future::select_all((0..config.workers).map(| |{
循环{
f().map(| |{
计数器。获取_添加(1,订购::SeqCst);
})
})
.boxed_local()
}));
让timer=timer::Delay::new(Instant::now()+config.duration)
.compat()
.打开你或其他东西(| | | |恐慌!(“砰!”);
选择{
a=无穷大计数器。保险丝()=>(),
b=计时器。保险丝()=>(),
};
计数器加载(顺序::SeqCst)
}
fn main(){
let duration=std::env::args()
.skip(1)
.next()
.expect(“请以秒为单位提供持续时间”)
.parse()
.expect(“持续时间必须是整数”);
设ms=Duration::from_millis(1);
//使用actix\u rt运行时而不是vanilla tokio,因为我想要
//限制为一个OS线程并避免需要异步原语
让mut rt=actix_rt::Runtime::new().expect(“无法创建运行时”);;
环路{
设iters=rt
.封锁(
基准(
配置{
工人:65536,
持续时间:持续时间::从秒开始(持续时间),
},
|| {
//替换实际的基准测试调用
计时器::延迟::新建(即时::现在()+ms)
.compat()
.打开你或其他东西(| | |恐慌!(“砰!”)
},
)
.boxed_local()
.unit_错误()
.compat(),
)
.expect(“运行时错误”);
println!(“{}iters/秒”,iters为u64/时长);
}
}
这是我第一次使用futures 0.3,所以我没有得到像
select这样的部分语法,或是
盒装的\u 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"

看起来,
循环
返回的
直到
基准
结束时才被释放,内存分配/释放需要越来越多的时间

我已经用
async_wait
重写了您的程序,没有使用
,结果现在是一致的:

#![功能(异步等待)]
使用未来:{compat::Future01CompatExt,future,prelude::*,select};
使用std::sync::atomic::{AtomicUsize,Ordering};
使用std::time::{Duration,Instant};
使用tokio::定时器;
结构配置{
工人:u32,
持续时间:持续时间,
}
//构建无限重复的未来
异步fn周期P+'a,P:Future+'a>(
config:config,
f:f,
)->使用{
让计数器=原子化::新建(0);
让无限_counter=future::select_all((0..config.workers).map(| |{
循环{
f().map(| |{
计数器。获取_添加(1,订购::SeqCst);
})
})
.boxed_local()
}));
让timer=timer::Delay::new(Instant::now()+config.duration)
.compat()
.打开你或其他东西(| | | |恐慌!(“砰!”);
选择{
a=无穷大计数器。保险丝()=>(),
b=计时器。保险丝()=>(),
};
计数器加载(顺序::SeqCst)
}
fn main(){
let duration=std::env::args()
.skip(1)
.next()
.expect(“请以秒为单位提供持续时间”)
.parse()
.expect(“持续时间必须是整数”);
设ms=Duration::from_millis(1);
//使用actix\u rt运行时而不是vanilla tokio,因为我想要
//限制为一个OS线程并避免需要异步原语
让mut rt=actix_rt::Runtime::new().expect(“无法创建运行时”);;
环路{
设iters=rt
.封锁(
基准(
配置{
工人:65536,
持续时间:持续时间::从秒开始(持续时间),
},
|| {
//替换实际的基准测试调用
计时器::延迟::新建(即时::现在()+ms)
.compat()
.打开你或其他东西(| | |恐慌!(“砰!”)
},
)
.boxed_local()
.unit_错误()
.compat(),
)
.expect(“运行时错误”);
println!(“{}iters/秒”,iters为u64/时长);
}
}
这是我第一次使用futures 0.3,所以我没有得到像
select这样的部分synt