@@ -57,15 +57,22 @@ module Benchmark
57
57
end
58
58
59
59
class Job
60
+ property fullscreen_report : Bool = false
61
+ property scrollback : Int32 = 0
62
+
60
63
def initialize (calculation = 5 , warmup = 2 , interactive = STDOUT .tty?)
61
64
@interactive = !! interactive
62
65
@warmup_time = warmup.seconds
63
66
@calculation_time = calculation.seconds
64
67
@items = [] of Entry
65
- print " \e [s" if @interactive
68
+ at_exit do
69
+ cleanup_on_exit
70
+ end
66
71
end
67
72
68
73
def separator (label ) : Benchmark ::IPS ::Entry
74
+ self .fullscreen_report = true
75
+
69
76
item = Entry .new(label, - > (x : Int32 ) {})
70
77
item.separator = true
71
78
@items << item
@@ -74,6 +81,8 @@ module Benchmark
74
81
end
75
82
76
83
def separator (label, & action : Proc (Int32 , Nil )) : Benchmark ::IPS ::Entry
84
+ self .fullscreen_report = true
85
+
77
86
item = Entry .new(label, action)
78
87
item.separator = true
79
88
@items << item
@@ -88,8 +97,9 @@ module Benchmark
88
97
end
89
98
90
99
def report : Nil
91
- print " \e [2J\e [H" if @interactive
92
100
max_label = ran_items.max_of { |item | count_non_control_characters(item.label) }
101
+ print " \e [H\e [0J\e [?7l" if @interactive && fullscreen_report
102
+ puts " #{ " " .rjust(max_label - @items .size)} Benchmark Report |#{ " =" * ran_items.size} #{ ran_items.size < @items .size ? " #" : " " } #{ " -" * (@items .size - ran_items.size)} |"
93
103
max_compare = ran_items.max_of & .human_compare.size
94
104
max_bytes_per_op = ran_items.max_of & .bytes_per_op.humanize(base: 1024 ).size
95
105
@@ -110,8 +120,17 @@ module Benchmark
110
120
end
111
121
end
112
122
123
+ private def cleanup_on_exit
124
+ print " \e [?7h"
125
+ end
126
+
127
+ private def calculate_scrollback
128
+ self .scrollback = ran_items.size
129
+ ran_items.select { |item | self .scrollback += item.label.to_s.scan(/\n / ).size }
130
+ end
131
+
113
132
private def run_warmup
114
- print " \e [2J \e [H" if @interactive
133
+ print " \e [?7l \e [H\e [0J " if fullscreen_report
115
134
@items .each_with_index do |item , index |
116
135
print " \r Warming up [\e [48;5;231m#{ " " * index} \e [0m\e [48;5;240m#{ " " * (@items .size - index - 1 )} \e [0m]" if @interactive
117
136
next if item.separator?
@@ -130,7 +149,7 @@ module Benchmark
130
149
131
150
item.set_cycles(elapsed, count)
132
151
end
133
- print " \e [2K"
152
+ print " \e [2K\e [H "
134
153
end
135
154
136
155
private def run_calculation
@@ -165,6 +184,8 @@ module Benchmark
165
184
item.bytes_per_op = (bytes.to_f / cycles.to_f).round.to_u64
166
185
167
186
if @interactive
187
+ calculate_scrollback
188
+ print " \e [#{ scrollback + 1 } A\e [0J" if ! fullscreen_report
168
189
run_comparison
169
190
report
170
191
end
0 commit comments