Class: ZTK::Benchmark
- Inherits:
-
Object
- Object
- ZTK::Benchmark
- Defined in:
- lib/ztk/benchmark.rb
Overview
Benchmark Class
This class contains a benchmarking tool which doubles to supply indications of activity to the console user during long running tasks.
It can be run strictly for benchmarking (the default) or if supplied with the appropriate options it will display output to the user while benchmarking the supplied block.
Class Method Summary (collapse)
-
+ (Float) bench(options = {}) { ... }
Benchmark the supplied block.
Class Method Details
+ (Float) bench(options = {}) { ... }
Benchmark the supplied block.
If message and mark options are used, then the message text will be displayed to the user. The the supplied block is yielded inside a ZTK::Spinner.spin call. This will provide the spinning cursor while the block executes. It is advisable to not have output sent to the console during this period.
Once the block finishes executing, the mark text is displayed with the benchmark supplied to it as a sprintf option. One could use "%0.4f" in a String for example to get the benchmark time embedded in it
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ztk/benchmark.rb', line 60 def bench(={}, &block) = Base.build_config({ :use_spinner => true }, ) !block_given? and Base.log_and_raise(.ui.logger, BenchmarkError, "You must supply a block!") if (!..nil? && !..empty?) .ui.stdout.print(.) .ui.logger.info { ..strip } end benchmark = ::Benchmark.realtime do if .use_spinner ZTK::Spinner.spin() do yield end else yield end end if (!.mark.nil? && !.mark.empty?) .ui.stdout.print(.mark % benchmark) .ui.logger.info { .mark.strip % benchmark } end benchmark end |