Class: Benchmarker::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmarker.rb

Instance Method Summary collapse

Constructor Details

#initialize(bm = nil) ⇒ Scope

Returns a new instance of Scope.



447
448
449
# File 'lib/benchmarker.rb', line 447

def initialize(bm=nil)
  @__bm = bm
end

Instance Method Details

#after(&block) ⇒ Object



488
489
490
491
# File 'lib/benchmarker.rb', line 488

def after(&block)
  #; [!05up6] defines 'after' hook.
  @__bm.define_hook(:after, &block)
end

#after_all(&block) ⇒ Object



498
499
500
501
# File 'lib/benchmarker.rb', line 498

def after_all(&block)
  #; [!z7xop] defines 'after_all' hook.
  @__bm.define_hook(:after_all, &block)
end

#assert(expr, errmsg) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/benchmarker.rb', line 508

def assert(expr, errmsg)
  #; [!a0c7e] do nothing if assertion succeeded.
  #; [!5vmbc] raises error if assertion failed.
  #; [!7vt5l] puts newline if assertion failed.
  return if expr
  puts ""
  raise ValidationFailed, errmsg
rescue => exc
  #; [!mhw59] makes error backtrace compact.
  exc.backtrace.reject! {|x| x =~ /[\/\\:]benchmarker\.rb.*:/ }
  raise
end

#assert_eq(actual, expected, errmsg = nil) ⇒ Object



475
476
477
478
479
480
481
# File 'lib/benchmarker.rb', line 475

def assert_eq(actual, expected, errmsg=nil)
  #; [!8m6bh] do nothing if ectual == expected.
  #; [!f9ey6] raises error unless actual == expected.
  return if actual == expected
  errmsg ||= "#{actual.inspect} == #{expected.inspect}: failed."
  assert false, errmsg
end

#before(&block) ⇒ Object



483
484
485
486
# File 'lib/benchmarker.rb', line 483

def before(&block)
  #; [!2ir4q] defines 'before' hook.
  @__bm.define_hook(:before, &block)
end

#before_all(&block) ⇒ Object



493
494
495
496
# File 'lib/benchmarker.rb', line 493

def before_all(&block)
  #; [!1oier] defines 'before_all' hook.
  @__bm.define_hook(:before_all, &block)
end

#empty_task(code = nil, binding = nil, &block) ⇒ Object



470
471
472
473
# File 'lib/benchmarker.rb', line 470

def empty_task(code=nil, binding=nil, &block)
  #; [!ycoch] creates new empty-loop task object.
  return task(nil, code, binding, &block)
end

#task(name, code = nil, binding = nil, tag: nil, skip: nil, &block) ⇒ Object Also known as: report



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/benchmarker.rb', line 451

def task(name, code=nil, binding=nil, tag: nil, skip: nil, &block)
  #; [!843ju] when code argument provided...
  if code
    #; [!bwfak] code argument and block argument are exclusive.
    ! block_given?()  or
      raise TaskError, "task(#{name.inspect}): cannot accept #{code.class} argument when block argument given."
    #; [!4dm9q] generates block argument if code argument passed.
    location = caller_locations(1, 1).first
    defcode  = "proc do #{(code+';') * N_REPEAT} end"   # repeat code 100 times
    binding ||= ::TOPLEVEL_BINDING
    block = eval defcode, binding, location.path, location.lineno+1
  end
  #; [!kh7r9] define empty-loop task if name is nil.
  return @__bm.define_empty_task(code, tag: tag, skip: skip, &block) if name.nil?
  #; [!j6pmr] creates new task object.
  return @__bm.define_task(name, code, tag: tag, skip: skip, &block)
end

#validate(&block) ⇒ Object



503
504
505
506
# File 'lib/benchmarker.rb', line 503

def validate(&block)
  #; [!q2aev] defines validator.
  return @__bm.define_hook(:validate, &block)
end