sqlite-diff-rs 0.1.2

Build SQLite changeset and patchset binary formats programmatically, without SQLite
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Benchmark for Debezium parsing and conversion to `SQLite` changesets.
//!
//! Measures the performance of:
//! 1. Parsing Debezium envelope JSON format
//! 2. Converting parsed envelopes to `SQLite` changeset operations

use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use sqlite_diff_rs::debezium::{Envelope, Op, parse};
use sqlite_diff_rs::{ChangeDelete, ChangeUpdate, Insert, SimpleTable};
use std::hint::black_box;

/// Real Debezium CREATE (insert) event from `PostgreSQL` connector documentation.
/// Source: <https://debezium.io/documentation/reference/stable/connectors/postgresql.html>
const CREATE_EVENT_JSON: &str = r#"{
    "before": null,
    "after": {
        "id": 1,
        "first_name": "Anne",
        "last_name": "Kretchmar",
        "email": "annek@noanswer.org"
    },
    "source": {
        "version": "2.7.4.Final",
        "connector": "postgresql",
        "name": "PostgreSQL_server",
        "ts_ms": 1559033904863,
        "db": "postgres",
        "schema": "public",
        "table": "customers",
        "txId": 555,
        "lsn": 24023128
    },
    "op": "c",
    "ts_ms": 1559033904863
}"#;

/// Real Debezium UPDATE event from `PostgreSQL` connector documentation.
const UPDATE_EVENT_JSON: &str = r#"{
    "before": {
        "id": 1,
        "first_name": "Anne",
        "last_name": "Kretchmar",
        "email": "annek@noanswer.org"
    },
    "after": {
        "id": 1,
        "first_name": "Anne Marie",
        "last_name": "Kretchmar",
        "email": "annek@noanswer.org"
    },
    "source": {
        "version": "2.7.4.Final",
        "connector": "postgresql",
        "name": "PostgreSQL_server",
        "ts_ms": 1559033905123,
        "db": "postgres",
        "schema": "public",
        "table": "customers",
        "txId": 556,
        "lsn": 24023256
    },
    "op": "u",
    "ts_ms": 1559033905123
}"#;

/// Real Debezium DELETE event from `PostgreSQL` connector documentation.
const DELETE_EVENT_JSON: &str = r#"{
    "before": {
        "id": 1,
        "first_name": "Anne Marie",
        "last_name": "Kretchmar",
        "email": "annek@noanswer.org"
    },
    "after": null,
    "source": {
        "version": "2.7.4.Final",
        "connector": "postgresql",
        "name": "PostgreSQL_server",
        "ts_ms": 1559033906456,
        "db": "postgres",
        "schema": "public",
        "table": "customers",
        "txId": 557,
        "lsn": 24023384
    },
    "op": "d",
    "ts_ms": 1559033906456
}"#;

/// Compact CREATE event (minified JSON like Kafka messages)
const CREATE_EVENT_COMPACT: &str = r#"{"before":null,"after":{"id":1,"first_name":"Anne","last_name":"Kretchmar","email":"annek@noanswer.org"},"source":{"version":"2.7.4.Final","connector":"postgresql","name":"PostgreSQL_server","ts_ms":1559033904863,"db":"postgres","schema":"public","table":"customers","txId":555,"lsn":24023128},"op":"c","ts_ms":1559033904863}"#;

/// Large Debezium event with many columns (orders table)
const LARGE_INSERT_JSON: &str = r#"{
    "before": null,
    "after": {
        "id": 12345,
        "customer_id": 9876,
        "order_date": "2024-01-15T10:30:00Z",
        "status": "pending",
        "total_amount": 1234.56,
        "shipping_address": "123 Main Street, Anytown, ST 12345, USA",
        "billing_address": "456 Oak Avenue, Somewhere, ST 67890, USA",
        "notes": "Please deliver between 9am and 5pm. Ring doorbell twice.",
        "created_at": "2024-01-15T10:30:00Z",
        "updated_at": "2024-01-15T10:30:00Z",
        "is_express": true,
        "discount_code": "SAVE20",
        "tax_amount": 98.76,
        "shipping_cost": 15.99,
        "tracking_number": null
    },
    "source": {
        "version": "2.7.4.Final",
        "connector": "postgresql",
        "name": "PostgreSQL_server",
        "ts_ms": 1705318200000,
        "db": "ecommerce",
        "schema": "public",
        "table": "orders",
        "txId": 12345,
        "lsn": 98765432
    },
    "op": "c",
    "ts_ms": 1705318200000
}"#;

