Ensure that we don't exhaust memory while trying to generate packets

next
Sayan Nandan 2 years ago
parent 0f11dcfb88
commit 021579d4f8
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -56,6 +56,13 @@ macro_rules! binfo {
}; };
} }
#[inline(always)]
fn vec_with_cap<T>(cap: usize) -> BResult<Vec<T>> {
let mut v = Vec::new();
v.try_reserve_exact(cap)?;
Ok(v)
}
pub fn run_bench(servercfg: &ServerConfig, matches: ArgMatches) -> BResult<()> { pub fn run_bench(servercfg: &ServerConfig, matches: ArgMatches) -> BResult<()> {
// init bench config // init bench config
let bench_config = BenchmarkConfig::new(servercfg, matches)?; let bench_config = BenchmarkConfig::new(servercfg, matches)?;
@ -118,15 +125,16 @@ pub fn run_bench(servercfg: &ServerConfig, matches: ArgMatches) -> BResult<()> {
// such an approach helps us keep memory usage low // such an approach helps us keep memory usage low
// bench set // bench set
binfo!("Benchmarking SET ..."); binfo!("Benchmarking SET ...");
let set_packets: Vec<Box<[u8]>> = (0..bench_config.query_count()) let mut set_packets: Vec<Box<[u8]>> = vec_with_cap(bench_config.query_count())?;
.map(|i| { (0..bench_config.query_count()).for_each(|i| {
set_packets.push(
Query::from("SET") Query::from("SET")
.arg(RawString::from(keys[i].clone())) .arg(RawString::from(keys[i].clone()))
.arg(RawString::from(values[i].clone())) .arg(RawString::from(values[i].clone()))
.into_raw_query() .into_raw_query()
.into_boxed_slice() .into_boxed_slice(),
}) )
.collect(); });
run_bench_for( run_bench_for(
&pool_config, &pool_config,
move |sock, packet: Box<[u8]>| { move |sock, packet: Box<[u8]>| {
@ -150,15 +158,16 @@ pub fn run_bench(servercfg: &ServerConfig, matches: ArgMatches) -> BResult<()> {
// bench update // bench update
binfo!("Benchmarking UPDATE ..."); binfo!("Benchmarking UPDATE ...");
let update_packets: Vec<Box<[u8]>> = (0..bench_config.query_count()) let mut update_packets = vec_with_cap(bench_config.query_count())?;
.map(|i| { (0..bench_config.query_count()).for_each(|i| {
update_packets.push(
Query::from("UPDATE") Query::from("UPDATE")
.arg(RawString::from(keys[i].clone())) .arg(RawString::from(keys[i].clone()))
.arg(RawString::from(new_updated_key.clone())) .arg(RawString::from(new_updated_key.clone()))
.into_raw_query() .into_raw_query()
.into_boxed_slice() .into_boxed_slice(),
}) )
.collect(); });
run_bench_for( run_bench_for(
&pool_config, &pool_config,
move |sock, packet: Box<[u8]>| { move |sock, packet: Box<[u8]>| {
@ -178,14 +187,15 @@ pub fn run_bench(servercfg: &ServerConfig, matches: ArgMatches) -> BResult<()> {
// bench get // bench get
binfo!("Benchmarking GET ..."); binfo!("Benchmarking GET ...");
let get_packets: Vec<Box<[u8]>> = (0..bench_config.query_count()) let mut get_packets: Vec<Box<[u8]>> = vec_with_cap(bench_config.query_count())?;
.map(|i| { (0..bench_config.query_count()).for_each(|i| {
get_packets.push(
Query::from("GET") Query::from("GET")
.arg(RawString::from(keys[i].clone())) .arg(RawString::from(keys[i].clone()))
.into_raw_query() .into_raw_query()
.into_boxed_slice() .into_boxed_slice(),
}) )
.collect(); });
run_bench_for( run_bench_for(
&pool_config, &pool_config,
move |sock, packet: Box<[u8]>| { move |sock, packet: Box<[u8]>| {

Loading…
Cancel
Save