Skip to content

Commit e4ab99f

Browse files
Add benchmark (#2855)
* Add benchmark gem to Faker * Fix #2851 Add benchmark rake task for evaluating time execution. Fixes Co-authored-by: Rubens Fernandes <[email protected]> * 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 <[email protected]>
1 parent 918de1a commit e4ab99f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
55
# Specify your gem's dependencies in faker.gemspec
66
gemspec
77

8+
gem 'benchmark'
89
gem 'minitest', '5.20.0'
910
gem 'pry', '0.14.2'
1011
gem 'rake', '13.1.0'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ GEM
88
remote: https://rubygems.org/
99
specs:
1010
ast (2.4.2)
11+
benchmark (0.3.0)
1112
coderay (1.1.3)
1213
concurrent-ruby (1.2.2)
1314
docile (1.4.0)
@@ -64,6 +65,7 @@ PLATFORMS
6465
ruby
6566

6667
DEPENDENCIES
68+
benchmark
6769
faker!
6870
minitest (= 5.20.0)
6971
pry (= 0.14.2)

tasks/benchmark.rake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
# rubocop:disable Security/Eval,Style/EvalWithLocation
4+
5+
require 'benchmark'
6+
require 'faker'
7+
8+
desc 'Benchmarking every Faker generator'
9+
task :benchmark do
10+
all_methods = BenchmarkHelper.all_methods
11+
count = all_methods.count
12+
13+
Benchmark.bmbm do |x|
14+
x.report("Number of generators: #{count}") do
15+
100.times do
16+
all_methods.each { |method_name| eval(method_name) }
17+
end
18+
end
19+
end
20+
end
21+
22+
class BenchmarkHelper
23+
class << self
24+
def all_methods
25+
subclasses.map do |subclass|
26+
subclass_methods(subclass).flatten
27+
end.flatten.sort
28+
end
29+
30+
def subclasses
31+
Faker.constants.delete_if do |subclass|
32+
%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)
33+
end.sort
34+
end
35+
36+
def subclass_methods(subclass)
37+
eval("Faker::#{subclass}.public_methods(false) - Faker::Base.public_methods(false)").sort.map do |method|
38+
"Faker::#{subclass}.#{method}"
39+
end.sort
40+
end
41+
end
42+
end
43+
# rubocop:enable Security/Eval,Style/EvalWithLocation

0 commit comments

Comments
 (0)