/// Read/snapshot event (initial data load)
const READ_EVENT_JSON: &str = r#"{
    "before": null,
    "after": {
        "id": 1001,
        "first_name": "Sally",
        "last_name": "Thomas",
        "email": "sally.thomas@acme.com"
    },
    "source": {
        "version": "2.7.4.Final",
        "connector": "postgresql",
        "name": "PostgreSQL_server",
        "ts_ms": 1559033900000,
        "snapshot": "true",
        "db": "postgres",
        "schema": "public",
        "table": "customers"
    },
    "op": "r",
    "ts_ms": 1559033900000
}"#;

/// Event with transaction metadata
const CREATE_WITH_TRANSACTION_JSON: &str = r#"{
    "before": null,
    "after": {
        "id": 1,
        "first_name": "Anne",
        "last_name": "Kretchmar",
        "email": "annek@noanswer.org"
    },
    "source": {
        "version": "2.7.4.Final",
        "connector": "postgresql",
        "name": "PostgreSQL_server",
        "ts_ms": 1559033904863,
        "db": "postgres",
        "schema": "public",
        "table": "customers",
        "txId": 555,
        "lsn": 24023128
    },
    "op": "c",
    "ts_ms": 1559033904863,
    "transaction": {
        "id": "555:24023128",
        "total_order": 1,
        "data_collection_order": 1
    }
}"#;

/// Create the table schema for customers
fn customers_table() -> SimpleTable {
    SimpleTable::new(
        "customers",
        &["id", "first_name", "last_name", "email"],
        &[0],
    )
}

/// Create the table schema for orders (large table)
fn orders_table() -> SimpleTable {
    SimpleTable::new(
        "orders",
        &[
            "id",
            "customer_id",
            "order_date",
            "status",
            "total_amount",
            "shipping_address",
            "billing_address",
            "notes",
            "created_at",
            "updated_at",
            "is_express",
            "discount_code",
            "tax_amount",
            "shipping_cost",
            "tracking_number",
        ],
        &[0],
    )
}

/// Create a batch of Debezium messages for throughput testing
fn create_batch(count: usize) -> Vec<String> {
    (0..count)
        .map(|i| {
            format!(
                r#"{{"before":null,"after":{{"id":{},"first_name":"User{}","last_name":"Test","email":"user{}@example.com"}},"source":{{"version":"2.7.4.Final","connector":"postgresql","name":"test","ts_ms":1559033904863,"db":"test","schema":"public","table":"customers","txId":{},"lsn":{}}},"op":"c","ts_ms":1559033904863}}"#,
                i, i, i, i, i * 100
            )
        })
        .collect()
}

fn benchmark_parsing(c: &mut Criterion) {
    let mut group = c.benchmark_group("debezium_parsing");

    group.bench_function("create_event", |b| {
        b.iter(|| black_box(parse::<serde_json::Value>(black_box(CREATE_EVENT_JSON)).unwrap()));
    });

    group.bench_function("create_event_compact", |b| {
        b.iter(|| black_box(parse::<serde_json::Value>(black_box(CREATE_EVENT_COMPACT)).unwrap()));
    });

    group.bench_function("update_event", |b| {
        b.iter(|| black_box(parse::<serde_json::Value>(black_box(UPDATE_EVENT_JSON)).unwrap()));
    });

    group.bench_function("delete_event", |b| {
        b.iter(|| black_box(parse::<serde_json::Value>(black_box(DELETE_EVENT_JSON)).unwrap()));
    });

    group.bench_function("large_insert", |b| {
        b.iter(|| black_box(parse::<serde_json::Value>(black_box(LARGE_INSERT_JSON)).unwrap()));
    });

    group.bench_function("read_snapshot", |b| {
        b.iter(|| black_box(parse::<serde_json::Value>(black_box(READ_EVENT_JSON)).unwrap()));
    });

    group.bench_function("with_transaction", |b| {
        b.iter(|| {
            black_box(parse::<serde_json::Value>(black_box(CREATE_WITH_TRANSACTION_JSON)).unwrap())
        });
    });

    group.finish();
}

