Skip to content

Commit a109a55

Browse files
Reorganize benchmark scripts
Having these in a folder helps because we can document experiment results in it as well. And we can edit the require script to raise an error if it takes longer than a threshold to load faker. Co-Authored-By: Thiago Araujo <thd.araujo@gmail.com>
1 parent f09c7b1 commit a109a55

6 files changed

Lines changed: 93 additions & 67 deletions

File tree

.github/workflows/bench.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Bench
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
name: Benchmarks
13+
steps:
14+
- uses: actions/checkout@v6
15+
- uses: ruby/setup-ruby@v1
16+
with:
17+
ruby-version: '3.4'
18+
- name: Install dependencies
19+
run: bundle install
20+
21+
- name: Generators
22+
run: RUBYOPT="-W0" bundle exec ruby benchmark/generators.rb
23+
- name: Require
24+
run: RUBYOPT="-W0" bundle exec ruby benchmark/require.rb
25+

.rubocop.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Naming/VariableNumber:
5050
Description: Use the configured style when numbering symbols, methods and variables.
5151
Enabled: false
5252

53+
Security/Eval:
54+
Description: The use of eval represents a serious security risk.
55+
Exclude:
56+
- 'lib/faker/default/json.rb'
57+
- 'benchmark/generators.rb'
58+
5359
Style/AsciiComments:
5460
Description: This cop checks for non-ascii (non-English) characters in comments.
5561
Exclude:
@@ -71,6 +77,7 @@ Style/EvalWithLocation:
7177
Description: This cop checks eval method usage. eval can receive source location metadata, that are filename and line number.
7278
Exclude:
7379
- 'lib/faker/default/json.rb'
80+
- 'benchmark/generators.rb'
7481

7582
Style/FormatStringToken:
7683
Description: This cop checks for a consistent style for named format string tokens.
@@ -102,11 +109,6 @@ Style/RegexpLiteral:
102109
- mixed
103110
AllowInnerSlashes: false
104111

105-
Security/Eval:
106-
Description: The use of eval represents a serious security risk.
107-
Exclude:
108-
- 'lib/faker/default/json.rb'
109-
110112
Style/IfUnlessModifier:
111113
Description: Checks for `if` and `unless` statements that would fit on one line if written as modifier `if`/`unless`. The cop also checks for modifier `if`/`unless` lines that exceed the maximum line length.
112114
Enabled: false

benchmark/generators.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'benchmark/ips'
4+
require 'faker'
5+
6+
def all_generators
7+
generators.map do |klass|
8+
subclass_methods(klass).flatten
9+
end.flatten.sort
10+
end
11+
12+
def generators
13+
Faker.constants.delete_if do |subclass|
14+
%i[
15+
Base
16+
Cat
17+
Char
18+
Base58
19+
Config
20+
Date
21+
Deprecator
22+
Dog
23+
Religion
24+
Time
25+
VERSION
26+
].include?(subclass)
27+
end.sort
28+
end
29+
30+
def subclass_methods(subclass)
31+
eval("Faker::#{subclass}.public_methods(false) - Faker::Base.public_methods(false)").sort.map do |method|
32+
"Faker::#{subclass}.#{method}"
33+
end.sort
34+
end
35+
36+
Benchmark.ips do |x|
37+
x.report("Number of generators: #{all_generators.count}") do
38+
all_generators.each { |generator| eval(generator) }
39+
end
40+
end

benchmark/load_yml_vs_json.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'benchmark/ips'
4+
require 'json'
5+
require 'yaml'
6+
7+
Benchmark.ips do |x|
8+
x.report('YML') { YAML.load_file(File.expand_path("#{File.dirname(__FILE__)}/../lib/locales/es-MX.yml")) }
9+
x.report('JSON') { JSON.load_file("#{File.dirname(__FILE__)}/../test/fixtures/locales/es-MX.json") }
10+
11+
x.compare!(order: :baseline)
12+
end

benchmark/require.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require 'benchmark'
4+
5+
ms = Benchmark.realtime do
6+
require 'faker'
7+
end * 1000
8+
9+
puts "took #{ms}ms to load"

tasks/benchmark.rake

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)