forked from cornucopia-rs/cornucopia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_benches.rs
207 lines (179 loc) · 5.57 KB
/
postgres_benches.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
use criterion::Bencher;
use postgres::types::ToSql;
use postgres::{fallible_iterator::FallibleIterator, Client};
use std::collections::HashMap;
use std::fmt::Write;
const NO_PARAMS: Vec<&dyn ToSql> = Vec::new();
pub struct User {
pub id: i32,
pub name: String,
pub hair_color: Option<String>,
}
pub struct Post {
pub id: i32,
pub user_id: i32,
pub title: String,
pub body: Option<String>,
}
pub struct Comment {
pub id: i32,
pub post_id: i32,
pub text: String,
}
pub fn bench_trivial_query(b: &mut Bencher, client: &mut Client) {
let query = client
.prepare("SELECT id, name, hair_color FROM users")
.unwrap();
b.iter(|| {
client
.query_raw(&query, NO_PARAMS)
.unwrap()
.map(|row| {
Ok(User {
id: row.get(0),
name: row.get(1),
hair_color: row.get(2),
})
})
.collect::<Vec<_>>()
.unwrap()
})
}
pub fn bench_medium_complex_query(b: &mut Bencher, client: &mut Client) {
let query = client
.prepare(
"SELECT u.id, u.name, u.hair_color, p.id, p.user_id, p.title, p.body \
FROM users as u LEFT JOIN posts as p on u.id = p.user_id",
)
.unwrap();
b.iter(|| {
client
.query_raw(&query, NO_PARAMS)
.unwrap()
.map(|row| {
let user = User {
id: row.get(0),
name: row.get(1),
hair_color: row.get(2),
};
let post = row.get::<_, Option<i32>>(3).map(|id| Post {
id,
user_id: row.get(4),
title: row.get(5),
body: row.get(6),
});
Ok((user, post))
})
.collect::<Vec<_>>()
.unwrap()
})
}
pub fn bench_insert(b: &mut Bencher, client: &mut Client, size: usize) {
b.iter(|| {
let mut query = String::from("INSERT INTO users (name, hair_color) VALUES");
let mut params = Vec::with_capacity(2 * size);
for x in 0..size {
write!(
query,
"{} (${}, ${})",
if x == 0 { "" } else { "," },
2 * x + 1,
2 * x + 2
)
.unwrap();
params.push((format!("User {x}"), Some("hair_color")));
}
let params = params
.iter()
.flat_map(|(a, b)| [a as _, b as _])
.collect::<Vec<_>>();
client.execute(&query as &str, ¶ms).unwrap();
})
}
pub fn loading_associations_sequentially(b: &mut Bencher, client: &mut Client) {
let user_query = client
.prepare("SELECT id, name, hair_color FROM users")
.unwrap();
b.iter(|| {
let users = client
.query_raw(&user_query, NO_PARAMS)
.unwrap()
.map(|row| {
Ok(User {
id: row.get("id"),
name: row.get("name"),
hair_color: row.get("hair_color"),
})
})
.collect::<Vec<_>>()
.unwrap();
let mut posts_query =
String::from("SELECT id, title, user_id, body FROM posts WHERE user_id IN(");
let user_ids = users
.iter()
.enumerate()
.map(|(i, &User { id, .. })| {
posts_query += &format!("{}${}", if i == 0 { "" } else { "," }, i + 1);
id
})
.collect::<Vec<i32>>();
posts_query += ")";
let posts = client
.query_raw(&posts_query as &str, user_ids)
.unwrap()
.map(|row| {
Ok(Post {
id: row.get("id"),
user_id: row.get("user_id"),
title: row.get("title"),
body: row.get("body"),
})
})
.collect::<Vec<_>>()
.unwrap();
let mut comments_query =
String::from("SELECT id, post_id, text FROM comments WHERE post_id IN(");
let post_ids = posts
.iter()
.enumerate()
.map(|(i, &Post { id, .. })| {
comments_query += &format!("{}${}", if i == 0 { "" } else { "," }, i + 1);
id
})
.collect::<Vec<i32>>();
comments_query += ")";
let comments = client
.query_raw(&comments_query as &str, post_ids)
.unwrap()
.map(|row| {
Ok(Comment {
id: row.get("id"),
post_id: row.get("post_id"),
text: row.get("text"),
})
})
.collect::<Vec<_>>()
.unwrap();
let mut posts = posts
.into_iter()
.map(|p| (p.id, (p, Vec::new())))
.collect::<HashMap<_, _>>();
let mut users = users
.into_iter()
.map(|u| (u.id, (u, Vec::new())))
.collect::<HashMap<_, _>>();
for comment in comments {
posts.get_mut(&comment.post_id).unwrap().1.push(comment);
}
for (_, post_with_comments) in posts {
users
.get_mut(&post_with_comments.0.user_id)
.unwrap()
.1
.push(post_with_comments);
}
users
.into_values()
.collect::<Vec<(User, Vec<(Post, Vec<Comment>)>)>>()
})
}