Skip to content

Commit ad05dcd

Browse files
committed
Time initial sync.
1 parent 76353dd commit ad05dcd

File tree

6 files changed

+117
-33
lines changed

6 files changed

+117
-33
lines changed

demos/benchmarks/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class MyHomePage extends StatelessWidget {
6767
Widget build(BuildContext context) {
6868
return Scaffold(
6969
appBar: StatusAppBar(title: title),
70-
body: Center(child: content),
70+
body: content,
7171
floatingActionButton: floatingActionButton,
7272
drawer: Drawer(
7373
// Add a ListView to the drawer. This ensures the user can scroll

demos/benchmarks/lib/models/benchmark_item.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ class BenchmarkItem {
6464
return BenchmarkItem.fromRow(results);
6565
}
6666

67+
static Future<void> deleteAll() async {
68+
await db.execute(
69+
'DELETE FROM benchmark_items',
70+
);
71+
}
72+
6773
/// Delete this item.
6874
Future<void> delete() async {
6975
await db.execute('DELETE FROM benchmark_items WHERE id = ?', [id]);

demos/benchmarks/lib/powersync.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// This file performs setup of the PowerSync database
22
import 'dart:convert';
3-
import 'dart:io';
4-
5-
import 'package:http/http.dart' as http;
63

74
import 'package:flutter/foundation.dart';
5+
import 'package:http/http.dart' as http;
86
import 'package:logging/logging.dart';
97
import 'package:path/path.dart';
108
import 'package:path_provider/path_provider.dart';
119
import 'package:powersync/powersync.dart';
1210
import 'package:powersync_benchmarks/models/benchmark_item.dart';
11+
import 'package:powersync_benchmarks/sync_timer.dart';
1312

1413
import './app_config.dart';
1514
import './models/schema.dart';
@@ -75,6 +74,7 @@ class BackendConnector extends PowerSyncBackendConnector {
7574

7675
/// Global reference to the database
7776
late final PowerSyncDatabase db;
77+
SyncTimer timer = SyncTimer();
7878

7979
Future<String> getDatabasePath() async {
8080
const dbFilename = 'powersync-benchmarks.db';
@@ -86,6 +86,16 @@ Future<String> getDatabasePath() async {
8686
return join(dir.path, dbFilename);
8787
}
8888

89+
var currentConnector = BackendConnector();
90+
91+
Future<void> resync() async {
92+
await db.disconnectAndClear();
93+
timer.start(db);
94+
db.connect(
95+
connector: currentConnector,
96+
crudThrottleTime: const Duration(milliseconds: 1));
97+
}
98+
8999
Future<void> openDatabase() async {
90100
// Open the local database
91101
db = PowerSyncDatabase(
@@ -94,8 +104,7 @@ Future<void> openDatabase() async {
94104

95105
BenchmarkItem.updateItemBenchmarks();
96106

97-
var currentConnector = BackendConnector();
98-
107+
timer.start(db);
99108
db.connect(
100109
connector: currentConnector,
101110
crudThrottleTime: const Duration(milliseconds: 1));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'dart:async';
2+
3+
import 'package:powersync/powersync.dart';
4+
5+
class SyncTimer {
6+
Stopwatch stopwatch = Stopwatch();
7+
Duration? syncTime;
8+
bool hasSynced = false;
9+
StreamSubscription? subscription;
10+
11+
StreamSubscription start(PowerSyncDatabase db) {
12+
hasSynced = false;
13+
syncTime = null;
14+
subscription?.cancel();
15+
stopwatch.reset();
16+
17+
stopwatch.start();
18+
subscription = db.statusStream.listen((data) {
19+
if (hasSynced != true &&
20+
data.hasSynced == true &&
21+
data.connected == true &&
22+
data.downloading == false) {
23+
syncTime = stopwatch.elapsed;
24+
hasSynced = true;
25+
}
26+
});
27+
return subscription!;
28+
}
29+
30+
Duration get elapsed {
31+
return stopwatch.elapsed;
32+
}
33+
}

demos/benchmarks/lib/widgets/benchmark_item_widget.dart

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,18 @@ class ListItemWidget extends StatelessWidget {
2222
? '${item.uploadLatency!.inMilliseconds}ms'
2323
: '';
2424

25-
String subtitle =
26-
'$latency / $uploadLatency / ${item.serverCreatedAt?.toIso8601String() ?? ''}';
25+
String subtitle = item.latency == null
26+
? 'Syncing...'
27+
: 'Sync latency: $latency / upload: $uploadLatency';
2728
return Card(
2829
child: Column(
2930
mainAxisSize: MainAxisSize.min,
3031
children: <Widget>[
3132
ListTile(
32-
leading: const Icon(Icons.list),
33+
leading: const Icon(Icons.timer),
3334
title: Text(item.description),
3435
subtitle: Text(subtitle),
3536
),
36-
Row(
37-
mainAxisAlignment: MainAxisAlignment.end,
38-
children: <Widget>[
39-
IconButton(
40-
iconSize: 30,
41-
icon: const Icon(
42-
Icons.delete,
43-
color: Colors.red,
44-
),
45-
tooltip: 'Delete Item',
46-
alignment: Alignment.centerRight,
47-
onPressed: delete,
48-
),
49-
const SizedBox(width: 8),
50-
],
51-
),
5237
],
5338
),
5439
);

demos/benchmarks/lib/widgets/benchmark_items_page.dart

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import '../main.dart';
77
import '../models/benchmark_item.dart';
88
import '../powersync.dart';
99

10+
var itemIndex = 1;
11+
1012
class BenchmarkItemsPage extends StatelessWidget {
1113
const BenchmarkItemsPage({super.key});
1214

1315
@override
1416
Widget build(BuildContext context) {
1517
const content = BenchmarkItemsWidget();
1618

17-
final button = FloatingActionButton(
19+
final addButton = FloatingActionButton(
1820
onPressed: () {
19-
BenchmarkItem.create('Benchmarks');
21+
BenchmarkItem.create('Latency test ${itemIndex++}');
2022
},
2123
tooltip: 'Create Item',
2224
child: const Icon(Icons.add),
@@ -25,7 +27,7 @@ class BenchmarkItemsPage extends StatelessWidget {
2527
final page = MyHomePage(
2628
title: 'Benchmarks',
2729
content: content,
28-
floatingActionButton: button,
30+
floatingActionButton: addButton,
2931
);
3032
return page;
3133
}
@@ -44,7 +46,9 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
4446
List<BenchmarkItem> _data = [];
4547
bool hasSynced = false;
4648
StreamSubscription? _subscription;
49+
StreamSubscription? _countSubscription;
4750
StreamSubscription? _syncStatusSubscription;
51+
int? count;
4852

4953
_BenchmarkItemsWidgetState();
5054

@@ -60,6 +64,15 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
6064
_data = data;
6165
});
6266
});
67+
_countSubscription =
68+
db.watch('select count() as count from ps_oplog').listen((data) {
69+
if (!context.mounted) {
70+
return;
71+
}
72+
setState(() {
73+
count = data.first['count'];
74+
});
75+
});
6376
_syncStatusSubscription = db.statusStream.listen((status) {
6477
if (!context.mounted) {
6578
return;
@@ -75,17 +88,55 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
7588
super.dispose();
7689
_subscription?.cancel();
7790
_syncStatusSubscription?.cancel();
91+
_countSubscription?.cancel();
7892
}
7993

8094
@override
8195
Widget build(BuildContext context) {
82-
return !hasSynced
83-
? const Text("Busy with sync...")
84-
: ListView(
96+
Duration? syncDuration = timer.syncTime ?? timer.elapsed;
97+
if (!hasSynced) {
98+
return Center(
99+
child: Text(
100+
"Busy with sync... ${syncDuration.inMilliseconds}ms / $count operations"));
101+
}
102+
103+
final clearButton = TextButton.icon(
104+
label: const Text('Delete all'),
105+
onPressed: () {
106+
BenchmarkItem.deleteAll();
107+
},
108+
icon: const Icon(Icons.delete));
109+
110+
final resyncButton = TextButton.icon(
111+
label: const Text('Resync'),
112+
onPressed: () async {
113+
await resync();
114+
},
115+
icon: const Icon(Icons.sync));
116+
117+
var buttons = Padding(
118+
padding: const EdgeInsets.all(8.0),
119+
child: OverflowBar(
120+
alignment: MainAxisAlignment.end,
121+
spacing: 8.0,
122+
overflowSpacing: 8.0,
123+
children: <Widget>[
124+
Text(
125+
'First sync duration: ${syncDuration.inMilliseconds}ms / $count operations'),
126+
resyncButton,
127+
clearButton,
128+
],
129+
),
130+
);
131+
return Column(children: [
132+
buttons,
133+
Expanded(
134+
child: ListView(
85135
padding: const EdgeInsets.symmetric(vertical: 8.0),
86136
children: _data.map((list) {
87137
return ListItemWidget(item: list);
88-
}).toList(),
89-
);
138+
}).toList()),
139+
)
140+
]);
90141
}
91142
}

0 commit comments

Comments
 (0)