From e4ab99f0c356ccfe62c6bf7da80575e989c3ecd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=B3n=20Charabati?= Date: Tue, 21 Nov 2023 16:53:05 -0600 Subject: [PATCH] Add benchmark (#2855) * Add benchmark gem to Faker * Fix #2851 Add benchmark rake task for evaluating time execution. Fixes Co-authored-by: Rubens Fernandes * 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 --- Gemfile | 1 + Gemfile.lock | 2 ++ tasks/benchmark.rake | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 tasks/benchmark.rake diff --git a/Gemfile b/Gemfile index 70564a2869..c2aa9980af 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 5f674054e4..0e8ad5a04d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -64,6 +65,7 @@ PLATFORMS ruby DEPENDENCIES + benchmark faker! minitest (= 5.20.0) pry (= 0.14.2) diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake new file mode 100644 index 0000000000..ca2aef913a --- /dev/null +++ b/tasks/benchmark.rake @@ -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