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

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

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

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

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

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

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