benchmark.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'dart:io';
  2. import 'package:xterm/xterm.dart';
  3. void main() {
  4. // BenchmarkWrite().run();
  5. // BenchmarkWrite2().run();
  6. // BenchmarkWriteBuffer().run();
  7. BenchmarkWriteCMatrix().run();
  8. }
  9. abstract class Benchmark {
  10. String explain();
  11. void benchmark();
  12. void run() {
  13. print('benchmark: ${explain()}');
  14. print('preheating...');
  15. benchmark();
  16. final sw = Stopwatch()..start();
  17. print('running...');
  18. benchmark();
  19. sw.stop();
  20. print('result: ${sw.elapsedMilliseconds} ms');
  21. }
  22. }
  23. class BenchmarkWrite extends Benchmark {
  24. static const cycle = 1 << 20;
  25. static const data = 'hello world';
  26. String explain() {
  27. return "write '$data' to Terminal for $cycle times";
  28. }
  29. void benchmark() {
  30. final terminal = Terminal();
  31. for (var i = 0; i < cycle; i++) {
  32. terminal.write(data);
  33. }
  34. }
  35. }
  36. class BenchmarkWrite2 extends Benchmark {
  37. static const cycle = 100000;
  38. static const data = '100000';
  39. String explain() {
  40. return "write '$data' to Terminal for $cycle times";
  41. }
  42. void benchmark() {
  43. final terminal = Terminal();
  44. for (var i = 0; i < cycle; i++) {
  45. terminal.write(data);
  46. }
  47. }
  48. }
  49. class BenchmarkWriteCMatrix extends Benchmark {
  50. BenchmarkWriteCMatrix() {
  51. data = File('script/cmatrix.txt').readAsStringSync();
  52. }
  53. static const cycle = 1 << 0;
  54. late final String data;
  55. String explain() {
  56. return 'write ${data.length / 1024} kb CMatrix output to Terminal for $cycle time(s)';
  57. }
  58. void benchmark() {
  59. final terminal = Terminal();
  60. for (var i = 0; i < cycle; i++) {
  61. terminal.write(data);
  62. }
  63. }
  64. }
  65. class BenchmarkWriteBuffer extends Benchmark {
  66. static const cycle = 1 << 20;
  67. static const data = 'hello world';
  68. String explain() {
  69. return "write '$data' to StringBuffer for $cycle times";
  70. }
  71. void benchmark() {
  72. final buffer = StringBuffer();
  73. for (var i = 0; i < cycle; i++) {
  74. buffer.write(data);
  75. }
  76. }
  77. }