You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wrote a casual benchmark program with redis-rs and bincode (version = "2.0.0-rc1", features=["serde"]), and this is what I got:
on debug mode:
on release mode:
The conclusion could be made that using bincode has a slight edge over storing structures in HASH and manually parsing them into fields. Considering the memory occupation, bincode is preferred.
source code
// src/main.rs#![feature(bench_black_box)]use std::hint::black_box;use eyre::Result;use playground::{Metadata,Mode};use redis::{aio::Connection,AsyncCommands,Client};use time::format_description::well_known::Rfc3339;use tracing::info;use tracing_subscriber::FmtSubscriber;constHASH:&str = "hash";constBIN:&str = "bin";constITERATIONS:usize = 100000;asyncfnhash(con:&mutConnection) -> Metadata{let(mode, last_modified, content_length, content_md5):(String,String,String,String) = con
.hget(HASH,&["mode","last_modified","content_length","content_md5",]).await.unwrap();let mode = Mode::try_from(mode).unwrap();let last_modified = time::OffsetDateTime::parse(last_modified.as_str(),&Rfc3339).unwrap();let content_length:u64 = content_length.parse().unwrap();Metadata{
mode,
last_modified,
content_length,
content_md5,}}asyncfnbincode(con:&mutConnection) -> Metadata{let config = bincode::config::standard();let bytes:Vec<u8> = con.get(BIN).await.unwrap();let(m, _) = bincode::serde::decode_from_slice(bytes.as_slice(), config).unwrap();
m
}#[tokio::main]asyncfnmain() -> Result<()>{// a builder for `FmtSubscriber`.let subscriber = FmtSubscriber::builder().finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");info!("begin init!");let client = Client::open("redis://localhost:6379")?;letmut con = client.get_async_connection().await?;let mode = Mode::File;let last_modified = time::OffsetDateTime::now_utc();let content_length = 32;let content_md5 = "1231sdakfljasdlfsj".to_string();let metadata = Metadata{
mode,
last_modified,
content_length,content_md5: content_md5.clone(),};let config = bincode::config::standard();let lm = last_modified.format(&Rfc3339)?;info!("last modified time is {}", lm);let content_length = content_length.to_string();let fields = [("mode",b"file".as_slice()),("last_modified", lm.as_bytes()),("content_length", content_length.as_bytes()),("content_md5", content_md5.as_bytes()),];
con.hset_multiple(HASH,&fields).await?;let slice = bincode::serde::encode_to_vec(metadata.clone(), config)?;
con.set(BIN, slice).await?;info!("inited!");info!("test correctness");assert_eq!(hash(&mut con).await, metadata);assert_eq!(bincode(&mut con).await, metadata);info!("all correct!");asyncfntest1() -> Result<()>{let t1 = time::Instant::now();let client = Client::open("redis://localhost:6379")?;letmut con = client.get_async_connection().await?;info!("begin reading {} times from hash", ITERATIONS);for _ in0..ITERATIONS{black_box(hash(&mut con).await);}info!("reading from hash for {} times elaspted to {}s",
ITERATIONS,
t1.elapsed().as_seconds_f64());Ok(())}asyncfntest2() -> Result<()>{let t1 = time::Instant::now();let client = Client::open("redis://localhost:6379")?;letmut con = client.get_async_connection().await?;info!("begin reading {} times from bincode", ITERATIONS);for _ in0..ITERATIONS{black_box(bincode(&mut con).await);}info!("reading from bincode for {} times elaspted to {}s",
ITERATIONS,
t1.elapsed().as_seconds_f64());Ok(())}let p1 = tokio::spawn(test1());let p2 = tokio::spawn(test2());let(p1, p2) = tokio::join!(p1, p2);
p1??;
p2??;Ok(())}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I wrote a casual benchmark program with
redis-rs
andbincode (version = "2.0.0-rc1", features=["serde"])
, and this is what I got:on debug mode:
on release mode:
The conclusion could be made that using
bincode
has a slight edge over storing structures in HASH and manually parsing them into fields. Considering the memory occupation,bincode
is preferred.source code
Beta Was this translation helpful? Give feedback.
All reactions