diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2019-07-18 23:43:49 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2019-07-18 23:43:49 +0300 |
commit | 5db5e50927e9c90c4a353897edb17eeda1f1f10f (patch) | |
tree | 05e39a5c1402ee2f95ea9d8f28b9db4681b2db35 /lib | |
parent | b1d2aed43acc21cfe48f517dd9dbf624549c3db7 (diff) | |
parent | dc14c91d065d869b77b0ec0db47b8b36c96f15be (diff) |
Merge branch 'an-sidekiq-chaos' into 'master'
Adds chaos endpoints to Sidekiq
Closes #64663
See merge request gitlab-org/gitlab-ce!30814
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/chaos.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/gitlab/chaos.rb b/lib/gitlab/chaos.rb new file mode 100644 index 00000000000..4f47cdef971 --- /dev/null +++ b/lib/gitlab/chaos.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module Gitlab + # Chaos methods for GitLab. + # See https://docs.gitlab.com/ee/development/chaos_endpoints.html for more details. + class Chaos + # leak_mem will retain the specified amount of memory and sleep. + # On return, the memory will be released. + 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 + + # cpu_spin will consume all CPU on a single core for the specified duration + def self.cpu_spin(duration_s) + expected_end_time = Time.now + duration_s + + rand while Time.now < expected_end_time + end + + # db_spin will query the database in a tight loop for the specified duration + def self.db_spin(duration_s, interval_s) + expected_end_time = Time.now + duration_s + + while Time.now < expected_end_time + ActiveRecord::Base.connection.execute("SELECT 1") + + end_interval_time = Time.now + [duration_s, interval_s].min + rand while Time.now < end_interval_time + end + end + + # sleep will sleep for the specified duration + def self.sleep(duration_s) + Kernel.sleep(duration_s) + end + + # Kill will send a SIGKILL signal to the current process + def self.kill + Process.kill("KILL", Process.pid) + end + end +end |