2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe 'Request Profiler' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
shared_examples 'profiling a request' do |profile_type, extension|
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
|
|
|
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::MemoryStore.new)
|
|
|
|
allow(RubyProf::Profile).to receive(:profile) do |&blk|
|
|
|
|
blk.call
|
|
|
|
RubyProf::Profile.new
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
allow(MemoryProfiler).to receive(:report) do |&blk|
|
|
|
|
blk.call
|
|
|
|
MemoryProfiler.start
|
|
|
|
MemoryProfiler.stop
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a profile of the request' do
|
|
|
|
project = create(:project, namespace: user.namespace)
|
|
|
|
time = Time.now
|
2017-09-10 17:25:29 +05:30
|
|
|
path = "/#{project.full_path}"
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
travel_to(time) do
|
2019-10-12 21:52:04 +05:30
|
|
|
get path, params: {}, headers: { 'X-Profile-Token' => Gitlab::RequestProfiler.profile_token, 'X-Profile-Mode' => profile_type }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
profile_type = 'execution' if profile_type.nil?
|
|
|
|
profile_path = "#{Gitlab.config.shared.path}/tmp/requests_profiles/#{path.tr('/', '|')}_#{time.to_i}_#{profile_type}.#{extension}"
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(File.exist?(profile_path)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Gitlab::RequestProfiler.remove_all_profiles
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is logged-in" do
|
|
|
|
before do
|
|
|
|
login_as(user)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
include_examples 'profiling a request', 'execution', 'html'
|
|
|
|
include_examples 'profiling a request', nil, 'html'
|
|
|
|
include_examples 'profiling a request', 'memory', 'txt'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not logged-in" do
|
2019-10-12 21:52:04 +05:30
|
|
|
include_examples 'profiling a request', 'execution', 'html'
|
|
|
|
include_examples 'profiling a request', nil, 'html'
|
|
|
|
include_examples 'profiling a request', 'memory', 'txt'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|