File: memory_spec.rb

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 (53 lines) | stat: -rw-r--r-- 1,782 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
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 MiB')
      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 KiB')
    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