|
| 1 | +import 'dart:typed_data'; |
| 2 | +import 'dart:math'; |
| 3 | + |
| 4 | +const _minIterations = 50; |
| 5 | +const _maxIterations = 1000; |
| 6 | + |
| 7 | +Future<void> execute() async { |
| 8 | + print('Envelope Builder Benchmark'); |
| 9 | + print('=========================='); |
| 10 | + print('Comparing legacy List<int> vs BytesBuilder approaches\n'); |
| 11 | + |
| 12 | + // Test with different envelope sizes |
| 13 | + final sizes = [ |
| 14 | + (1024, '1 KB'), |
| 15 | + (10 * 1024, '10 KB'), |
| 16 | + (100 * 1024, '100 KB'), |
| 17 | + (1024 * 1024, '1 MB'), |
| 18 | + (5 * 1024 * 1024, '5 MB'), |
| 19 | + ]; |
| 20 | + |
| 21 | + for (final (size, label) in sizes) { |
| 22 | + print('Envelope size: $label'); |
| 23 | + print('-' * 40); |
| 24 | + |
| 25 | + // Use adaptive iteration count based on data size |
| 26 | + final iterations = _getIterationCount(size); |
| 27 | + print('Running $iterations iterations...'); |
| 28 | + |
| 29 | + // Create mock envelope data |
| 30 | + final mockData = _generateMockEnvelopeData(size); |
| 31 | + |
| 32 | + // Benchmark legacy approach |
| 33 | + final legacyResults = await _benchmarkLegacyApproach(mockData, iterations); |
| 34 | + final legacyAvg = |
| 35 | + legacyResults.reduce((a, b) => a + b) / legacyResults.length; |
| 36 | + final legacyMin = legacyResults.reduce(min); |
| 37 | + final legacyMax = legacyResults.reduce(max); |
| 38 | + |
| 39 | + // Benchmark new approach |
| 40 | + final newResults = await _benchmarkNewApproach(mockData, iterations); |
| 41 | + final newAvg = newResults.reduce((a, b) => a + b) / newResults.length; |
| 42 | + final newMin = newResults.reduce(min); |
| 43 | + final newMax = newResults.reduce(max); |
| 44 | + |
| 45 | + // Calculate improvement |
| 46 | + final improvement = |
| 47 | + ((legacyAvg - newAvg) / legacyAvg * 100).toStringAsFixed(1); |
| 48 | + final speedup = (legacyAvg / newAvg).toStringAsFixed(2); |
| 49 | + |
| 50 | + print('Legacy approach (List<int> + addAll):'); |
| 51 | + print(' Average: ${_formatMicroseconds(legacyAvg)}'); |
| 52 | + print(' Min: ${_formatMicroseconds(legacyMin)}'); |
| 53 | + print(' Max: ${_formatMicroseconds(legacyMax)}'); |
| 54 | + |
| 55 | + print('New approach (BytesBuilder):'); |
| 56 | + print(' Average: ${_formatMicroseconds(newAvg)}'); |
| 57 | + print(' Min: ${_formatMicroseconds(newMin)}'); |
| 58 | + print(' Max: ${_formatMicroseconds(newMax)}'); |
| 59 | + |
| 60 | + print('Performance improvement: $improvement% (${speedup}x faster)'); |
| 61 | + print(''); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Adaptive iteration count to avoid memory pressure and hanging |
| 66 | +int _getIterationCount(int dataSize) { |
| 67 | + if (dataSize <= 10 * 1024) { |
| 68 | + return _maxIterations; // 1K iterations for <= 10KB |
| 69 | + } else if (dataSize <= 100 * 1024) { |
| 70 | + return _maxIterations ~/ 2; // 500 iterations for <= 100KB |
| 71 | + } else if (dataSize <= 1024 * 1024) { |
| 72 | + return _maxIterations ~/ 5; // 200 iterations for <= 1MB |
| 73 | + } else { |
| 74 | + return _minIterations; // 50 iterations for > 1MB |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Generate mock envelope data chunks to simulate streaming |
| 79 | +List<List<int>> _generateMockEnvelopeData(int totalSize) { |
| 80 | + final chunks = <List<int>>[]; |
| 81 | + final random = Random(42); // Fixed seed for reproducibility |
| 82 | + |
| 83 | + // Simulate realistic chunk sizes (similar to how envelope streams work) |
| 84 | + final chunkSizes = [64, 128, 256, 512, 1024]; |
| 85 | + var remaining = totalSize; |
| 86 | + |
| 87 | + while (remaining > 0) { |
| 88 | + final chunkSize = chunkSizes[random.nextInt(chunkSizes.length)]; |
| 89 | + final actualSize = remaining < chunkSize ? remaining : chunkSize; |
| 90 | + |
| 91 | + // Create chunk with random data |
| 92 | + final chunk = List<int>.generate(actualSize, (_) => random.nextInt(256)); |
| 93 | + chunks.add(chunk); |
| 94 | + remaining -= actualSize; |
| 95 | + } |
| 96 | + |
| 97 | + return chunks; |
| 98 | +} |
| 99 | + |
| 100 | +Future<List<double>> _benchmarkLegacyApproach( |
| 101 | + List<List<int>> chunks, int iterations) async { |
| 102 | + final results = <double>[]; |
| 103 | + |
| 104 | + // Reduced warmup for large data |
| 105 | + final warmupIterations = min(20, iterations ~/ 5); |
| 106 | + |
| 107 | + // Warmup |
| 108 | + for (var i = 0; i < warmupIterations; i++) { |
| 109 | + _runLegacyApproach(chunks); |
| 110 | + } |
| 111 | + |
| 112 | + // Actual benchmark |
| 113 | + for (var i = 0; i < iterations; i++) { |
| 114 | + final stopwatch = Stopwatch()..start(); |
| 115 | + _runLegacyApproach(chunks); |
| 116 | + stopwatch.stop(); |
| 117 | + results.add(stopwatch.elapsedMicroseconds.toDouble()); |
| 118 | + } |
| 119 | + |
| 120 | + return results; |
| 121 | +} |
| 122 | + |
| 123 | +Future<List<double>> _benchmarkNewApproach( |
| 124 | + List<List<int>> chunks, int iterations) async { |
| 125 | + final results = <double>[]; |
| 126 | + |
| 127 | + // Reduced warmup for large data |
| 128 | + final warmupIterations = min(20, iterations ~/ 5); |
| 129 | + |
| 130 | + // Warmup |
| 131 | + for (var i = 0; i < warmupIterations; i++) { |
| 132 | + _runNewApproach(chunks); |
| 133 | + } |
| 134 | + |
| 135 | + // Actual benchmark |
| 136 | + for (var i = 0; i < iterations; i++) { |
| 137 | + final stopwatch = Stopwatch()..start(); |
| 138 | + _runNewApproach(chunks); |
| 139 | + stopwatch.stop(); |
| 140 | + results.add(stopwatch.elapsedMicroseconds.toDouble()); |
| 141 | + } |
| 142 | + |
| 143 | + return results; |
| 144 | +} |
| 145 | + |
| 146 | +Uint8List _runLegacyApproach(List<List<int>> chunks) { |
| 147 | + final envelopeData = <int>[]; |
| 148 | + for (final chunk in chunks) { |
| 149 | + envelopeData.addAll(chunk); |
| 150 | + } |
| 151 | + return Uint8List.fromList(envelopeData); |
| 152 | +} |
| 153 | + |
| 154 | +Uint8List _runNewApproach(List<List<int>> chunks) { |
| 155 | + final builder = BytesBuilder(copy: false); |
| 156 | + for (final chunk in chunks) { |
| 157 | + builder.add(chunk); |
| 158 | + } |
| 159 | + return builder.takeBytes(); |
| 160 | +} |
| 161 | + |
| 162 | +String _formatMicroseconds(double microseconds) { |
| 163 | + if (microseconds < 1000) { |
| 164 | + return '${microseconds.toStringAsFixed(1)} μs'; |
| 165 | + } else if (microseconds < 1000000) { |
| 166 | + return '${(microseconds / 1000).toStringAsFixed(2)} ms'; |
| 167 | + } else { |
| 168 | + return '${(microseconds / 1000000).toStringAsFixed(2)} s'; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +void main() async { |
| 173 | + await execute(); |
| 174 | +} |
0 commit comments