Skip to content

Commit

Permalink
Add a benchmarking script for comparing performance of formatters
Browse files Browse the repository at this point in the history
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
pdobb committed Jan 5, 2023
1 parent aece2a3 commit 773cac1
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Layout/LineLength:
Max: 80
Exclude:
- "object_identifier.gemspec"
- "scripts/benchmarking/**/*"

Layout/MultilineMethodCallBraceLayout:
EnforcedStyle: same_line
Expand All @@ -53,6 +54,10 @@ Lint/AmbiguousOperator:
Lint/AmbiguousRegexpLiteral:
Enabled: false # Conflicts with other rules.

Lint/OrAssignmentToConstant:
Exclude:
- "scripts/**/*"

Lint/Void:
CheckForMethodsWithNoSideEffects: true

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.5 - 2023-01-??
- Add support for defining customer Formatters.
- Add ObjectInspector::Configuration#formatter_class setting for overriding the default Formatter. See the README for more.
- Add a benchmarking script for comparing performance of formatters. See the README for more.

### 0.4.1 - 2022-12-30
- Make compatible with Ruby 3.2 (and likely Ruby 3.0 and 3.1 as well).
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GEM
specs:
ansi (1.5.0)
ast (2.4.2)
benchmark-ips (2.10.0)
builder (3.2.4)
byebug (11.1.3)
coderay (1.1.3)
Expand Down Expand Up @@ -64,6 +65,7 @@ PLATFORMS
ruby

DEPENDENCIES
benchmark-ips
bundler
byebug
minitest
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ OpenStruct.new(my_value: my_value_object).identify(:my_value)
ObjectIdentifier works great with the [ObjectInspector](https://github.com/pdobb/object_inspector) gem.


### Benchmarking Formatters

Performance of Formatters can be tested by playing the [Formatters Benchmarking Scripts](https://github.com/pdobb/object_identifier/tree/master/scripts/benchmarking) 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.

```ruby
custom_formatter_klasses = [MyCustomFormatter]

play scripts/benchmarking/formatters.rb
# ObjectIdentifier::StringFormatter
# 58.478k (± 0.8%) i/s - 295.776k in 5.058178s
# MyCustomFormatter
# ...
```


## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
1 change: 1 addition & 0 deletions object_identifier.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "benchmark-ips"
spec.add_development_dependency "bundler"
spec.add_development_dependency "byebug"
spec.add_development_dependency "minitest"
Expand Down
77 changes: 77 additions & 0 deletions scripts/benchmarking/formatters.rb
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"

0 comments on commit 773cac1

Please sign in to comment.