| 1 |
use criterion::BenchmarkId; |
| 2 |
use criterion::Criterion; |
| 3 |
use criterion::{criterion_group, criterion_main}; |
| 4 |
|
| 5 |
use sqlx::sqlite::{Sqlite, SqliteConnection}; |
| 6 |
use sqlx::{Connection, Executor}; |
| 7 |
use sqlx_test::new; |
| 8 |
|
| 9 |
|
| 10 |
async fn do_describe_trivial(db: &std::cell::RefCell<SqliteConnection>) { |
| 11 |
db.borrow_mut().describe("select 1").await.unwrap(); |
| 12 |
} |
| 13 |
|
| 14 |
async fn do_describe_recursive(db: &std::cell::RefCell<SqliteConnection>) { |
| 15 |
db.borrow_mut() |
| 16 |
.describe( |
| 17 |
r#" |
| 18 |
WITH RECURSIVE schedule(begin_date) AS MATERIALIZED ( |
| 19 |
SELECT datetime('2022-10-01') |
| 20 |
WHERE datetime('2022-10-01') < datetime('2022-11-03') |
| 21 |
UNION ALL |
| 22 |
SELECT datetime(begin_date,'+1 day') |
| 23 |
FROM schedule |
| 24 |
WHERE datetime(begin_date) < datetime(?2) |
| 25 |
) |
| 26 |
SELECT |
| 27 |
begin_date |
| 28 |
FROM schedule |
| 29 |
GROUP BY begin_date |
| 30 |
"#, |
| 31 |
) |
| 32 |
.await |
| 33 |
.unwrap(); |
| 34 |
} |
| 35 |
|
| 36 |
async fn do_describe_insert(db: &std::cell::RefCell<SqliteConnection>) { |
| 37 |
db.borrow_mut() |
| 38 |
.describe("INSERT INTO tweet (id, text) VALUES (2, 'Hello') RETURNING *") |
| 39 |
.await |
| 40 |
.unwrap(); |
| 41 |
} |
| 42 |
|
| 43 |
async fn do_describe_insert_fks(db: &std::cell::RefCell<SqliteConnection>) { |
| 44 |
db.borrow_mut() |
| 45 |
.describe("insert into statements (text) values ('a') returning id") |
| 46 |
.await |
| 47 |
.unwrap(); |
| 48 |
} |
| 49 |
|
| 50 |
async fn init_connection() -> SqliteConnection { |
| 51 |
let mut conn = new::<Sqlite>().await.unwrap(); |
| 52 |
|
| 53 |
conn.execute( |
| 54 |
r#" |
| 55 |
CREATE TEMPORARY TABLE statements ( |
| 56 |
id integer not null primary key, |
| 57 |
text text not null |
| 58 |
); |
| 59 |
|
| 60 |
CREATE TEMPORARY TABLE votes1 (statement_id integer not null references statements(id)); |
| 61 |
CREATE TEMPORARY TABLE votes2 (statement_id integer not null references statements(id)); |
| 62 |
CREATE TEMPORARY TABLE votes3 (statement_id integer not null references statements(id)); |
| 63 |
CREATE TEMPORARY TABLE votes4 (statement_id integer not null references statements(id)); |
| 64 |
CREATE TEMPORARY TABLE votes5 (statement_id integer not null references statements(id)); |
| 65 |
CREATE TEMPORARY TABLE votes6 (statement_id integer not null references statements(id)); |
| 66 |
--CREATE TEMPORARY TABLE votes7 (statement_id integer not null references statements(id)); |
| 67 |
--CREATE TEMPORARY TABLE votes8 (statement_id integer not null references statements(id)); |
| 68 |
--CREATE TEMPORARY TABLE votes9 (statement_id integer not null references statements(id)); |
| 69 |
--CREATE TEMPORARY TABLE votes10 (statement_id integer not null references statements(id)); |
| 70 |
--CREATE TEMPORARY TABLE votes11 (statement_id integer not null references statements(id)); |
| 71 |
"#, |
| 72 |
) |
| 73 |
.await |
| 74 |
.unwrap(); |
| 75 |
conn |
| 76 |
} |
| 77 |
|
| 78 |
fn describe_trivial(c: &mut Criterion) { |
| 79 |
let runtime = tokio::runtime::Runtime::new().unwrap(); |
| 80 |
let db = std::cell::RefCell::new(runtime.block_on(init_connection())); |
| 81 |
|
| 82 |
c.bench_with_input( |
| 83 |
BenchmarkId::new("select", "trivial"), |
| 84 |
&db, |
| 85 |
move |b, db_ref| { |
| 86 |
|
| 87 |
|
| 88 |
b.to_async(&runtime).iter(|| do_describe_trivial(db_ref)); |
| 89 |
}, |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
fn describe_recursive(c: &mut Criterion) { |
| 94 |
let runtime = tokio::runtime::Runtime::new().unwrap(); |
| 95 |
let db = std::cell::RefCell::new(runtime.block_on(init_connection())); |
| 96 |
|
| 97 |
c.bench_with_input( |
| 98 |
BenchmarkId::new("select", "recursive"), |
| 99 |
&db, |
| 100 |
move |b, db_ref| { |
| 101 |
|
| 102 |
|
| 103 |
b.to_async(&runtime).iter(|| do_describe_recursive(db_ref)); |
| 104 |
}, |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
fn describe_insert(c: &mut Criterion) { |
| 109 |
let runtime = tokio::runtime::Runtime::new().unwrap(); |
| 110 |
let db = std::cell::RefCell::new(runtime.block_on(init_connection())); |
| 111 |
|
| 112 |
c.bench_with_input( |
| 113 |
BenchmarkId::new("insert", "returning"), |
| 114 |
&db, |
| 115 |
move |b, db_ref| { |
| 116 |
|
| 117 |
|
| 118 |
b.to_async(&runtime).iter(|| do_describe_insert(db_ref)); |
| 119 |
}, |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
fn describe_insert_fks(c: &mut Criterion) { |
| 124 |
let runtime = tokio::runtime::Runtime::new().unwrap(); |
| 125 |
let db = std::cell::RefCell::new(runtime.block_on(init_connection())); |
| 126 |
|
| 127 |
c.bench_with_input(BenchmarkId::new("insert", "fks"), &db, move |b, db_ref| { |
| 128 |
|
| 129 |
|
| 130 |
b.to_async(&runtime).iter(|| do_describe_insert_fks(db_ref)); |
| 131 |
}); |
| 132 |
} |
| 133 |
|
| 134 |
criterion_group!( |
| 135 |
benches, |
| 136 |
describe_trivial, |
| 137 |
describe_recursive, |
| 138 |
describe_insert, |
| 139 |
describe_insert_fks |
| 140 |
); |
| 141 |
criterion_main!(benches); |