Skip to content

Commit 795b482

Browse files
committed
add more benchmarks
1 parent 414504c commit 795b482

File tree

1 file changed

+149
-4
lines changed

1 file changed

+149
-4
lines changed

benches/callgrind_bench.rs

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,184 @@ use iai_callgrind::{library_benchmark, library_benchmark_group, main};
66
use once_cell::sync::Lazy;
77
use tokio::runtime::Runtime;
88

9+
const SMALL_KEY_SIZE: usize = 10;
10+
const LARGE_KEY_SIZE: usize = 250; // Memcached's ~default maximum key size
11+
const SMALL_PAYLOAD_SIZE: usize = 128;
912
const LARGE_PAYLOAD_SIZE: usize = 1000 * 1024; // Memcached's ~default maximum payload size
1013

14+
const MULTI_KEY_VEC: &[&str] = &[
15+
"key1",
16+
"key2",
17+
"key3",
18+
];
19+
20+
const MULTI_KV_VEC: &[(&str, &str)] = &[
21+
("key1", "value1"),
22+
("key2", "value2"),
23+
("key3", "value3"),
24+
];
25+
26+
27+
1128
static CLIENT: Lazy<Mutex<Client>> = Lazy::new(|| {
1229
let rt = Runtime::new().unwrap();
1330
let client = rt.block_on(async {
1431
Client::new("tcp://127.0.0.1:11211")
1532
.await
1633
.expect("failed to create client")
1734
});
35+
1836
Mutex::new(client)
1937
});
2038

39+
// 'get' method benchmarks
2140
#[library_benchmark]
2241
async fn bench_get_small() {
2342
let mut client = CLIENT.lock().unwrap();
24-
let small_key = "foo";
43+
let small_key = "a".repeat(SMALL_KEY_SIZE);
2544

26-
let _ = black_box(client.get(small_key).await.unwrap());
45+
let _ = black_box(client.get(&small_key).await.unwrap());
2746
}
2847

