|
| 1 | +// Opt-in synthetic diagnostic; see README.md for invocation and limitations. |
| 2 | +import 'dart:async'; |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +import 'package:buzz/features/channels/channel_messages_provider.dart'; |
| 6 | +import 'package:buzz/shared/relay/relay.dart'; |
| 7 | +import 'package:flutter/foundation.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + test( |
| 13 | + 'buffered live channel delivery', |
| 14 | + () async { |
| 15 | + for (final historySize in [50, 500, 2000]) { |
| 16 | + for (final burstSize in [50, 200, 1000]) { |
| 17 | + // Discard the first two runs to reduce debug/JIT warm-up effects. |
| 18 | + for (var run = 0; run < 6; run++) { |
| 19 | + await _measureBurst(historySize, burstSize, run); |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + }, |
| 24 | + skip: !const bool.fromEnvironment('BUZZ_RUN_BENCHMARKS'), |
| 25 | + timeout: const Timeout(Duration(minutes: 3)), |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +Future<void> _measureBurst(int historySize, int burstSize, int run) async { |
| 30 | + final session = _SyntheticSession(historySize); |
| 31 | + final container = ProviderContainer( |
| 32 | + overrides: [relaySessionProvider.overrideWith(() => session)], |
| 33 | + ); |
| 34 | + try { |
| 35 | + final loaded = Completer<void>(); |
| 36 | + container.listen(channelMessagesProvider(_channelId), (_, next) { |
| 37 | + if (!loaded.isCompleted && next.value?.length == historySize) { |
| 38 | + loaded.complete(); |
| 39 | + } |
| 40 | + }); |
| 41 | + await loaded.future.timeout(const Duration(seconds: 5)); |
| 42 | + |
| 43 | + for (var index = 0; index < burstSize; index++) { |
| 44 | + session.debugHandleMessage([ |
| 45 | + 'EVENT', |
| 46 | + 'l-1', |
| 47 | + _event('live-$index', historySize + index).toJson(), |
| 48 | + ]); |
| 49 | + } |
| 50 | + final tick = Completer<int>(); |
| 51 | + final watch = Stopwatch()..start(); |
| 52 | + Timer.run(() => tick.complete(watch.elapsedMicroseconds)); |
| 53 | + session.debugFlushEventBuffer(); |
| 54 | + final synchronousUs = watch.elapsedMicroseconds; |
| 55 | + final eventLoopDelayUs = await tick.future; |
| 56 | + |
| 57 | + expect( |
| 58 | + container.read(channelMessagesProvider(_channelId)).value!.length, |
| 59 | + historySize + burstSize, |
| 60 | + ); |
| 61 | + if (run >= 2) { |
| 62 | + debugPrint( |
| 63 | + jsonEncode({ |
| 64 | + 'history': historySize, |
| 65 | + 'burst': burstSize, |
| 66 | + 'run': run - 2, |
| 67 | + 'synchronous_us': synchronousUs, |
| 68 | + 'event_loop_delay_us': eventLoopDelayUs, |
| 69 | + }), |
| 70 | + ); |
| 71 | + } |
| 72 | + } finally { |
| 73 | + container.dispose(); |
| 74 | + // The original 16 ms buffer timer remains armed after a debug flush. |
| 75 | + await Future<void>.delayed(const Duration(milliseconds: 20)); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const _channelId = '11111111-1111-4111-8111-111111111111'; |
| 80 | + |
| 81 | +NostrEvent _event(String id, int createdAt) => NostrEvent( |
| 82 | + id: id, |
| 83 | + pubkey: 'synthetic-author', |
| 84 | + createdAt: createdAt, |
| 85 | + kind: EventKind.streamMessageV2, |
| 86 | + tags: const [ |
| 87 | + ['h', _channelId], |
| 88 | + ], |
| 89 | + content: 'Synthetic message', |
| 90 | + sig: 'synthetic-signature', |
| 91 | +); |
| 92 | + |
| 93 | +class _SyntheticSession extends RelaySessionNotifier { |
| 94 | + final int historySize; |
| 95 | + |
| 96 | + _SyntheticSession(this.historySize); |
| 97 | + |
| 98 | + @override |
| 99 | + SessionState build() => const SessionState(status: SessionStatus.connected); |
| 100 | + |
| 101 | + @override |
| 102 | + Future<void Function()> subscribe( |
| 103 | + NostrFilter filter, |
| 104 | + void Function(NostrEvent) onEvent, { |
| 105 | + void Function(String message)? onClosed, |
| 106 | + }) { |
| 107 | + // Preserve the real subscription and buffer delivery implementation. This |
| 108 | + // fixture creates exactly one subscription, whose generated id is l-1. |
| 109 | + final ready = super.subscribe(filter, onEvent, onClosed: onClosed); |
| 110 | + debugHandleMessage(['EOSE', 'l-1']); |
| 111 | + return ready; |
| 112 | + } |
| 113 | + |
| 114 | + @override |
| 115 | + Future<List<NostrEvent>> queryRelay( |
| 116 | + List<NostrFilter> filters, { |
| 117 | + Duration timeout = const Duration(seconds: 8), |
| 118 | + }) async => [ |
| 119 | + // Model retained history in one synthetic page; real windows page by 50. |
| 120 | + for (var index = historySize - 1; index >= 0; index--) |
| 121 | + _event('history-$index', index), |
| 122 | + NostrEvent( |
| 123 | + id: 'bounds', |
| 124 | + pubkey: 'synthetic-relay', |
| 125 | + createdAt: 0, |
| 126 | + kind: EventKind.channelWindowBounds, |
| 127 | + tags: const [ |
| 128 | + ['d', '$_channelId:head'], |
| 129 | + ], |
| 130 | + content: jsonEncode({'has_more': false, 'next_cursor': null}), |
| 131 | + sig: 'synthetic-signature', |
| 132 | + ), |
| 133 | + ]; |
| 134 | +} |
0 commit comments