2021-01-20 19:11:07 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
return unless ENV.key?('BENCHMARK')
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
require 'erb'
|
|
|
|
require 'benchmark/ips'
|
|
|
|
|
|
|
|
# This benchmarks some of the Banzai pipelines and filters.
|
|
|
|
# They are not definitive, but can be used by a developer to
|
|
|
|
# get a rough idea how the changing or addition of a new filter
|
|
|
|
# will effect performance.
|
|
|
|
#
|
|
|
|
# Run by:
|
|
|
|
# BENCHMARK=1 rspec spec/benchmarks/banzai_benchmark.rb
|
|
|
|
# or
|
|
|
|
# rake benchmark:banzai
|
|
|
|
#
|
|
|
|
# rubocop: disable RSpec/TopLevelDescribePath
|
|
|
|
RSpec.describe 'GitLab Markdown Benchmark', :aggregate_failures do
|
|
|
|
include MarkupHelper
|
|
|
|
|
|
|
|
let_it_be(:feature) { MarkdownFeature.new }
|
|
|
|
let_it_be(:project) { feature.project }
|
|
|
|
let_it_be(:group) { feature.group }
|
|
|
|
let_it_be(:wiki) { feature.wiki }
|
|
|
|
let_it_be(:wiki_page) { feature.wiki_page }
|
|
|
|
let_it_be(:markdown_text) { feature.raw_markdown }
|
|
|
|
let_it_be(:grafana_integration) { create(:grafana_integration, project: project) }
|
|
|
|
let_it_be(:default_context) do
|
|
|
|
{
|
|
|
|
project: project,
|
|
|
|
current_user: current_user,
|
|
|
|
suggestions_filter_enabled: true
|
|
|
|
}
|
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
let(:context) do
|
|
|
|
Banzai::Filter::AssetProxyFilter.transform_context(default_context)
|
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
let!(:render_context) { Banzai::RenderContext.new(project, current_user) }
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
before do
|
|
|
|
stub_application_setting(asset_proxy_enabled: true)
|
|
|
|
stub_application_setting(asset_proxy_secret_key: 'shared-secret')
|
|
|
|
stub_application_setting(asset_proxy_url: 'https://assets.example.com')
|
|
|
|
stub_application_setting(asset_proxy_whitelist: %w(gitlab.com *.mydomain.com))
|
|
|
|
stub_application_setting(plantuml_enabled: true, plantuml_url: 'http://localhost:8080')
|
|
|
|
stub_application_setting(kroki_enabled: true, kroki_url: 'http://localhost:8000')
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
Banzai::Filter::AssetProxyFilter.initialize_settings
|
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
context 'pipelines' do
|
|
|
|
it 'benchmarks several pipelines' do
|
2021-04-07 14:09:45 -04:00
|
|
|
name = 'example.jpg'
|
|
|
|
path = "images/#{name}"
|
|
|
|
blob = double(name: name, path: path, mime_type: 'image/jpeg', data: nil)
|
|
|
|
allow(wiki).to receive(:find_file).with(path, load_content: false).and_return(Gitlab::Git::WikiFile.new(blob))
|
2021-02-12 19:09:10 -05:00
|
|
|
allow(wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
puts "\n--> Benchmarking Full, Wiki, and Plain pipelines\n"
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
Benchmark.ips do |x|
|
|
|
|
x.config(time: 10, warmup: 2)
|
|
|
|
|
|
|
|
x.report('Full pipeline') { Banzai::Pipeline::FullPipeline.call(markdown_text, context) }
|
|
|
|
x.report('Wiki pipeline') { Banzai::Pipeline::WikiPipeline.call(markdown_text, context.merge(wiki: wiki, page_slug: wiki_page.slug)) }
|
|
|
|
x.report('Plain pipeline') { Banzai::Pipeline::PlainMarkdownPipeline.call(markdown_text, context) }
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
x.compare!
|
2021-01-20 19:11:07 -05:00
|
|
|
end
|
|
|
|
end
|
2021-02-12 19:09:10 -05:00
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
context 'filters' do
|
|
|
|
it 'benchmarks all filters in the FullPipeline' do
|
|
|
|
benchmark_pipeline_filters(:full)
|
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
it 'benchmarks all filters in the PlainMarkdownPipeline' do
|
|
|
|
benchmark_pipeline_filters(:plain_markdown)
|
|
|
|
end
|
2022-08-26 02:11:44 -04:00
|
|
|
|
|
|
|
it 'benchmarks specified filters in the FullPipeline' do
|
|
|
|
filter_klass_list = [Banzai::Filter::MathFilter]
|
|
|
|
benchmark_pipeline_filters(:full, filter_klass_list)
|
|
|
|
end
|
2021-02-12 19:09:10 -05:00
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
# build up the source text for each filter
|
|
|
|
def build_filter_text(pipeline, initial_text)
|
|
|
|
filter_source = {}
|
|
|
|
input_text = initial_text
|
2021-05-06 14:10:06 -04:00
|
|
|
result = nil
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
pipeline.filters.each do |filter_klass|
|
2021-05-06 14:10:06 -04:00
|
|
|
# store inputs for current filter_klass
|
|
|
|
filter_source[filter_klass] = { input_text: input_text, input_result: result }
|
2021-02-12 19:09:10 -05:00
|
|
|
|
2021-05-06 14:10:06 -04:00
|
|
|
filter = filter_klass.new(input_text, context, result)
|
|
|
|
output = filter.call
|
|
|
|
|
|
|
|
# save these for the next filter_klass
|
2021-02-12 19:09:10 -05:00
|
|
|
input_text = output
|
2021-05-06 14:10:06 -04:00
|
|
|
result = filter.result
|
2021-01-20 19:11:07 -05:00
|
|
|
end
|
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
filter_source
|
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2022-08-26 02:11:44 -04:00
|
|
|
def benchmark_pipeline_filters(pipeline_type, filter_klass_list = nil)
|
2021-02-12 19:09:10 -05:00
|
|
|
pipeline = Banzai::Pipeline[pipeline_type]
|
|
|
|
filter_source = build_filter_text(pipeline, markdown_text)
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
puts "\n--> Benchmarking #{pipeline.name.demodulize} filters\n"
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
Benchmark.ips do |x|
|
|
|
|
x.config(time: 10, warmup: 2)
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2022-08-26 02:11:44 -04:00
|
|
|
filters = filter_klass_list || pipeline.filters
|
|
|
|
filters.each do |filter_klass|
|
2021-02-12 19:09:10 -05:00
|
|
|
label = filter_klass.name.demodulize.delete_suffix('Filter').truncate(20)
|
2021-01-20 19:11:07 -05:00
|
|
|
|
2021-05-06 14:10:06 -04:00
|
|
|
x.report(label) do
|
|
|
|
filter = filter_klass.new(filter_source[filter_klass][:input_text],
|
|
|
|
context,
|
|
|
|
filter_source[filter_klass][:input_result])
|
|
|
|
filter.call
|
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
end
|
|
|
|
|
2021-02-12 19:09:10 -05:00
|
|
|
x.compare!
|
2021-01-20 19:11:07 -05:00
|
|
|
end
|
|
|
|
end
|
2021-02-12 19:09:10 -05:00
|
|
|
|
|
|
|
# Fake a `current_user` helper
|
|
|
|
def current_user
|
|
|
|
feature.user
|
|
|
|
end
|
2021-01-20 19:11:07 -05:00
|
|
|
end
|