Skip to content

Commit

Permalink
Tweak benchmarks page.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Nov 6, 2024
1 parent 0ea6b60 commit 7115578
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 51 deletions.
8 changes: 4 additions & 4 deletions demos/benchmarks/lib/models/benchmark_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class BenchmarkItem {
serverCreatedAt: DateTime.tryParse(row['server_created_at'] ?? ''));
}

static Stream<List<BenchmarkItem>> watchItems() {
static Stream<List<BenchmarkItem>> watchGroupedItems() {
return db
.watch(
'SELECT * FROM benchmark_items ORDER BY client_created_at DESC, id')
.map((results) {
.watch('''select max(description) as description, * from benchmark_items
group by client_created_at
order by client_created_at desc''').map((results) {
return results.map(BenchmarkItem.fromRow).toList(growable: false);
});
}
Expand Down
88 changes: 41 additions & 47 deletions demos/benchmarks/lib/widgets/benchmark_items_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@ class BenchmarkItemsPage extends StatelessWidget {
Widget build(BuildContext context) {
const content = BenchmarkItemsWidget();

final addButton = FloatingActionButton(
onPressed: () {
BenchmarkItem.create('Latency test ${itemIndex++}');
},
tooltip: 'Create Item',
child: const Icon(Icons.add),
);

final page = MyHomePage(
title: 'Benchmarks',
content: content,
floatingActionButton: addButton,
);
const page = MyHomePage(title: 'Benchmarks', content: content);
return page;
}
}
Expand All @@ -49,22 +37,33 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
StreamSubscription? _subscription;
StreamSubscription? _countSubscription;
StreamSubscription? _syncStatusSubscription;
String _latencyString = '0';
int? count;

_BenchmarkItemsWidgetState();

@override
void initState() {
super.initState();
final stream = BenchmarkItem.watchItems();
_subscription = stream.listen((data) {
_subscription = BenchmarkItem.watchGroupedItems().listen((data) {
if (!context.mounted) {
return;
}

// Latency is the same for all items in the group
final latencies =
data.map((e) => e.latency).where((e) => e != null).toList();
final totalLatency = latencies.fold(0, (a, b) => a + b!.inMicroseconds);
final averageLatencyMicros =
latencies.isNotEmpty ? totalLatency / latencies.length : 0;
final latencyString = (averageLatencyMicros / 1000.0).toStringAsFixed(1);

setState(() {
_data = data;
_latencyString = latencyString;
});
});

_countSubscription =
db.watch('select count() as count from ps_oplog').listen((data) {
if (!context.mounted) {
Expand Down Expand Up @@ -92,6 +91,20 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
_countSubscription?.cancel();
}

Future<void> createBatch(int n) async {
var items = <String>[];
for (var i = 1; i <= n; i++) {
items.add('Batch Test $itemIndex/$i');
}
itemIndex += 1;
await db.execute('''
INSERT INTO
benchmark_items(id, description, client_created_at)
SELECT uuid(), e.value, datetime('now', 'subsecond') || 'Z'
FROM json_each(?) e
''', [jsonEncode(items)]);
}

@override
Widget build(BuildContext context) {
Duration? syncDuration = timer.syncTime ?? timer.elapsed;
Expand All @@ -101,13 +114,6 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
"Busy with sync... ${syncDuration.inMilliseconds}ms / $count operations"));
}

final latencies =
_data.map((e) => e.latency).where((e) => e != null).toList();
final totalLatency = latencies.fold(0, (a, b) => a + b!.inMicroseconds);
final averageLatencyMicros =
latencies.isNotEmpty ? totalLatency / latencies.length : 0;
final latencyString = (averageLatencyMicros / 1000.0).toStringAsFixed(1);

final clearButton = TextButton.icon(
label: const Text('Delete all'),
onPressed: () {
Expand All @@ -122,37 +128,24 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
},
icon: const Icon(Icons.sync));

final create1 = TextButton.icon(
label: const Text('+1'),
onPressed: () async {
await createBatch(1);
},
icon: const Icon(Icons.create));

final create100 = TextButton.icon(
label: const Text('Create 100'),
label: const Text('+100'),
onPressed: () async {
var items = <String>[];
for (var i = 1; i <= 100; i++) {
items.add('Batch Test $itemIndex/$i');
}
itemIndex += 1;
await db.execute('''
INSERT INTO
benchmark_items(id, description, client_created_at)
SELECT uuid(), e.value, datetime('now', 'subsecond') || 'Z'
FROM json_each(?) e
''', [jsonEncode(items)]);
await createBatch(100);
},
icon: const Icon(Icons.create));

final create1000 = TextButton.icon(
label: const Text('Create 1000'),
label: const Text('+1000'),
onPressed: () async {
var items = <String>[];
for (var i = 1; i <= 1000; i++) {
items.add('Batch Test $itemIndex/$i');
}
itemIndex += 1;
await db.execute('''
INSERT INTO
benchmark_items(id, description, client_created_at)
SELECT uuid(), e.value, datetime('now', 'subsecond') || 'Z'
FROM json_each(?) e
''', [jsonEncode(items)]);
await createBatch(1000);
},
icon: const Icon(Icons.create));

Expand All @@ -164,7 +157,8 @@ class _BenchmarkItemsWidgetState extends State<BenchmarkItemsWidget> {
overflowSpacing: 8.0,
children: <Widget>[
Text(
'First sync duration: ${syncDuration.inMilliseconds}ms / $count operations / ${latencyString}ms latency'),
'First sync duration: ${syncDuration.inMilliseconds}ms / $count operations / ${_latencyString}ms latency'),
create1,
create100,
create1000,
resyncButton,
Expand Down

0 comments on commit 7115578

Please sign in to comment.