Skip to content

Commit 764d39c

Browse files
committed
Expand performance tests; add hard limits.
1 parent ce944d1 commit 764d39c

1 file changed

Lines changed: 51 additions & 9 deletions

File tree

dart/test/perf_test.dart

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ import 'utils/native_test_utils.dart';
88
import 'utils/tracking_vfs.dart';
99
import './schema_test.dart' show schema;
1010

11-
void main() {
11+
// These test how many filesystem reads and writes are performed during sync_local.
12+
// The real world performane of filesystem operations depend a lot on the specific system.
13+
// For example, on native desktop systems, the performance of temporary filesystem storage could
14+
// be close to memory performance. However, on web and mobile, (temporary) filesystem operations
15+
// could drastically slow down performance. So rather than only testing the real time for these
16+
// queries, we count the number of filesystem operations.
17+
void testFilesystemOperations(
18+
{bool unique = true,
19+
int count = 200000,
20+
int alreadyApplied = 10000,
21+
int buckets = 10}) {
1222
late TrackingFileSystem vfs;
1323
late CommonDatabase db;
1424

@@ -31,38 +41,40 @@ void main() {
3141

3242
setUp(() {
3343
db.execute('SELECT powersync_replace_schema(?)', [json.encode(schema)]);
44+
// Generate dummy data
45+
// We can replace this with actual similated download operations later
3446
db.execute('''
3547
BEGIN TRANSACTION;
3648
3749
WITH RECURSIVE generate_rows(n) AS (
3850
SELECT 1
3951
UNION ALL
40-
SELECT n + 1 FROM generate_rows WHERE n < 200000
52+
SELECT n + 1 FROM generate_rows WHERE n < $count
4153
)
4254
INSERT INTO ps_oplog (bucket, op_id, row_type, row_id, key, data, hash)
4355
SELECT
44-
(n % 10), -- Generate 10 different buckets
56+
(n % $buckets), -- Generate n different buckets
4557
n,
4658
'assets',
47-
uuid(),
59+
${unique ? 'uuid()' : "'duplicated_id'"},
4860
uuid(),
4961
'{"description": "' || n || '", "make": "test", "model": "this is just filler data. this is just filler data. this is just filler data. this is just filler data. this is just filler data. this is just filler data. this is just filler data. "}',
5062
(n * 17) % 1000000000 -- Some pseudo-random hash
5163
5264
FROM generate_rows;
5365
54-
WITH RECURSIVE generate_rows(n) AS (
66+
WITH RECURSIVE generate_bucket_rows(n) AS (
5567
SELECT 1
5668
UNION ALL
57-
SELECT n + 1 FROM generate_rows WHERE n < 10
69+
SELECT n + 1 FROM generate_bucket_rows WHERE n < $buckets
5870
)
5971
INSERT INTO ps_buckets (id, name, last_applied_op)
6072
SELECT
61-
(n % 10),
73+
(n % $buckets),
6274
'bucket' || n,
63-
10000
75+
$alreadyApplied -- simulate a percentage of operations previously applied
6476
65-
FROM generate_rows;
77+
FROM generate_bucket_rows;
6678
6779
COMMIT;
6880
''');
@@ -76,6 +88,11 @@ COMMIT;
7688
db.select('insert into powersync_operations(op, data) values(?, ?)',
7789
['sync_local', '']);
7890
print('${timer.elapsed.inMilliseconds}ms ${vfs.stats()}');
91+
92+
// These are fairly generous limits, to catch significant regressions only.
93+
expect(vfs.tempWrites, lessThan(count / 50));
94+
expect(timer.elapsed,
95+
lessThan(Duration(milliseconds: 100 + (count / 50).round())));
7996
});
8097

8198
test('sync_local (partial)', () {
@@ -88,5 +105,30 @@ COMMIT;
88105
})
89106
]);
90107
print('${timer.elapsed.inMilliseconds}ms ${vfs.stats()}');
108+
expect(vfs.tempWrites, lessThan(count / 50));
109+
expect(timer.elapsed,
110+
lessThan(Duration(milliseconds: 100 + (count / 50).round())));
111+
});
112+
}
113+
114+
main() {
115+
group('test filesystem operations with unique ids', () {
116+
testFilesystemOperations(
117+
unique: true, count: 500000, alreadyApplied: 10000, buckets: 10);
118+
});
119+
group('test filesytem operations with duplicate ids', () {
120+
// If this takes more than a couple of milliseconds to complete, there is a performance bug
121+
testFilesystemOperations(
122+
unique: false, count: 5000, alreadyApplied: 1000, buckets: 10);
123+
});
124+
125+
group('test filesystem operations with a small number of changes', () {
126+
testFilesystemOperations(
127+
unique: true, count: 100000, alreadyApplied: 95000, buckets: 10);
128+
});
129+
130+
group('test filesystem operations with a large number of buckets', () {
131+
testFilesystemOperations(
132+
unique: true, count: 100000, alreadyApplied: 10000, buckets: 1000);
91133
});
92134
}

0 commit comments

Comments
 (0)