File: merge-auto-explain-logs

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (49 lines) | stat: -rwxr-xr-x 1,396 bytes parent folder | download
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' # -- 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