fn benchmark_conversion(c: &mut Criterion) {
    let mut group = c.benchmark_group("debezium_conversion");

    let customers = customers_table();
    let orders = orders_table();

    // Pre-parse envelopes for conversion-only benchmarks
    let create_envelope: Envelope<serde_json::Value> = parse(CREATE_EVENT_JSON).unwrap();
    let update_envelope: Envelope<serde_json::Value> = parse(UPDATE_EVENT_JSON).unwrap();
    let delete_envelope: Envelope<serde_json::Value> = parse(DELETE_EVENT_JSON).unwrap();
    let large_envelope: Envelope<serde_json::Value> = parse(LARGE_INSERT_JSON).unwrap();
    let read_envelope: Envelope<serde_json::Value> = parse(READ_EVENT_JSON).unwrap();

    group.bench_function("create_to_insert", |b| {
        b.iter(|| {
            let env = black_box(&create_envelope);
            let table = black_box(&customers);
            let _: Insert<_, String, Vec<u8>> = (env, table).try_into().unwrap();
        });
    });

    group.bench_function("update_to_changeset", |b| {
        b.iter(|| {
            let env = black_box(&update_envelope);
            let table = black_box(&customers);
            let _: ChangeUpdate<_, String, Vec<u8>> = (env, table).try_into().unwrap();
        });
    });

    group.bench_function("delete_to_changeset", |b| {
        b.iter(|| {
            let env = black_box(&delete_envelope);
            let table = black_box(&customers);
            let _: ChangeDelete<_, String, Vec<u8>> = (env, table).try_into().unwrap();
        });
    });

    group.bench_function("large_insert_to_changeset", |b| {
        b.iter(|| {
            let env = black_box(&large_envelope);
            let table = black_box(&orders);
            let _: Insert<_, String, Vec<u8>> = (env, table).try_into().unwrap();
        });
    });

    group.bench_function("read_to_insert", |b| {
        b.iter(|| {
            let env = black_box(&read_envelope);
            let table = black_box(&customers);
            let _: Insert<_, String, Vec<u8>> = (env, table).try_into().unwrap();
        });
    });

    group.finish();
}

fn benchmark_end_to_end(c: &mut Criterion) {
    let mut group = c.benchmark_group("debezium_end_to_end");

    let customers = customers_table();
    let orders = orders_table();

    group.bench_function("parse_and_convert_create", |b| {
        b.iter(|| {
            let json = black_box(CREATE_EVENT_JSON);
            let table = black_box(&customers);
            let env: Envelope<serde_json::Value> = parse(json).unwrap();
            let _: Insert<_, String, Vec<u8>> = (&env, table).try_into().unwrap();
        });
    });

    group.bench_function("parse_and_convert_update", |b| {
        b.iter(|| {
            let json = black_box(UPDATE_EVENT_JSON);
            let table = black_box(&customers);
            let env: Envelope<serde_json::Value> = parse(json).unwrap();
            let _: ChangeUpdate<_, String, Vec<u8>> = (&env, table).try_into().unwrap();
        });
    });

    group.bench_function("parse_and_convert_delete", |b| {
        b.iter(|| {
            let json = black_box(DELETE_EVENT_JSON);
            let table = black_box(&customers);
            let env: Envelope<serde_json::Value> = parse(json).unwrap();
            let _: ChangeDelete<_, String, Vec<u8>> = (&env, table).try_into().unwrap();
        });
    });

    group.bench_function("parse_and_convert_large", |b| {
        b.iter(|| {
            let json = black_box(LARGE_INSERT_JSON);
            let table = black_box(&orders);
            let env: Envelope<serde_json::Value> = parse(json).unwrap();
            let _: Insert<_, String, Vec<u8>> = (&env, table).try_into().unwrap();
        });
    });

    group.finish();
}

fn benchmark_batch_throughput(c: &mut Criterion) {
    let mut group = c.benchmark_group("debezium_batch_throughput");

    let customers = customers_table();

    for batch_size in [10, 100, 1000] {
        let batch = create_batch(batch_size);
        let total_bytes: usize = batch.iter().map(String::len).sum();

        group.throughput(Throughput::Bytes(total_bytes as u64));

        group.bench_with_input(
            BenchmarkId::new("parse_only", batch_size),
            &batch,
            |b, batch| {
                b.iter(|| {
                    for json in batch {
                        black_box(parse::<serde_json::Value>(json).unwrap());
                    }
                });
            },
        );

        group.bench_with_input(
            BenchmarkId::new("parse_and_convert", batch_size),
            &batch,
            |b, batch| {
                b.iter(|| {
                    let table = black_box(&customers);
                    for json in batch {
                        let env: Envelope<serde_json::Value> = parse(json).unwrap();
                        if env.op == Op::Create {
                            let _: Insert<_, String, Vec<u8>> = (&env, table).try_into().unwrap();
                        }
                    }
                });
            },
        );
    }

    group.finish();
}

criterion_group!(
    benches,
    benchmark_parsing,
    benchmark_conversion,
    benchmark_end_to_end,
    benchmark_batch_throughput,
);
criterion_main!(benches);