diff --git a/Cargo.toml b/Cargo.toml index 3cfcde3..900f8b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,11 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" thiserror = "2" uuid = { version = "1", features = ["v5", "v7", "serde"] } + +# Debug builds (`flutter run`, tests) with optimised code: dependencies (SQLite, serde...) +# at full speed, our crates lightly optimised. Unoptimised, the graph is ~8x slower. +[profile.dev] +opt-level = 1 + +[profile.dev.package."*"] +opt-level = 3 diff --git a/app/integration_test/perf_test.dart b/app/integration_test/perf_test.dart new file mode 100644 index 0000000..f2bdd29 --- /dev/null +++ b/app/integration_test/perf_test.dart @@ -0,0 +1,239 @@ +// Frame timings of common interactions on a real graph (Rust core over FFI). +// Run in profile mode on a COPY of a graph (opening it may write today's journal): +// flutter drive --profile -d linux --driver=test_driver/integration_test.dart \ +// --target=integration_test/perf_test.dart --dart-define=GRAPH=/path/to/copy +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:synapse/src/app.dart'; +import 'package:synapse/src/backend/graph_backend.dart'; +import 'package:synapse/src/rust/frb_generated.dart'; +import 'package:synapse/src/state/app_state.dart'; +import 'package:synapse/src/views/quick_switcher.dart'; +import 'package:synapse/src/views/shell.dart'; + +const graph = String.fromEnvironment('GRAPH'); + +/// Lines and errors, returned to the driver (`build/integration_response_data.json`): +/// `flutter drive` does not show the app's prints. +final report = []; + +Future wait(WidgetTester tester, Duration d) async { + final end = DateTime.now().add(d); + while (DateTime.now().isBefore(end)) { + await tester.pump(const Duration(milliseconds: 16)); + } +} + +/// Runs [action], keeps pumping for [after] and prints wall time plus frame stats. +Future measure( + WidgetTester tester, + String name, + Future Function() action, { + Duration after = const Duration(milliseconds: 800), +}) async { + final timings = []; + void collect(List t) => timings.addAll(t); + SchedulerBinding.instance.addTimingsCallback(collect); + final watch = Stopwatch()..start(); + await action(); + final acted = watch.elapsedMilliseconds; + await wait(tester, after); + SchedulerBinding.instance.removeTimingsCallback(collect); + int us(Duration d) => d.inMicroseconds; + final build = timings.map((t) => us(t.buildDuration)).toList()..sort(); + final raster = timings.map((t) => us(t.rasterDuration)).toList()..sort(); + String p(List l, double q) => l.isEmpty + ? '-' + : (l[((l.length - 1) * q).round()] / 1000).toStringAsFixed(1); + final slow = timings + .where( + (t) => + t.buildDuration + t.rasterDuration > + const Duration(milliseconds: 16), + ) + .length; + report.add( + 'PERF ${name.padRight(26)} action ${acted.toString().padLeft(5)} ms | ' + 'frames ${timings.length.toString().padLeft(3)} slow ${slow.toString().padLeft(3)} | ' + 'build p50 ${p(build, .5)} p90 ${p(build, .9)} max ${p(build, 1)} | ' + 'raster p50 ${p(raster, .5)} p90 ${p(raster, .9)} max ${p(raster, 1)} | ' + 'rss ${ProcessInfo.currentRss ~/ (1 << 20)} MB', + ); +} + +void main() { + final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.benchmarkLive; + + testWidgets('interaction timings on a real graph', (tester) async { + if (graph.isEmpty) { + markTestSkipped('pass --dart-define=GRAPH='); + return; + } + final errors = []; + final previous = FlutterError.onError; + FlutterError.onError = (d) => errors.add(d.toString()); + try { + await _run(tester); + } catch (e, st) { + errors.add('$e\n$st'); + } finally { + FlutterError.onError = previous; + binding.reportData = {'perf': report, 'errors': errors}; + } + }); +} + +Future _run(WidgetTester tester) async { + { + await RustLib.init(); + final backend = await FfiGraphBackend.open(graph); + late AppState app; + await measure(tester, 'startup', () async { + await tester.pumpWidget(SynapseApp(backend: backend)); + while (find.byType(Shell).evaluate().isEmpty) { + await tester.pump(const Duration(milliseconds: 16)); + } + app = AppScope.read(tester.element(find.byType(Shell))); + }, after: const Duration(seconds: 2)); + + final scroll = find.byType(Scrollable).first; + await measure(tester, 'journals scroll', () async { + for (var i = 0; i < 40; i++) { + await tester.drag(scroll, const Offset(0, -120), warnIfMissed: false); + await tester.pump(const Duration(milliseconds: 16)); + } + }); + + final pages = await backend.listPages( + includeJournals: false, + limit: 1000, + offset: 0, + ); + final hub = pages.firstWhere((p) => p.isHub); + final notes = pages.where((p) => !p.isHub).take(8).toList(); + await measure(tester, 'open hub', () async { + app.go(PageTarget(id: hub.id)); + await tester.pump(); + }); + final hubs = pages.where((p) => p.isHub).toList(); + await measure(tester, 'open all hubs', () async { + for (final h in hubs) { + app.go(PageTarget(id: h.id)); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + } + }); + await measure(tester, 'open first hub again', () async { + app.go(PageTarget(id: hub.id)); + await tester.pump(); + }); + await measure(tester, 'open 8 notes', () async { + for (final n in notes) { + app.go(PageTarget(id: n.id)); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 16)); + } + }); + + final field = find.byType(TextField); + if (field.evaluate().isNotEmpty) { + await tester.tap(field.first); + await tester.pump(); + final c = tester.widget(field.first).controller!; + final base = c.text; + await measure(tester, 'type 40 chars', () async { + for (var i = 1; i <= 40; i++) { + final t = '$base ${'x' * i}'; + tester.testTextInput.updateEditingValue( + TextEditingValue( + text: t, + selection: TextSelection.collapsed(offset: t.length), + ), + ); + await tester.pump(const Duration(milliseconds: 60)); + } + }, after: const Duration(seconds: 2)); + tester.testTextInput.updateEditingValue( + TextEditingValue( + text: base, + selection: TextSelection.collapsed(offset: base.length), + ), + ); + await wait(tester, const Duration(seconds: 1)); + FocusManager.instance.primaryFocus?.unfocus(); + } + + await measure(tester, 'back x5', () async { + for (var i = 0; i < 5; i++) { + app.back(); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 16)); + } + }); + await measure(tester, 'all pages', () async { + app.go(const AllPagesRoute()); + await tester.pump(); + }); + await measure(tester, 'all pages scroll', () async { + for (var i = 0; i < 30; i++) { + await tester.drag( + find.byType(Scrollable).last, + const Offset(0, -150), + warnIfMissed: false, + ); + await tester.pump(const Duration(milliseconds: 16)); + } + }); + await measure(tester, 'quick switcher typing', () async { + showQuickSwitcher(tester.element(find.byType(Shell))); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + var q = ''; + for (final ch in 'proyecto'.split('')) { + q += ch; + await tester.enterText(find.byType(TextField).last, q); + await tester.pump(const Duration(milliseconds: 80)); + } + }); + Navigator.of(tester.element(find.byType(TextField).last)).pop(); + await wait(tester, const Duration(milliseconds: 300)); + await measure(tester, 'new tab + switch x6', () async { + app.newTab(); + await tester.pump(); + for (var i = 0; i < 6; i++) { + app.nextTab(); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 16)); + } + }); + await measure(tester, 'graph tree', () async { + app.go(const GraphRoute(GraphMode.tree)); + await tester.pump(); + }, after: const Duration(seconds: 4)); + await measure(tester, 'graph network', () async { + app.go(const GraphRoute(GraphMode.network)); + await tester.pump(); + }, after: const Duration(seconds: 6)); + await measure( + tester, + 'network idle', + () async {}, + after: const Duration(seconds: 2), + ); + await measure(tester, 'journals after graph', () async { + app.go(const JournalsRoute()); + await tester.pump(); + }, after: const Duration(seconds: 2)); + await measure( + tester, + 'journals idle', + () async {}, + after: const Duration(seconds: 2), + ); + } +} diff --git a/docs/app.md b/docs/app.md index 2ce8cbb..4d9985a 100644 --- a/docs/app.md +++ b/docs/app.md @@ -236,7 +236,16 @@ cargo run --release -p synapse-ffi --example graph_stats -- /path/to/graph cd app && flutter drive --profile -d linux --driver=test_driver/integration_test.dart \ --target=integration_test/graph_smoke_test.dart \ --dart-define=GRAPH=/path/to/copy [--dart-define=SHOTS=/dir/for/pngs] +# Frame timings and RSS of common interactions (journals, pages, typing, tabs, Ctrl+K, +# tree, network); results land in build/integration_response_data.json: +cd app && flutter drive --profile -d linux --driver=test_driver/integration_test.dart \ + --target=integration_test/perf_test.dart --dart-define=GRAPH=/path/to/copy ``` +Debug builds compile the Rust core optimised (`[profile.dev]` in the root `Cargo.toml`): +unoptimised, the graph build took ~400 ms instead of ~50 ms, which made `flutter run` feel +slow. Judge UI speed in profile mode; debug Dart is several times slower by design. On a +240-page graph (profile), every interaction stays under 16 ms per frame except the first +hub opened (~75 ms, once), with RSS steady around 230–260 MB. Measured (profile, Linux, Impeller GL): 5 000 pages / 19 000 links settle in ~7 s and pan at build p50 6 ms / raster p50 4 ms per frame; a 360-page graph settles in ~1 s at ~2 ms/frame. Edges are drawn as one triangle mesh (`drawVertices`): stroking 10k+ lines made Impeller