summaryrefslogtreecommitdiff
path: root/bin/profile-url
blob: 6047cb70b8de43d9bc1cea7efe0ccfded21596d5 (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
50
51
52
53
#!/usr/bin/env ruby
require 'optparse'

options = {}

opt_parser = OptionParser.new do |opt|
  opt.banner = <<DOCSTRING
Profile a URL on this GitLab instance.

Usage:
  #{__FILE__} url --output=<profile-dump> --sql=<sql-log> [--user=<user>] [--post=<post-data>]

Example:
  #{__FILE__} /dashboard/issues --output=dashboard-profile.dump --sql=dashboard.log --user=root
DOCSTRING
  opt.separator ''
  opt.separator 'Options:'

  opt.on('-o', '--output=/tmp/profile.dump', 'profile output filename') do |output|
    options[:profile_output] = output
  end

  opt.on('-s', '--sql=/tmp/profile_sql.txt', 'SQL output filename') do |sql|
    options[:sql_output] = sql
  end

  opt.on('-u', '--user=root', 'User to authenticate as') do |username|
    options[:username] = username
  end

  opt.on('-p', "--post='user=john&pass=test'", 'Send HTTP POST data') do |post_data|
    options[:post_data] = post_data
  end
end

opt_parser.parse!
options[:url] = ARGV[0]

if options[:url].nil? ||
   options[:profile_output].nil? ||
   options[:sql_output].nil?
  puts opt_parser
  exit
end

require File.expand_path('../config/environment', File.dirname(__FILE__))

Gitlab::Profiler.profile(options[:url],
                         logger: Logger.new(options[:sql_output]),
                         post_data: options[:post_data],
                         user: UserFinder.new(options[:username]).find_by_username,
                         private_token: ENV['PRIVATE_TOKEN'],
                         profiler_options: { out: options[:profile_output] })