Welcome to mirror list, hosted at ThFree Co, Russian Federation.

memory_spec.rb « views « peek « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f88aadfc549990d348cf29f944cffaf87403414 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Peek::Views::Memory, :request_store do
  subject! { described_class.new }

  before do
    stub_memory_instrumentation
  end

  context 'with process_action.action_controller notification' do
    it 'returns empty results when it has not yet fired' do
      expect(subject.results).to eq({})
    end

    it 'returns memory instrumentation data when it has fired' do
      publish_notification

      expect(subject.results[:calls]).to eq('2 MB')
      expect(subject.results[:details]).to all(have_key(:item_header))
      expect(subject.results[:details]).to all(have_key(:item_content))
      expect(subject.results[:summary]).to include('Objects allocated' => '200 k')
      expect(subject.results[:summary]).to include('Allocator calls' => '500')
      expect(subject.results[:summary]).to include('Large allocations' => '1 KB')
    end
  end

  def stub_memory_instrumentation
    start_memory = {
      total_malloc_bytes: 1,
      total_mallocs: 2,
      total_allocated_objects: 3
    }
    allow(Gitlab::Memory::Instrumentation).to receive(:start_thread_memory_allocations).and_return(start_memory)
    allow(Gitlab::Memory::Instrumentation).to receive(:measure_thread_memory_allocations).with(start_memory).and_return({
      mem_total_bytes: 2_097_152,
      mem_bytes: 1024,
      mem_mallocs: 500,
      mem_objects: 200_000
    })
    Gitlab::InstrumentationHelper.init_instrumentation_data
  end

  def publish_notification
    headers = double
    allow(headers).to receive(:env).and_return('action_dispatch.request_id': 'req-42')

    ActiveSupport::Notifications.publish(
      'process_action.action_controller', Time.current - 1.second, Time.current, 'id', headers: headers
    )
  end
end