rewinfrey/ActionLogic

Update README to document new ActionLogic::Configuration

Closed this issue · 1 comments

For now this only includes benchmarking configuration options:

  • benchmark
  • benchmark_log
  • benchmark_formatter

By default benchmark is false (this must be explicitly set to true to enable benchmarking functionality):

ActionLogic::ActionConfiguration.configure do |config|
  config.benchmark = true
end

By default benchmark_log uses stdout unless a Ruby IO object is specified (like a File):

ActionLogic::ActionConfiguration.configure do |config|
  config.benchmark = true
  config.benchmark_log = File.open("benchmark.log", "w")
end

By default benchmark_formatter uses the ActionLogic::ActionBenchmark::DefaultFormatter. Any custom formatters should subclass ActionLogic::ActionBenchmark::DefaultFormatter or verify that the custom formatter class implements the same interface.

class CustomFormatter < ActionLogic::ActionBenchmark::DefaultFormatter

  # Custom formatters require you to define three methods:
  # `task`, `use_case` and `coordinator`

  # Each of the format methods receives two inputs:
  #1. `benchmark_result` :  type is a Ruby stdlib Benchmark instance

  #2. `context_name` :  type is a Ruby String representing the class name of the execution
  # context (`ActionTask`, `ActionUseCase`, or `ActionCoordinator`  class)

  # A `benchmark_log` attr_reader is provided to write to within the `task`, `use_case` or 
  # `coordinator` methods.

  def task(benchmark_result, context_name)
    benchmark_log.puts("[task] #{context_name}: #{benchmark_result.real}")
  end

  def use_case(benchmark_result, context_name)
    benchmark_log.puts("[use case] #{context_name}: #{benchmark_result.real}")
  end

  def coordinator(benchmark_result, context_name)
    benchmark_log.puts("[coordinator] #{context_name}: #{benchmark_result.real}")
  end
end

# To use the CustomFormatter formatter class, we can configure ActionLogic like so:
ActionLogic::ActionConfiguration.configure do |config|
  config.benchmark = true
  config.benchmark_log = File.open("benchmark.log", "w")
  config.benchmark_formatter = CustomFormatter
end

# Using this configuration with the test suite yields a benchmark output formatted like so:

SimpleTestTask: 3.322999691590667e-05
SimpleTestUseCase: 0.00011545396409928799
UseCaseTestTask1: 2.9074028134346008e-05
UseCaseTestTask2: 2.7913018129765987e-05
SimpleTestUseCase2: 0.00011622201418504119
SimpleTestTask: 6.190297426655889e-05
SimpleTestUseCase: 0.00018308503786101937
...

# If no custom formatter is defined, ActionLogic provides a default formatter
# whose output is a machine readable format for easy parsing:

context:   SimpleTestTask                                     user_time: 0.000000   system_time: 0.000000   total_time: 0.000000   real_time: 0.000048
context:   SimpleTestTask                                     user_time: 0.000000   system_time: 0.000000   total_time: 0.000000   real_time: 0.000030
context:   FailureTestTask                                    user_time: 0.000000   system_time: 0.000000   total_time: 0.000000   real_time: 0.000038
context:   ErrorHandlerTestTask                               user_time: 0.000000   system_time: 0.000000   total_time: 0.000000   real_time: 0.000042
context:   ValidateAroundCustomTypeTestTask                   user_time: 0.000000   system_time: 0.000000   total_time: 0.000000   real_time: 0.000044
context:   ValidateAroundTestTask                             user_time: 0.000000   system_time: 0.000000   total_time: 0.000000   real_time: 0.000164
...

This behavior has been added.

Additionally, you can also specify a custom benchmark_handler (in v.0.2.5)