Class: Gitlab::Chaos

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/chaos.rb

Overview

Chaos methods for GitLab. See docs.gitlab.com/ee/development/chaos_endpoints.html for more details.

Class Method Summary collapse

Class Method Details

.cpu_spin(duration_s) ⇒ Object

cpu_spin will consume all CPU on a single core for the specified duration


21
22
23
24
25
26
27
# File 'lib/gitlab/chaos.rb', line 21

def self.cpu_spin(duration_s)
  return unless Gitlab::Metrics::System.thread_cpu_time

  expected_end_time = Gitlab::Metrics::System.thread_cpu_time + duration_s

  rand while Gitlab::Metrics::System.thread_cpu_time < expected_end_time
end

.db_spin(duration_s, interval_s) ⇒ Object

db_spin will query the database in a tight loop for the specified duration


30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/chaos.rb', line 30

def self.db_spin(duration_s, interval_s)
  expected_end_time = Time.now + duration_s

  while Time.now < expected_end_time
    ApplicationRecord.connection.execute("SELECT 1")

    end_interval_time = Time.now + [duration_s, interval_s].min
    rand while Time.now < end_interval_time
  end
end

.kill(signal) ⇒ Object

Kill will send the given signal to the current process.


47
48
49
# File 'lib/gitlab/chaos.rb', line 47

def self.kill(signal)
  Process.kill(signal, Process.pid)
end

.leak_mem(memory_mb, duration_s) ⇒ Object

leak_mem will retain the specified amount of memory and sleep. On return, the memory will be released.


9
10
11
12
13
14
15
16
17
18
# File 'lib/gitlab/chaos.rb', line 9

def self.leak_mem(memory_mb, duration_s)
  start_time = Time.now

  retainer = []
  # Add `n` 1mb chunks of memory to the retainer array
  memory_mb.times { retainer << "x" * 1.megabyte }

  duration_left = [start_time + duration_s - Time.now, 0].max
  Kernel.sleep(duration_left)
end

.run_gcObject


51
52
53
54
55
56
57
# File 'lib/gitlab/chaos.rb', line 51

def self.run_gc
  # Tenure any live objects from young-gen to old-gen
  4.times { GC.start(full_mark: false) }
  # Run a full mark-and-sweep collection
  GC.start
  GC.stat
end

.sleep(duration_s) ⇒ Object

sleep will sleep for the specified duration


42
43
44
# File 'lib/gitlab/chaos.rb', line 42

def self.sleep(duration_s)
  Kernel.sleep(duration_s)
end