Class: BenchmarkSpec
- Inherits:
-
Object
- Object
- BenchmarkSpec
- Includes:
- DSL, SharedContext, Rack::Test::Methods
- Defined in:
- lib/benchmark_spec.rb,
lib/benchmark_spec/dsl.rb,
lib/benchmark_spec/shared_context.rb,
lib/benchmark_spec/output_formatter.rb,
lib/benchmark_spec/report_presenter.rb
Defined Under Namespace
Modules: DSL, SharedContext Classes: OutputFormatter, ReportPresenter
Instance Attribute Summary collapse
-
#after_hooks ⇒ Object
Returns the value of attribute after_hooks.
-
#before_hooks ⇒ Object
Returns the value of attribute before_hooks.
-
#config ⇒ Object
Returns the value of attribute config.
-
#description ⇒ Object
Returns the value of attribute description.
-
#execution ⇒ Object
Returns the value of attribute execution.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#nested_specs ⇒ Object
Returns the value of attribute nested_specs.
-
#reports ⇒ Object
Returns the value of attribute reports.
-
#tasks ⇒ Object
Returns the value of attribute tasks.
Class Method Summary collapse
- .configure(&block) ⇒ Object
- .describe(description, &block) ⇒ Object
- .evaluate_all ⇒ Object
- .load_files_recursively(path) ⇒ Object
- .run(args = []) ⇒ Object
Instance Method Summary collapse
- #after(hook = :each, &block) ⇒ Object
- #before(hook = :each, &block) ⇒ Object
- #benchmark(description, &block) ⇒ Object
- #describe(description, &block) ⇒ Object
- #evaluate ⇒ Object
-
#initialize(description, opts = {}) ⇒ BenchmarkSpec
constructor
A new instance of BenchmarkSpec.
- #run_all_tasks ⇒ Object
Methods included from SharedContext
Methods included from DSL
Constructor Details
#initialize(description, opts = {}) ⇒ BenchmarkSpec
Returns a new instance of BenchmarkSpec.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/benchmark_spec.rb', line 23 def initialize(description, opts = {}) @description = description @execution = opts[:execution] @before_hooks = opts[:before_hooks] || {} @after_hooks = opts[:after_hooks] || {} @tasks = [] @nested_specs = [] @reports = [] @formatter = opts[:formatter] || OutputFormatter.new @config ||= {} instance_exec @config, &config_proc end |
Instance Attribute Details
#after_hooks ⇒ Object
Returns the value of attribute after_hooks.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def after_hooks @after_hooks end |
#before_hooks ⇒ Object
Returns the value of attribute before_hooks.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def before_hooks @before_hooks end |
#config ⇒ Object
Returns the value of attribute config.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def config @config end |
#description ⇒ Object
Returns the value of attribute description.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def description @description end |
#execution ⇒ Object
Returns the value of attribute execution.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def execution @execution end |
#formatter ⇒ Object
Returns the value of attribute formatter.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def formatter @formatter end |
#nested_specs ⇒ Object
Returns the value of attribute nested_specs.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def nested_specs @nested_specs end |
#reports ⇒ Object
Returns the value of attribute reports.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def reports @reports end |
#tasks ⇒ Object
Returns the value of attribute tasks.
19 20 21 |
# File 'lib/benchmark_spec.rb', line 19 def tasks @tasks end |
Class Method Details
.configure(&block) ⇒ Object
41 42 43 |
# File 'lib/benchmark_spec.rb', line 41 def configure(&block) BenchmarkSpec.config_proc = block end |
.describe(description, &block) ⇒ Object
55 56 57 58 |
# File 'lib/benchmark_spec.rb', line 55 def describe(description, &block) @benchmarks ||= [] @benchmarks << BenchmarkSpec.new(description, execution: block) end |
.evaluate_all ⇒ Object
60 61 62 63 64 |
# File 'lib/benchmark_spec.rb', line 60 def evaluate_all return unless @benchmark @benchmarks.each {|b| b.evaluate} end |
.load_files_recursively(path) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/benchmark_spec.rb', line 45 def load_files_recursively(path) Dir[path].each do |file| if File.directory? file load_files_recursively file + "/*" else require file if File.basename(file).match /benchmark\.rb$/ end end end |
.run(args = []) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/benchmark_spec.rb', line 66 def run(args = []) base_path = Dir.pwd files_to_run = args.map{|path| "#{base_path}/#{path}"} files_to_run = ["#{base_path}/benchmark"] if files_to_run.empty? files_to_run.each do |f| load_files_recursively(f) end OutputFormatter.title("Benchmark Spec") evaluate_all OutputFormatter.title("Results") ensure ReportPresenter.print_results(@benchmarks) end |
Instance Method Details
#after(hook = :each, &block) ⇒ Object
91 92 93 94 |
# File 'lib/benchmark_spec.rb', line 91 def after(hook = :each, &block) after_hooks[hook] ||= [] after_hooks[hook] << block end |
#before(hook = :each, &block) ⇒ Object
86 87 88 89 |
# File 'lib/benchmark_spec.rb', line 86 def before(hook = :each, &block) before_hooks[hook] ||= [] before_hooks[hook] << block end |
#benchmark(description, &block) ⇒ Object
106 107 108 |
# File 'lib/benchmark_spec.rb', line 106 def benchmark(description, &block) tasks << { description: description, execution: block } end |
#describe(description, &block) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/benchmark_spec.rb', line 96 def describe(description, &block) = {} [:execution] = block if block [:before_hooks] = before_hooks.dup [:after_hooks] = after_hooks.dup [:formatter] = OutputFormatter.new(indent_level: formatter.indent_level + 1) nested_specs << BenchmarkSpec.new(description, ) end |
#evaluate ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/benchmark_spec.rb', line 128 def evaluate if execution formatter.print "#{description}".colorize(:light_blue) instance_eval &execution run_all_tasks nested_specs.each do |ns| ns.evaluate end else pending(description) end end |
#run_all_tasks ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/benchmark_spec.rb', line 110 def run_all_tasks return if tasks.empty? tasks.each do |t| begin pending t[:description] if t[:execution].nil? formatter.print "running #{t[:description]} ..." before_each_hooks.each {|b| b.call} reports << Benchmark.measure(t[:description]) { t[:execution].call } after_each_hooks.each {|a| a.call} rescue => e formatter.print "Error: #{e.message}".colorize(:red) end end end |