|
| 1 | +import 'package:benchmarking/benchmarking.dart'; |
| 2 | +import 'package:collection/collection.dart'; |
| 3 | + |
| 4 | +import 'package:sqlite_async/sqlite_async.dart'; |
| 5 | + |
| 6 | +import '../test/util.dart'; |
| 7 | + |
| 8 | +typedef BenchmarkFunction = Future<void> Function( |
| 9 | + SqliteDatabase, List<List<String>>); |
| 10 | + |
| 11 | +class SqliteBenchmark { |
| 12 | + String name; |
| 13 | + int maxBatchSize; |
| 14 | + BenchmarkFunction fn; |
| 15 | + bool enabled; |
| 16 | + |
| 17 | + SqliteBenchmark(this.name, this.fn, |
| 18 | + {this.maxBatchSize = 100000, this.enabled = true}); |
| 19 | +} |
| 20 | + |
| 21 | +List<SqliteBenchmark> benchmarks = [ |
| 22 | + SqliteBenchmark('Insert: Direct', |
| 23 | + (SqliteDatabase db, List<List<String>> parameters) async { |
| 24 | + for (var params in parameters) { |
| 25 | + await db.execute( |
| 26 | + 'INSERT INTO customers(name, email) VALUES(?, ?)', params); |
| 27 | + } |
| 28 | + }, maxBatchSize: 500), |
| 29 | + SqliteBenchmark('Insert: writeTransaction', |
| 30 | + (SqliteDatabase db, List<List<String>> parameters) async { |
| 31 | + await db.writeTransaction((tx) async { |
| 32 | + for (var params in parameters) { |
| 33 | + await tx.execute( |
| 34 | + 'INSERT INTO customers(name, email) VALUES(?, ?)', params); |
| 35 | + } |
| 36 | + }); |
| 37 | + }, maxBatchSize: 1000), |
| 38 | + SqliteBenchmark('Insert: computeWithDatabase', |
| 39 | + (SqliteDatabase db, List<List<String>> parameters) async { |
| 40 | + await db.computeWithDatabase((db) async { |
| 41 | + for (var params in parameters) { |
| 42 | + db.execute('INSERT INTO customers(name, email) VALUES(?, ?)', params); |
| 43 | + } |
| 44 | + }); |
| 45 | + }), |
| 46 | + SqliteBenchmark('Insert: computeWithDatabase, prepared', |
| 47 | + (SqliteDatabase db, List<List<String>> parameters) async { |
| 48 | + await db.computeWithDatabase((db) async { |
| 49 | + var stmt = db.prepare('INSERT INTO customers(name, email) VALUES(?, ?)'); |
| 50 | + try { |
| 51 | + for (var params in parameters) { |
| 52 | + stmt.execute(params); |
| 53 | + } |
| 54 | + } finally { |
| 55 | + stmt.dispose(); |
| 56 | + } |
| 57 | + }); |
| 58 | + }), |
| 59 | + SqliteBenchmark('Insert: executeBatch', |
| 60 | + (SqliteDatabase db, List<List<String>> parameters) async { |
| 61 | + await db.writeTransaction((tx) async { |
| 62 | + await tx.executeBatch( |
| 63 | + 'INSERT INTO customers(name, email) VALUES(?, ?)', parameters); |
| 64 | + }); |
| 65 | + }), |
| 66 | + SqliteBenchmark('Insert: computeWithDatabase, prepared x10', |
| 67 | + (SqliteDatabase db, List<List<String>> parameters) async { |
| 68 | + await db.computeWithDatabase((db) async { |
| 69 | + var stmt = db.prepare( |
| 70 | + 'INSERT INTO customers(name, email) VALUES (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)'); |
| 71 | + try { |
| 72 | + for (var i = 0; i < parameters.length; i += 10) { |
| 73 | + var myParams = |
| 74 | + parameters.sublist(i, i + 10).flattened.toList(growable: false); |
| 75 | + stmt.execute(myParams); |
| 76 | + } |
| 77 | + } finally { |
| 78 | + stmt.dispose(); |
| 79 | + } |
| 80 | + }); |
| 81 | + }, enabled: false) |
| 82 | +]; |
| 83 | + |
| 84 | +void main() async { |
| 85 | + setupLogger(); |
| 86 | + |
| 87 | + var parameters = List.generate( |
| 88 | + 20000, (index) => ['Test user $index', 'user$index@example.org']); |
| 89 | + |
| 90 | + createTables(SqliteDatabase db) async { |
| 91 | + await db.writeTransaction((tx) async { |
| 92 | + await tx.execute( |
| 93 | + 'CREATE TABLE IF NOT EXISTS customers(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT)'); |
| 94 | + await tx.execute('DELETE FROM customers WHERE 1'); |
| 95 | + }); |
| 96 | + await db.execute('VACUUM'); |
| 97 | + await db.execute('PRAGMA wal_checkpoint(TRUNCATE)'); |
| 98 | + } |
| 99 | + |
| 100 | + final db = await setupDatabase(path: 'test-db/benchmark.db'); |
| 101 | + await createTables(db); |
| 102 | + |
| 103 | + benchmark(SqliteBenchmark benchmark) async { |
| 104 | + if (!benchmark.enabled) { |
| 105 | + return; |
| 106 | + } |
| 107 | + await createTables(db); |
| 108 | + |
| 109 | + var limitedParameters = parameters; |
| 110 | + if (limitedParameters.length > benchmark.maxBatchSize) { |
| 111 | + limitedParameters = limitedParameters.sublist(0, benchmark.maxBatchSize); |
| 112 | + } |
| 113 | + |
| 114 | + final rows1 = await db.execute('SELECT count(*) as count FROM customers'); |
| 115 | + assert(rows1[0]['count'] == 0); |
| 116 | + final results = await asyncBenchmark(benchmark.name, () async { |
| 117 | + await benchmark.fn(db, limitedParameters); |
| 118 | + // This would make the benchmark fair, but only if each benchmark uses the |
| 119 | + // same batch size. |
| 120 | + // await db.execute('PRAGMA wal_checkpoint(TRUNCATE)'); |
| 121 | + }); |
| 122 | + |
| 123 | + final row2 = await db.execute('SELECT count(*) as count FROM customers'); |
| 124 | + assert(row2[0]['count'] == 0); |
| 125 | + results.report(units: limitedParameters.length); |
| 126 | + } |
| 127 | + |
| 128 | + for (var entry in benchmarks) { |
| 129 | + await benchmark(entry); |
| 130 | + } |
| 131 | +} |
0 commit comments