2948
#[library_benchmark]
3049
async fn bench_get_large() {
3150
let mut client = CLIENT.lock().unwrap();
32-
let large_key = "a".repeat(230);
51+
let large_key = "a".repeat(LARGE_KEY_SIZE);
52+
53+
let _ = black_box(client.get(&large_key).await.unwrap());
54+
}
55+
56+
#[library_benchmark]
57+
async fn bench_get_multi_small() {
58+
let mut client = CLIENT.lock().unwrap();
59+
60+
let _ = black_box(client.get_multi(MULTI_KEY_VEC).await.unwrap());
61+
}
62+
63+
// 'set' method benchmarks
64+
#[library_benchmark]
65+
async fn bench_set_small() {
66+
let mut client = CLIENT.lock().unwrap();
67+
let small_key = "a".repeat(SMALL_KEY_SIZE);
68+
let small_payload = "b".repeat(SMALL_PAYLOAD_SIZE);
69+
70+
let _ = black_box(client.set(&small_key, &small_payload, None, None).await.unwrap());
71+
}
72+
73+
#[library_benchmark]
74+
async fn bench_set_large() {
75+
let mut client = CLIENT.lock().unwrap();
76+
let small_key = "a".repeat(SMALL_KEY_SIZE);
77+
let large_payload = "b".repeat(LARGE_PAYLOAD_SIZE);
78+
79+
let _ = black_box(client.set(&small_key, &large_payload, None, None).await.unwrap());
80+
}
81+
82+
#[library_benchmark]
83+
async fn bench_set_multi_small() {
84+
let mut client = CLIENT.lock().unwrap();
85+
86+
let _ = black_box(client.set_multi(MULTI_KV_VEC, None, None).await.unwrap());
87+
}
88+
89+
// 'add' method benchmarks
90+
#[library_benchmark]
91+
async fn bench_add_small() {
92+
let mut client = CLIENT.lock().unwrap();
93+
let small_key = "a".repeat(SMALL_KEY_SIZE);
94+
let small_payload = "b".repeat(SMALL_PAYLOAD_SIZE);
95+
96+
let _ = black_box(client.add(&small_key, &small_payload, None, None).await.unwrap());
97+
}
98+
99+
#[library_benchmark]
100+
async fn bench_add_large() {
101+
let mut client = CLIENT.lock().unwrap();
102+
let small_key = "a".repeat(SMALL_KEY_SIZE);
103+
let large_payload = "b".repeat(LARGE_PAYLOAD_SIZE);
104+
105+
let _ = black_box(client.add(&small_key, &large_payload, None, None).await.unwrap());
106+
}
107+
108+
#[library_benchmark]
109+
async fn bench_add_multi_small() {
110+
let mut client = CLIENT.lock().unwrap();
111+
112+
let _ = black_box(client.add_multi(MULTI_KV_VEC, None, None).await.unwrap());
113+
}
114+
115+
// 'delete' method benchmarks
116+
#[library_benchmark]
117+
async fn bench_delete() {
118+
let mut client = CLIENT.lock().unwrap();
119+
let small_key = "a".repeat(SMALL_KEY_SIZE);
120+
121+
let _ = black_box(client.delete(&small_key).await.unwrap());
122+
}
123+
124+
#[library_benchmark]
125+
async fn bench_delete_multi_no_reply_small() {
126+
let mut client = CLIENT.lock().unwrap();
127+
128+
let _ = black_box(client.delete_multi_no_reply(MULTI_KEY_VEC).await.unwrap());
129+
}
130+
131+
// 'increment' method benchmarks
132+
#[library_benchmark]
133+
async fn bench_increment() {
134+
let mut client = CLIENT.lock().unwrap();
135+
let small_key = "a".repeat(SMALL_KEY_SIZE);
136+
let _ = client.set(&small_key, 0_u64, None, None).await.unwrap();
137+
138+
let _ = black_box(client.increment(&small_key, 1).await.unwrap());
139+
}
140+
141+
#[library_benchmark]
142+
async fn bench_increment_no_reply() {
143+
let mut client = CLIENT.lock().unwrap();
144+
let small_key = "a".repeat(SMALL_KEY_SIZE);
145+
let _ = client.set(&small_key, 0_u64, None, None).await.unwrap();
146+
147+
let _ = black_box(client.increment_no_reply(&small_key, 1).await.unwrap());
148+
}
149+
150+
// 'decrement' method benchmarks
151+
#[library_benchmark]
152+
async fn bench_decrement() {
153+
let mut client = CLIENT.lock().unwrap();
154+
let small_key = "a".repeat(SMALL_KEY_SIZE);
155+
let _ = client.set(&small_key, 1000000_u64, None, None).await.unwrap();
156+
157+
let _ = black_box(client.decrement(&small_key, 1).await.unwrap());
158+
}
159+
160+
#[library_benchmark]
161+
async fn bench_decrement_no_reply() {
162+
let mut client = CLIENT.lock().unwrap();
163+
let small_key = "a".repeat(SMALL_KEY_SIZE);
164+
let _ = client.set(&small_key, 1000000_u64, None, None).await.unwrap();
33165

34-
let _ = black_box(client.get(large_key).await.unwrap());
166+
let _ = black_box(client.decrement_no_reply(&small_key, 1).await.unwrap());
35167
}
36168

37169
library_benchmark_group!(
38170
name = bench_cache_group;
39171
benchmarks =
40172
bench_get_small,
41173
bench_get_large,
174+
bench_get_multi_small,
175+
bench_set_small,
176+
bench_set_large,
177+
bench_set_multi_small,
178+
bench_add_small,
179+
bench_add_large,
180+
bench_add_multi_small,
181+
bench_delete,
182+
bench_delete_multi_no_reply_small,
183+
bench_increment,
184+
bench_increment_no_reply,
185+
bench_decrement,
186+
bench_decrement_no_reply,
42187
);
43188

44189
main!(library_benchmark_groups = bench_cache_group);

0 commit comments

Comments
 (0)