/
cassandra.rs
378 lines (345 loc) · 12.8 KB
/
cassandra.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use crate::helpers::cassandra::{CassandraConnection, CassandraDriver};
use crate::helpers::ShotoverManager;
use cassandra_cpp::{stmt, Session, Statement};
use criterion::{criterion_group, criterion_main, Criterion};
use std::collections::HashMap;
use test_helpers::cert::generate_cassandra_test_certs;
use test_helpers::docker_compose::DockerCompose;
use test_helpers::lazy::new_lazy_shared;
#[cfg(feature = "alpha-transforms")]
struct Query {
name: &'static str,
statement: Statement,
}
const DRIVER: CassandraDriver = CassandraDriver::Datastax;
fn cassandra(c: &mut Criterion) {
let mut group = c.benchmark_group("cassandra");
group.throughput(criterion::Throughput::Elements(1));
group.noise_threshold(0.2);
let queries = ["insert", "select", "execute"];
// Benches the case where the message does not meet the criteria for encryption
#[cfg(feature = "alpha-transforms")]
{
let resources = new_lazy_shared(|| {
BenchResources::new(
"example-configs/cassandra-protect-local/topology.yaml",
"example-configs/cassandra-protect-local/docker-compose.yaml",
)
});
for query in &queries {
group.bench_with_input(
format!("protect_local_{}_unencrypted", query),
&resources,
|b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection
.execute(resources.as_ref().unwrap().get(query))
.wait()
.unwrap();
})
},
);
}
}
{
let resources = new_lazy_shared(|| {
BenchResources::new(
"example-configs/cassandra-redis-cache/topology.yaml",
"example-configs/cassandra-redis-cache/docker-compose.yaml",
)
});
// Benches the case where the message does not meet the criteria for caching
for query in &queries {
group.bench_with_input(
format!("redis_cache_{}_uncached", query),
&resources,
|b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection
.execute(resources.as_ref().unwrap().get(query))
.wait()
.unwrap();
})
},
);
}
}
{
let resources = new_lazy_shared(|| {
BenchResources::new(
"example-configs/cassandra-passthrough/topology.yaml",
"example-configs/cassandra-passthrough/docker-compose.yaml",
)
});
for query in &queries {
group.bench_with_input(
format!("passthrough_no_parse_{}", query),
&resources,
|b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection
.execute(resources.as_ref().unwrap().get(query))
.wait()
.unwrap();
})
},
);
}
}
#[cfg(feature = "alpha-transforms")]
{
let resources = new_lazy_shared(|| {
BenchResources::new(
"tests/test-configs/cassandra-passthrough-parse-request/topology.yaml",
"tests/test-configs/cassandra-passthrough-parse-request/docker-compose.yaml",
)
});
for query in &queries {
group.bench_with_input(
format!("passthrough_parse_request_{}", query),
&resources,
|b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection
.execute(resources.as_ref().unwrap().get(query))
.wait()
.unwrap();
})
},
);
}
}
#[cfg(feature = "alpha-transforms")]
{
let resources = new_lazy_shared(|| {
BenchResources::new(
"tests/test-configs/cassandra-passthrough-parse-response/topology.yaml",
"tests/test-configs/cassandra-passthrough-parse-response/docker-compose.yaml",
)
});
for query in &queries {
group.bench_with_input(
format!("passthrough_parse_response_{}", query),
&resources,
|b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection
.execute(resources.as_ref().unwrap().get(query))
.wait()
.unwrap();
})
},
);
}
}
{
let resources = new_lazy_shared(|| {
BenchResources::new_tls(
"example-configs/cassandra-tls/topology.yaml",
"example-configs/cassandra-tls/docker-compose.yaml",
)
});
for query in &queries {
group.bench_with_input(format!("tls_{}", query), &resources, |b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection
.execute(resources.as_ref().unwrap().get(query))
.wait()
.unwrap();
})
});
}
}
#[cfg(feature = "alpha-transforms")]
{
let queries = [
Query {
name: "insert",
statement: stmt!("INSERT INTO test_protect_keyspace.test_table (pk, cluster, col1, col2, col3) VALUES ('pk1', 'cluster', 'I am gonna get encrypted!!', 42, true);"),
},
Query {
name: "select",
statement: stmt!("SELECT pk, cluster, col1, col2, col3 FROM test_protect_keyspace.test_table"),
},
];
let resources = new_lazy_shared(|| {
let resources = BenchResources::new(
"example-configs/cassandra-protect-local/topology.yaml",
"example-configs/cassandra-protect-local/docker-compose.yaml",
);
resources
.get_connection()
.execute(&stmt!(
"CREATE KEYSPACE test_protect_keyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"
))
.wait()
.unwrap();
resources
.get_connection()
.execute(&stmt!(
"CREATE TABLE test_protect_keyspace.test_table (pk varchar PRIMARY KEY, cluster varchar, col1 blob, col2 int, col3 boolean);"
))
.wait()
.unwrap();
resources
.get_connection()
.execute(&stmt!(
"INSERT INTO test_protect_keyspace.test_table (pk, cluster, col1, col2, col3) VALUES ('pk1', 'cluster', 'Initial value', 42, true);"
))
.wait()
.unwrap();
resources
});
for query in queries {
// Benches the case where the message meets the criteria for encryption
group.bench_with_input(
format!("protect_local_{}_encrypted", query.name),
&resources,
|b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection.execute(&query.statement).wait().unwrap();
})
},
);
}
}
{
let resources = new_lazy_shared(|| {
BenchResources::new(
"example-configs/cassandra-request-throttling/topology.yaml",
"example-configs/cassandra-request-throttling/docker-compose.yaml",
)
});
for query in &queries {
group.bench_with_input(
format!("request_throttling_{}", query),
&resources,
|b, resources| {
b.iter(|| {
let resources = resources.borrow();
let connection = resources.as_ref().unwrap().get_connection();
connection
.execute(resources.as_ref().unwrap().get(query))
.wait()
.unwrap();
})
},
);
}
}
}
criterion_group!(benches, cassandra);
criterion_main!(benches);
pub struct BenchResources {
connection: CassandraConnection,
_shotover_manager: ShotoverManager,
_compose: DockerCompose,
queries: HashMap<String, Statement>,
}
impl BenchResources {
fn new(shotover_topology: &str, compose_file: &str) -> Self {
let compose = DockerCompose::new(compose_file);
let shotover_manager = ShotoverManager::from_topology_file(shotover_topology);
let connection =
futures::executor::block_on(CassandraConnection::new("127.0.0.1", 9042, DRIVER));
let mut bench_resources = Self {
_compose: compose,
_shotover_manager: shotover_manager,
connection,
queries: HashMap::new(),
};
bench_resources.setup();
bench_resources
}
pub fn get_connection(&self) -> &Session {
self.connection.as_datastax()
}
fn new_tls(shotover_topology: &str, compose_file: &str) -> Self {
generate_cassandra_test_certs();
let compose = DockerCompose::new(compose_file);
let shotover_manager = ShotoverManager::from_topology_file(shotover_topology);
let ca_cert = "example-configs/docker-images/cassandra-tls-4.0.6/certs/localhost_CA.crt";
let connection = futures::executor::block_on(CassandraConnection::new_tls(
"127.0.0.1",
9042,
ca_cert,
DRIVER,
));
let mut bench_resources = Self {
_compose: compose,
_shotover_manager: shotover_manager,
connection,
queries: HashMap::new(),
};
bench_resources.setup();
bench_resources
}
pub fn get(&self, query: &str) -> &Statement {
self.queries.get(query).unwrap()
}
fn setup(&mut self) {
let create_keyspace = stmt!(
"CREATE KEYSPACE benchmark_keyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"
);
let create_table = stmt!(
"CREATE TABLE benchmark_keyspace.table_1 (id int PRIMARY KEY, x int, name varchar);"
);
let insert = stmt!(
"INSERT INTO benchmark_keyspace.table_1 (id, x, name) VALUES (0, 10, 'initial value');"
);
self.connection
.as_datastax()
.execute(&create_keyspace)
.wait()
.unwrap();
self.connection
.as_datastax()
.execute(&create_table)
.wait()
.unwrap();
self.connection
.as_datastax()
.execute(&insert)
.wait()
.unwrap();
let prepared_statement = self
.connection
.as_datastax()
.prepare(
"INSERT INTO benchmark_keyspace.table_1 (id, x, name) VALUES (0, 10, 'new_value');",
)
.unwrap()
.wait()
.unwrap()
.bind();
self.queries = [
(
"insert".into(),
stmt!(
"INSERT INTO benchmark_keyspace.table_1 (id, x, name) VALUES (1, 11, 'foo');"
),
),
(
"select".into(),
stmt!("SELECT id, x, name FROM benchmark_keyspace.table_1;"),
),
("execute".into(), prepared_statement),
]
.into_iter()
.collect();
}
}