Skip to content

Commit 4542929

Browse files
authored
CI: Add benchmark scripts (#4989)
**Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: This PR adds benchmark that measures performance using the `in_tail` plugin. It would be good if performance regression can be found in the future... We can check the results on the summary page in Benchmark workflow. https://github.com/fluent/fluentd/actions/runs/15407283089?pr=4989 **Docs Changes**: Not needed. **Release Note**: Same as the title. --------- Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
1 parent 19fb599 commit 4542929

File tree

8 files changed

+127
-1
lines changed

8 files changed

+127
-1
lines changed

.github/workflows/benchmark.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Benchmark
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
workflow_dispatch:
9+
10+
permissions: read-all
11+
12+
concurrency:
13+
group: ${{ github.head_ref || github.sha }}-${{ github.workflow }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
test:
18+
runs-on: ${{ matrix.os }}
19+
continue-on-error: false
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
24+
ruby-version: ['3.4']
25+
26+
name: Ruby ${{ matrix.ruby-version }} on ${{ matrix.os }}
27+
steps:
28+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
29+
- name: Set up Ruby
30+
uses: ruby/setup-ruby@13e7a03dc3ac6c3798f4570bfead2aed4d96abfb # v1.244.0
31+
with:
32+
ruby-version: ${{ matrix.ruby-version }}
33+
- name: Install dependencies
34+
run: bundle install
35+
- name: Run Benchmark
36+
shell: bash # Ensure to use bash shell on all platforms
37+
run: |
38+
bundle exec rake benchmark:run:in_tail | tee -a $GITHUB_STEP_SUMMARY

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fluent-cat
1616
fluent-gem
1717
fluentd
1818
pkg/*
19+
tmp/*
1920
test/tmp/*
2021
test/config/tmp/*
2122
make_dist.sh

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ source 'https://rubygems.org/'
22

33
gemspec
44

5+
gem 'benchmark'
6+
57
local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local")
68
if File.exist?(local_gemfile)
79
puts "Loading Gemfile.local ..." if $DEBUG # `ruby -d` or `bundle -v`

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ require 'fileutils'
55
require 'rake/testtask'
66
require 'rake/clean'
77

8+
require_relative 'tasks/benchmark'
9+
810
task test: [:base_test]
911

1012
# 1. update ChangeLog and lib/fluent/version.rb

fluentd.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
1313
gem.files = Dir.chdir(__dir__) do
1414
`git ls-files -z`.split("\x0").reject do |f|
1515
(File.expand_path(f) == __FILE__) ||
16-
f.start_with?(*%w[test/ .git Gemfile])
16+
f.start_with?(*%w[tasks/ test/ .git Gemfile])
1717
end
1818
end
1919
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }

tasks/benchmark.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require "json"
2+
require "fileutils"
3+
4+
BENCHMARK_FILE_SIZE = 1 * 1024 * 1024 * 1024
5+
BENCHMARK_FILE_PATH = File.expand_path("./tmp/benchmark/data.log")
6+
7+
namespace :benchmark do
8+
task :init do
9+
# Synchronize stdout because the output order is not as intended on Windows environment
10+
STDOUT.sync = true
11+
end
12+
13+
task :prepare_1GB do
14+
FileUtils.mkdir_p(File.dirname(BENCHMARK_FILE_PATH))
15+
File.open(BENCHMARK_FILE_PATH, "w") do |f|
16+
data = { "message": "a" * 1024 }.to_json
17+
18+
loop do
19+
f.puts data
20+
break if File.size(BENCHMARK_FILE_PATH) > BENCHMARK_FILE_SIZE
21+
end
22+
end
23+
end
24+
25+
task :show_info do
26+
# Output the information with markdown format
27+
puts "### Environment"
28+
puts "```"
29+
system "bundle exec ruby --version"
30+
system "bundle exec ruby bin/fluentd --version"
31+
puts "```\n"
32+
end
33+
34+
desc "Run in_tail benchmark"
35+
task :"run:in_tail" => [:init, :prepare_1GB, :show_info] do
36+
# Output the results with markdown format
37+
puts "### in_tail with 1 GB file"
38+
puts "```"
39+
system "bundle exec ruby bin/fluentd -r ./tasks/benchmark/patch_in_tail.rb --no-supervisor -c ./tasks/benchmark/conf/in_tail.conf -o ./tmp/benchmark/fluent.log"
40+
puts "```"
41+
42+
Rake::Task["benchmark:clean"].invoke
43+
end
44+
45+
task :clean do
46+
FileUtils.rm_rf(File.dirname(BENCHMARK_FILE_PATH))
47+
end
48+
end

tasks/benchmark/conf/in_tail.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<source>
2+
@type tail
3+
tag benchmark
4+
path "#{File.expand_path './tmp/benchmark/data.log'}"
5+
read_from_head true
6+
<parse>
7+
@type json
8+
</parse>
9+
</source>
10+
11+
<match **>
12+
@type file
13+
path "#{File.expand_path './tmp/benchmark/in_tail'}"
14+
</match>

tasks/benchmark/patch_in_tail.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'benchmark'
2+
require 'fluent/plugin/in_tail'
3+
4+
class Fluent::Plugin::TailInput::TailWatcher::IOHandler
5+
alias_method :original_with_io, :with_io
6+
7+
def with_io(&block)
8+
@benchmark_measured_in_tail ||= false
9+
# Measure the benchmark only once.
10+
return original_with_io(&block) if @benchmark_measured_in_tail
11+
12+
Benchmark.bm do |x|
13+
x.report {
14+
original_with_io(&block)
15+
@benchmark_measured_in_tail = true
16+
}
17+
end
18+
19+
exit 0
20+
end
21+
end

0 commit comments

Comments
 (0)