1
use criterion::measurement::WallTime;
2
use criterion::{BenchmarkGroup, BenchmarkId};
3
use ubyte::ToByteUnit;
4

            
5
4
pub fn read_blobs(c: &mut BenchmarkGroup<WallTime>, data: &[u8]) {
6
4
    let client = redis::Client::open("redis://localhost").unwrap();
7
4
    let mut connection = match client.get_connection() {
8
4
        Ok(connection) => connection,
9
        Err(err) => {
10
            eprintln!("Error connecting to redis: {err:?}");
11
            return;
12
        }
13
    };
14
    // The high level api would require allocations. However, by using the low
15
    // level API we can skip the clone.
16
4
    let data = std::str::from_utf8(data).unwrap();
17
4
    redis::cmd("SET")
18
4
        .arg("blob")
19
4
        .arg(data)
20
4
        .execute(&mut connection);
21
4
    c.bench_function(BenchmarkId::new("redis", data.len().bytes()), |b| {
22
4
        b.iter(|| {
23
4
            redis::cmd("GET")
24
4
                .arg("blob")
25
4
                .query::<Vec<u8>>(&mut connection)
26
4
                .unwrap();
27
4
        });
28
4
    });
29
4

            
30
4
    let runtime = tokio::runtime::Runtime::new().unwrap();
31
7
    let mut connection = runtime.block_on(async { client.get_async_connection().await.unwrap() });
32
4
    c.bench_function(BenchmarkId::new("redis-async", data.len().bytes()), |b| {
33
4
        b.iter(|| {
34
4
            runtime.block_on(async {
35
4
                redis::cmd("GET")
36
4
                    .arg("blob")
37
4
                    .query_async::<_, Vec<u8>>(&mut connection)
38
9
                    .await
39
4
                    .unwrap();
40
4
            })
41
4
        });
42
4
    });
43
4
}
44

            
45
4
pub fn write_blobs(c: &mut BenchmarkGroup<WallTime>, data: &[u8]) {
46
4
    let client = redis::Client::open("redis://localhost").unwrap();
47
4
    let mut connection = match client.get_connection() {
48
4
        Ok(connection) => connection,
49
        Err(err) => {
50
            eprintln!("Error connecting to redis: {err:?}");
51
            return;
52
        }
53
    };
54
    // Disable saving
55
4
    redis::cmd("CONFIG")
56
4
        .arg("SET")
57
4
        .arg("SAVE")
58
4
        .arg("")
59
4
        .execute(&mut connection);
60
4
    // The high level api would require allocations. However, by using the low
61
4
    // level API we can skip the clone.
62
4
    let data = std::str::from_utf8(data).unwrap();
63
4
    c.bench_function(BenchmarkId::new("redis", data.len().bytes()), |b| {
64
4
        b.iter(|| {
65
4
            redis::cmd("SET")
66
4
                .arg("blob")
67
4
                .arg(data)
68
4
                .execute(&mut connection);
69
4
        });
70
4
    });
71
4

            
72
4
    let runtime = tokio::runtime::Runtime::new().unwrap();
73
7
    let mut connection = runtime.block_on(async { client.get_async_connection().await.unwrap() });
74
4
    c.bench_function(BenchmarkId::new("redis-async", data.len().bytes()), |b| {
75
4
        b.iter(|| {
76
4
            runtime.block_on(async {
77
4
                redis::cmd("SET")
78
4
                    .arg("blob")
79
4
                    .arg(data)
80
4
                    .query_async::<_, ()>(&mut connection)
81
9
                    .await
82
4
                    .unwrap();
83
4
            })
84
4
        });
85
4
    });
86
4
}
87

            
88
1
pub fn increment(c: &mut BenchmarkGroup<WallTime>) {
89
1
    let client = redis::Client::open("redis://localhost").unwrap();
90
1
    let mut connection = match client.get_connection() {
91
1
        Ok(connection) => connection,
92
        Err(err) => {
93
            eprintln!("Error connecting to redis: {err:?}");
94
            return;
95
        }
96
    };
97
    // Disable saving
98
1
    redis::cmd("CONFIG")
99
1
        .arg("SET")
100
1
        .arg("SAVE")
101
1
        .arg("")
102
1
        .execute(&mut connection);
103
1
    c.bench_function("redis", |b| {
104
1
        b.iter(|| {
105
1
            redis::cmd("INCR").arg("u64").execute(&mut connection);
106
1
        });
107
1
    });
108
1

            
109
1
    let runtime = tokio::runtime::Runtime::new().unwrap();
110
2
    let mut connection = runtime.block_on(async { client.get_async_connection().await.unwrap() });
111
1
    c.bench_function("redis-async", |b| {
112
1
        b.iter(|| {
113
1
            runtime.block_on(async {
114
1
                redis::cmd("INCR")
115
1
                    .arg("u64")
116
1
                    .query_async::<_, ()>(&mut connection)
117
2
                    .await
118
1
                    .unwrap();
119
1
            })
120
1
        });
121
1
    });
122
1
}