Skip to content

Commit c48530e

Browse files
Benchmark revamp + run benchmark as part of CI (#3176)
* Organize benchmark gems in a group Inspired by ruby/json#606 * 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> * Strict permissions for gh workflows * Skip eval and avoid generating all generators inside of the benchmark execution Looping over the constants instead of using `eval` is a more secure approach. Plus, build the list of generators outside so that we're only benchmarking generator execution. Co-Authored-By: Thiago Araujo <thd.araujo@gmail.com> * This was a one-time experiment * Don't need to keep this around anymore after comparing YML vs JSON --------- Co-authored-by: Thiago Araujo <thd.araujo@gmail.com>
1 parent 916d8f8 commit c48530e

File tree

9 files changed

+100
-2095
lines changed

9 files changed

+100
-2095
lines changed

.github/workflows/bench.yml

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

.github/workflows/ruby.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ on:
1515

1616
permissions:
1717
contents: read
18+
pull-requests: read
1819

1920
jobs:
2021
lint:
@@ -53,7 +54,9 @@ jobs:
5354
ruby-version: ${{ matrix.ruby }}
5455

5556
- name: Install dependencies
56-
run: bundle install
57+
run: |
58+
bundle config --without benchmark
59+
bundle install
5760
5861
- name: Run tests
5962
run: bundle exec rake test

.rubocop.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ 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+
5358
Style/AsciiComments:
5459
Description: This cop checks for non-ascii (non-English) characters in comments.
5560
Exclude:
@@ -102,11 +107,6 @@ Style/RegexpLiteral:
102107
- mixed
103108
AllowInnerSlashes: false
104109

105-
Security/Eval:
106-
Description: The use of eval represents a serious security risk.
107-
Exclude:
108-
- 'lib/faker/default/json.rb'
109-
110110
Style/IfUnlessModifier:
111111
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.
112112
Enabled: false

Gemfile

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

8-
gem 'benchmark'
98
gem 'irb'
109
gem 'minitest', '5.27.0'
1110
gem 'pry', '0.16.0'
@@ -18,3 +17,8 @@ gem 'simplecov', '0.22.0'
1817
gem 'test-unit', '3.7.7'
1918
gem 'timecop', '0.9.10'
2019
gem 'yard', '0.9.38'
20+
21+
group :benchmark do
22+
gem 'benchmark'
23+
gem 'benchmark-ips'
24+
end

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ GEM
99
specs:
1010
ast (2.4.3)
1111
benchmark (0.5.0)
12+
benchmark-ips (2.14.0)
1213
coderay (1.1.3)
1314
concurrent-ruby (1.3.6)
1415
date (3.5.1)
@@ -96,6 +97,7 @@ PLATFORMS
9697

9798
DEPENDENCIES
9899
benchmark
100+
benchmark-ips
99101
faker!
100102
irb
101103
minitest (= 5.27.0)

benchmark/generators.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require 'benchmark/ips'
4+
require 'faker'
5+
6+
def generators
7+
constants.flat_map do |klass|
8+
subclass_methods(klass)
9+
end
10+
end
11+
12+
def constants
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+
klass = Faker.const_get(subclass)
32+
33+
public_methods = klass.public_methods(false) - Faker::Base.public_methods(false)
34+
35+
public_methods.sort.map do |method|
36+
[klass, method]
37+
end
38+
end
39+
40+
all_generators = generators
41+
42+
Benchmark.ips do |x|
43+
x.report("Number of generators: #{all_generators.count}") do
44+
all_generators.each { |klass, generator| klass.send(generator) }
45+
end
46+
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)