blob: 9379189a9d3d8a89ef12b845e868febcd64efba0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'set' # rubocop:disable Lint/RedundantRequireStatement -- Ruby 3.1 and earlier needs this. Drop this line after Ruby 3.2+ is only supported.
require 'zlib'
# Load query analyzers
require_relative File.expand_path('database/query_analyzers.rb', __dir__)
source = ENV['CI_MERGE_REQUEST_SOURCE_BRANCH_SHA']
target = ENV['CI_MERGE_REQUEST_TARGET_BRANCH_SHA']
log_path = ENV['RSPEC_AUTO_EXPLAIN_LOG_PATH']
logs_path = File.dirname(log_path)
exit(0) unless Dir.exist?(logs_path)
fingerprints = Set.new
jobs = Set.new
query_analyzers = Database::QueryAnalyzers.new
JOB_NAME = %r{^(.*)\.\d+\.[^.]+\.ndjson\.gz$}
Zlib::GzipWriter.open(log_path) do |log|
Dir[File.join(logs_path, '*.gz')].reject { |p| p == log_path }.each do |file|
job_name = File.basename(file)[JOB_NAME, 1]
Zlib::GzipReader.open(file) do |gz|
gz.each_line do |line|
query = JSON.parse(line)
fingerprint = query['fingerprint']
next unless fingerprints.add?(fingerprint)
query_analyzers.analyze(query)
jobs << job_name
query['job_name'] = job_name
log.puts(JSON.generate(query))
end
end
File.delete(file)
end
end
query_analyzers.save!
warn("auto_explain log contains #{fingerprints.size} entries from: #{jobs.to_a.sort.join(', ')}")
warn("auto_explain comparison of #{target} to #{source}") if source && target
|