Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark #2855

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in faker.gemspec
gemspec

gem 'benchmark'
gem 'minitest', '5.20.0'
gem 'pry', '0.14.2'
gem 'rake', '13.1.0'
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
remote: https://rubygems.org/
specs:
ast (2.4.2)
benchmark (0.3.0)
coderay (1.1.3)
concurrent-ruby (1.2.2)
docile (1.4.0)
Expand Down Expand Up @@ -64,6 +65,7 @@ PLATFORMS
ruby

DEPENDENCIES
benchmark
faker!
minitest (= 5.20.0)
pry (= 0.14.2)
Expand Down
43 changes: 43 additions & 0 deletions tasks/benchmark.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# rubocop:disable Security/Eval,Style/EvalWithLocation

require 'benchmark'
require 'faker'

desc 'Benchmarking every Faker generator'
task :benchmark do
all_methods = BenchmarkHelper.all_methods
count = all_methods.count

Benchmark.bmbm do |x|
x.report("Number of generators: #{count}") do
100.times do
all_methods.each { |method_name| eval(method_name) }
end
end
end
end

class BenchmarkHelper
class << self
def all_methods
subclasses.map do |subclass|
subclass_methods(subclass).flatten
end.flatten.sort
end

def subclasses
Faker.constants.delete_if do |subclass|
%i[Base Bank Books Cat Char Base58 ChileRut CLI Config Creature Date Dog DragonBall Dota ElderScrolls Fallout Games GamesHalfLife HeroesOfTheStorm Internet JapaneseMedia LeagueOfLegends Movies Myst Overwatch OnePiece Pokemon Religion Sports SwordArtOnline TvShows Time VERSION Witcher WorldOfWarcraft Zelda].include?(subclass)
end.sort
end

def subclass_methods(subclass)
eval("Faker::#{subclass}.public_methods(false) - Faker::Base.public_methods(false)").sort.map do |method|
"Faker::#{subclass}.#{method}"
end.sort
end
end
end
# rubocop:enable Security/Eval,Style/EvalWithLocation