|
| 1 | +use criterion::{Criterion, criterion_group, criterion_main}; |
| 2 | +use std::hint::black_box; |
| 3 | +use tokio::runtime::Runtime; |
| 4 | + |
| 5 | +use fred::prelude::*; // fred |
| 6 | +use redis::{AsyncCommands, aio::MultiplexedConnection}; // redis-rs |
| 7 | +use rustis::{ |
| 8 | + client::Client as RustisClient, |
| 9 | + commands::{GenericCommands, ListCommands}, |
| 10 | +}; // rustis |
| 11 | + |
| 12 | +const KEY: &str = "large_list"; |
| 13 | + |
| 14 | +async fn setup_data(client: &RustisClient) { |
| 15 | + // Cleanup |
| 16 | + let _: usize = client.del(KEY).await.unwrap(); |
| 17 | + |
| 18 | + // prepare 500 strings of around 100 bytes each to force parser to work on volume |
| 19 | + let payloads: Vec<String> = (0..5000) |
| 20 | + .map(|i| { |
| 21 | + format!( |
| 22 | + "user_session_data_buffer_overflow_protection_check_{:05}", |
| 23 | + i |
| 24 | + ) |
| 25 | + }) |
| 26 | + .collect(); |
| 27 | + |
| 28 | + client.rpush(KEY, payloads).await.unwrap(); |
| 29 | +} |
| 30 | + |
| 31 | +async fn bench_rustis(client: RustisClient) { |
| 32 | + let _: Vec<String> = black_box(client.lrange(KEY, 0, -1).await.unwrap()); |
| 33 | +} |
| 34 | + |
| 35 | +async fn bench_fred(client: fred::clients::Client) { |
| 36 | + let _: Vec<String> = black_box(client.lrange(KEY, 0, -1).await.unwrap()); |
| 37 | +} |
| 38 | + |
| 39 | +async fn bench_redis_rs(mut conn: MultiplexedConnection) { |
| 40 | + let _: Vec<String> = black_box(conn.lrange(KEY, 0, -1).await.unwrap()); |
| 41 | +} |
| 42 | + |
| 43 | +fn compare_drivers(c: &mut Criterion) { |
| 44 | + let rt = Runtime::new().unwrap(); |
| 45 | + let redis_host = std::env::var("REDIS_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()); |
| 46 | + |
| 47 | + // Initialisation des clients (Setup) |
| 48 | + let (rustis, fred, redis_rs) = rt.block_on(async { |
| 49 | + // rustis |
| 50 | + let rustis = RustisClient::connect(redis_host.clone()).await.unwrap(); |
| 51 | + setup_data(&rustis).await; |
| 52 | + // fred |
| 53 | + let config = Config::from_url(&format!("redis://{redis_host}:6379/0")).unwrap(); |
| 54 | + let fred = Builder::from_config(config).build().unwrap(); |
| 55 | + fred.init().await.unwrap(); |
| 56 | + // redis-rs |
| 57 | + let redis_rs = redis::Client::open(format!("redis://{redis_host}:6379")).unwrap(); |
| 58 | + let redis_rs = redis_rs.get_multiplexed_async_connection().await.unwrap(); |
| 59 | + |
| 60 | + (rustis, fred, redis_rs) |
| 61 | + }); |
| 62 | + |
| 63 | + let mut group = c.benchmark_group("Large Array Parsing Comparison"); |
| 64 | + |
| 65 | + group.bench_function("rustis", |b| { |
| 66 | + b.to_async(&rt).iter(|| bench_rustis(rustis.clone())); |
| 67 | + }); |
| 68 | + |
| 69 | + group.bench_function("fred", |b| { |
| 70 | + b.to_async(&rt).iter(|| bench_fred(fred.clone())); |
| 71 | + }); |
| 72 | + |
| 73 | + group.bench_function("redis-rs", |b| { |
| 74 | + b.to_async(&rt).iter(|| bench_redis_rs(redis_rs.clone())); |
| 75 | + }); |
| 76 | + |
| 77 | + group.finish(); |
| 78 | +} |
| 79 | + |
| 80 | +criterion_group!(benches, compare_drivers); |
| 81 | +criterion_main!(benches); |
0 commit comments