-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a benchmarking script for comparing performance of formatters
Performance of Formatters can be tested by playing the Formatters Benchmarking Scripts in the pry console for this gem. Custom Formatters may be similarly gauged for comparison by adding them to the `custom_formatter_klasses` array before playing the script. Example: ```ruby custom_formatter_klasses = [MyCustomFormatter] play scripts/benchmarking/formatters.rb ```
- Loading branch information
Showing
6 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
# Play from the pry console with: | ||
# play scripts/benchmarking/formatters.rb | ||
|
||
require "benchmark/ips" | ||
|
||
custom_formatter_klasses ||= [] | ||
|
||
formatter_klasses = [ | ||
ObjectIdentifier::StringFormatter, | ||
*Array(custom_formatter_klasses) | ||
].freeze | ||
|
||
MyObject ||= Struct.new(:id, :name) | ||
|
||
objects = [ | ||
MyObject.new(id: 1, name: "NAME1"), | ||
MyObject.new(id: 2, name: "NAME2"), | ||
MyObject.new(id: 3, name: "NAME3") | ||
].freeze | ||
|
||
puts "== Averaged =============================================================" | ||
Benchmark.ips { |x| | ||
formatter_klasses.each do |formatter_klass| | ||
x.report(formatter_klass) { | ||
formatter_klass.new(objects[0]).call | ||
formatter_klass.new(objects[0], %i[id name]).call | ||
formatter_klass.new(objects[0], klass: "CustomClass").call | ||
formatter_klass.new(objects[0], %i[id name], klass: "CustomClass").call | ||
formatter_klass.new(objects, limit: 2).call | ||
formatter_klass.new(objects, %i[id name], klass: "CustomClass", limit: 2).call | ||
} | ||
end | ||
|
||
x.compare! | ||
} | ||
puts "== Done" | ||
|
||
puts "== Individualized =======================================================" | ||
Benchmark.ips { |x| | ||
# rubocop:disable Style/CombinableLoops | ||
formatter_klasses.each do |formatter_klass| | ||
x.report("#{formatter_klass} - Default Attributes") { | ||
formatter_klass.new(objects[0]).call | ||
} | ||
end | ||
formatter_klasses.each do |formatter_klass| | ||
x.report("#{formatter_klass} - Custom Attributes") { | ||
formatter_klass.new(objects[0], %i[id name]).call | ||
} | ||
end | ||
formatter_klasses.each do |formatter_klass| | ||
x.report("#{formatter_klass} - Custom Class") { | ||
formatter_klass.new(objects[0], klass: "CustomClass").call | ||
} | ||
end | ||
formatter_klasses.each do |formatter_klass| | ||
x.report("#{formatter_klass} - Custom Attributes & Custom Class") { | ||
formatter_klass.new(objects[0], %i[id name], klass: "CustomClass").call | ||
} | ||
end | ||
formatter_klasses.each do |formatter_klass| | ||
x.report("#{formatter_klass} - Limit 2") { | ||
formatter_klass.new(objects, limit: 2).call | ||
} | ||
end | ||
formatter_klasses.each do |formatter_klass| | ||
x.report("#{formatter_klass} - Custom Attributes & Custom Class & Limit 2") { | ||
formatter_klass.new(objects, %i[id name], klass: "CustomClass", limit: 2).call | ||
} | ||
end | ||
# rubocop:enable Style/CombinableLoops | ||
|
||
x.compare! | ||
} | ||
puts "== Done" |