debian-mirror-gitlab/config/initializers/rspec_profiling.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
3.1 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
return unless Rails.env.test?
2017-08-17 22:00:37 +05:30
module RspecProfilingExt
2019-07-07 11:18:12 +05:30
module Collectors
class CSVWithTimestamps < ::RspecProfiling::Collectors::CSV
TIMESTAMP_FIELDS = %w(created_at updated_at).freeze
2023-03-17 16:20:25 +05:30
METADATA_FIELDS = %w(feature_category).freeze
HEADERS = (::RspecProfiling::Collectors::CSV::HEADERS + TIMESTAMP_FIELDS + METADATA_FIELDS).freeze
2019-07-07 11:18:12 +05:30
def insert(attributes)
output << HEADERS.map do |field|
if TIMESTAMP_FIELDS.include?(field)
Time.now
else
attributes.fetch(field.to_sym)
end
end
end
private
def output
@output ||= ::CSV.open(path, "w").tap { |csv| csv << HEADERS }
end
2017-08-17 22:00:37 +05:30
end
end
module Git
def branch
if ENV['CI_COMMIT_REF_NAME']
"#{defined?(Gitlab::License) ? 'ee' : 'ce'}:#{ENV['CI_COMMIT_REF_NAME']}"
else
2019-07-07 11:18:12 +05:30
super&.chomp
2017-08-17 22:00:37 +05:30
end
end
2019-07-07 11:18:12 +05:30
def sha
super&.chomp
end
2017-08-17 22:00:37 +05:30
end
2023-03-17 16:20:25 +05:30
module Example
def feature_category
metadata[:feature_category]
end
end
2017-08-17 22:00:37 +05:30
module Run
def example_finished(*args)
2023-03-17 16:20:25 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
collector.insert({
branch: vcs.branch,
commit_hash: vcs.sha,
date: vcs.time,
file: @current_example.file,
line_number: @current_example.line_number,
description: @current_example.description,
status: @current_example.status,
exception: @current_example.exception,
time: @current_example.time,
query_count: @current_example.query_count,
query_time: @current_example.query_time,
request_count: @current_example.request_count,
request_time: @current_example.request_time,
feature_category: @current_example.feature_category
})
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2021-06-08 01:23:25 +05:30
rescue StandardError => err
2018-03-17 18:26:18 +05:30
return if @already_logged_example_finished_error # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
warn "rspec_profiling couldn't collect an example: #{err}. Further warnings suppressed."
2018-03-17 18:26:18 +05:30
@already_logged_example_finished_error = true # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
end
alias_method :example_passed, :example_finished
alias_method :example_failed, :example_finished
end
end
2019-07-07 11:18:12 +05:30
RspecProfiling.configure do |config|
if ENV.key?('CI') || ENV.key?('RSPEC_PROFILING')
RspecProfiling::VCS::Git.prepend(RspecProfilingExt::Git)
RspecProfiling::Run.prepend(RspecProfilingExt::Run)
2023-03-17 16:20:25 +05:30
RspecProfiling::Example.prepend(RspecProfilingExt::Example)
2019-07-07 11:18:12 +05:30
config.collector = RspecProfilingExt::Collectors::CSVWithTimestamps
2020-03-13 15:44:24 +05:30
config.csv_path = -> do
2021-09-30 23:02:18 +05:30
prefix = "#{ENV['CI_JOB_NAME']}-".gsub(%r{[ /]}, '-') if ENV['CI_JOB_NAME']
2022-04-04 11:22:00 +05:30
"#{ENV['RSPEC_PROFILING_FOLDER_PATH']}/#{prefix}#{Time.now.to_i}-#{SecureRandom.hex(8)}-rspec-data.csv"
2020-03-13 15:44:24 +05:30
end
2017-08-17 22:00:37 +05:30
end
end