-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add benchmark gem to Faker * Fix #2851 Add benchmark rake task for evaluating time execution. Fixes Co-authored-by: Rubens Fernandes <rubens.fernandes97@gmail.com> * Commit PR suggestions by Thiago. - Build all_methods outside the benchmark since we don't want to measure that. * Commit PR suggestions by Steffani. - Avoid calling the same object twice --------- Co-authored-by: Rubens Fernandes <rubens.fernandes97@gmail.com>
- Loading branch information
1 parent
918de1a
commit e4ab99f
Showing
3 changed files
with
46 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
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 |