require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'benchmark-ips', '~> 2.7', '>= 2.7.2' gem 'interactor', '~> 3.1', '>= 3.1.1' gem 'u-service', '~> 0.12.0' end require 'benchmark/ips' module IT class ConvertToNumbers include Interactor def call numbers = context.numbers if numbers.all? { |value| String(value) =~ /\d+/ } context.numbers = numbers.map(&:to_i) else context.fail! numbers: 'must contain only numeric types' end end end class Add2 include Interactor def call numbers = context.numbers context.numbers = numbers.map { |number| number + 2 } end end class Add2ToAllNumbers include Interactor::Organizer organize ConvertToNumbers, Add2 end end module MSB class ConvertToNumbers < Micro::Service::Base attribute :numbers def call! if numbers.all? { |value| String(value) =~ /\d+/ } Success(numbers: numbers.map(&:to_i)) else Failure(numbers: 'must contain only numeric types') end end end class Add2 < Micro::Service::Base attribute :numbers def call! Success(numbers: numbers.map { |number| number + 2 }) end end Add2ToAllNumbers = ConvertToNumbers >> Add2 end module MSS class ConvertToNumbers < Micro::Service::Strict attribute :numbers def call! if numbers.all? { |value| String(value) =~ /\d+/ } Success(numbers: numbers.map(&:to_i)) else Failure(numbers: 'must contain only numeric types') end end end class Add2 < Micro::Service::Strict attribute :numbers def call! Success(numbers: numbers.map { |number| number + 2 }) end end Add2ToAllNumbers = ConvertToNumbers >> Add2 end NUMBERS = {numbers: %w[1 1 2 2 3 4]} Benchmark.ips do |x| x.config(:time => 5, :warmup => 2) x.time = 5 x.warmup = 2 x.report('Interactor::Organizer') do IT::Add2ToAllNumbers.call(NUMBERS) end x.report('Pipeline of Micro::Service::Base') do MSB::Add2ToAllNumbers.call(NUMBERS) end x.report('Pipeline of Micro::Service::Strict') do MSS::Add2ToAllNumbers.call(NUMBERS) end x.compare! end # Warming up -------------------------------------- # Interactor::Organizer # 5.047k i/100ms # Pipeline of Micro::Service::Base # 8.069k i/100ms # Pipeline of Micro::Service::Strict # 6.706k i/100ms # Calculating ------------------------------------- # Interactor::Organizer # 50.656k (± 2.4%) i/s - 257.397k in 5.084184s # Pipeline of Micro::Service::Base # 83.309k (± 1.5%) i/s - 419.588k in 5.037749s # Pipeline of Micro::Service::Strict # 69.195k (± 1.7%) i/s - 348.712k in 5.041089s # Comparison: # Pipeline of Micro::Service::Base: 83309.3 i/s # Pipeline of Micro::Service::Strict: 69195.1 i/s - 1.20x slower # Interactor::Organizer: 50656.5 i/s - 1.64x slower