// Emulator parse bench in the Dart VM: feeds generated output into // `Terminal.write` in the app's feed slices (16 Ki units) and prints MB/s // per workload. Pure Dart (the emulator core has no Flutter imports). // // dart run tool/perf/terminal_parse_bench.dart [--mb 16] [--workloads seq,yes,...] // // In V8, as the web build runs it: // dart compile js -O2 -Dworkloads=seq,yes tool/perf/terminal_parse_bench.dart -o /tmp/parse.js // node -e "globalThis.self = globalThis; require('/tmp/parse.js')" // // Workloads match tool/perf/terminal_stream_bench.dart. import 'package:xterm/core.dart'; void main(List args) { var megabytes = 16.0; var workloads = ['seq', 'yes', 'unicode', 'colors', 'screen', 'progress']; // Compiled to JavaScript (dart compile js, run in node) there are no // arguments: -Dworkloads=seq,yes picks them. const only = String.fromEnvironment('workloads'); if (only.isNotEmpty) workloads = only.split(','); for (var i = 0; i + 1 < args.length; i++) { if (args[i] == '--mb') megabytes = double.parse(args[i + 1]); if (args[i] == '--workloads') workloads = args[i + 1].split(','); } for (final workload in workloads) { final data = generateWorkload(workload, (megabytes * 1e6).round()); // Warm up, then measure the best of three. _run(data.substring(0, data.length ~/ 8)); var best = double.infinity; for (var round = 0; round < 3; round++) { final seconds = _run(data); if (seconds < best) best = seconds; } final mb = data.length / 1e6; // ignore: avoid_print print( '{"workload":"$workload","mb":${mb.toStringAsFixed(2)},' '"seconds":${best.toStringAsFixed(3)},' '"mbPerSecond":${(mb / best).toStringAsFixed(2)}}', ); } } double _run(String data) { final terminal = Terminal(maxLines: 10000); terminal.resize(158, 48); var notified = 0; terminal.addListener(() => notified++); const slice = 16 * 1024; final clock = Stopwatch()..start(); for (var at = 0; at < data.length; at += slice) { final end = at + slice < data.length ? at + slice : data.length; terminal.write(data.substring(at, end)); } clock.stop(); if (notified == 0) throw StateError('no writes'); return clock.elapsedMicroseconds / 1e6; } /// Generated output of about [size] UTF-16 units, as the web bench makes. String generateWorkload(String workload, int size) { final out = StringBuffer(); var counter = 0; while (out.length < size) { counter++; switch (workload) { case 'yes': out.write('y\r\n'); case 'unicode': out.write( '$counter é ñ 中文 \u{1F600} \u{1F44D}\u{1F3FD} ' '\u{1F1F5}\u{1F1F9} │ ╭─╮ ▓▒░ ' '\u{E0B0} done\r\n', ); case 'colors': final c = 31 + counter % 7; out.write( '\x1b[${c}m$counter\x1b[0m \x1b[1mbold\x1b[0m ' 'plain text line with some words\r\n', ); case 'screen': out.write('\x1b[H'); for (var row = 0; row < 48; row++) { final c = 31 + (row + counter) % 7; out.write( '\x1b[${c}m${(counter + row).toString().padLeft(8)}' '\x1b[0m \x1b[7m cpu ${row % 100}% \x1b[0m ' '${'process name and arguments ' * 4}\x1b[K\r\n', ); } case 'progress': if (counter == 1) { for (var row = 0; row < 47; row++) { out.write( '\x1b[3${row % 7 + 1}mline $row\x1b[0m ' '${'static output that does not change ' * 3}\r\n', ); } } final done = counter % 50; out.write( '\r\x1b[1m[${'#' * done}${'.' * (50 - done)}]\x1b[0m ' '$counter files', ); default: out.write('$counter\r\n'); } } return out.toString(); }