Sha256: e2783bde5031b084f8079fee345e8300d7c0cb69d35f597408a42f49c1d61328

Contents?: true

Size: 1.91 KB

Versions: 13

Compression:

Stored size: 1.91 KB

Contents

require 'spec_helper'
require_relative '../../support/mocks/fake_gc_profiler'

describe Appsignal::GarbageCollectionProfiler do
  let(:internal_profiler) { FakeGCProfiler.new }

  before do
    allow_any_instance_of(Appsignal::GarbageCollectionProfiler)
      .to receive(:internal_profiler)
      .and_return(internal_profiler)
  end

  it "should have a total time of 0" do
    expect(Appsignal::GarbageCollectionProfiler.new.total_time).to eq(0)
  end

  describe "after a GC run" do
    before do
      internal_profiler.total_time = 0.12345
      @profiler = Appsignal::GarbageCollectionProfiler.new
    end

    it "should take the total time from Ruby's GC::Profiler" do
      expect(@profiler.total_time).to eq(123)
    end

    it "should clear Ruby's GC::Profiler" do
      expect(internal_profiler).to receive(:clear)
      @profiler.total_time
    end
  end

  describe "when the total GC time becomes too high" do
    it "should reset" do
      profiler = Appsignal::GarbageCollectionProfiler.new
        internal_profiler.total_time = 2_147_483_647
        expect(profiler.total_time).to eq(0)
    end
  end

  describe "after multiple GC runs" do
    it "should add all times from Ruby's GC::Profiler together" do
      profiler = Appsignal::GarbageCollectionProfiler.new

      2.times do
        internal_profiler.total_time = 0.12345
        profiler.total_time
      end

      expect(profiler.total_time).to eq(246)
    end
  end

  describe "in multiple threads, with a slow GC::Profiler" do
    it "should not count garbage collection times twice" do
      threads, results = [], []
      internal_profiler.clear_delay = 0.001
      internal_profiler.total_time = 0.12345

      2.times do
        threads << Thread.new do
          profiler = Appsignal::GarbageCollectionProfiler.new
          results << profiler.total_time
        end
      end

      threads.each(&:join)
      expect(results).to eq([123, 0])
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
appsignal-2.0.6 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.5 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.5.beta.1 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.1.0.alpha.3 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.1.0.alpha.2 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.1.0.alpha.1 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.4 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.3 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.2 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.1 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.0 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-2.0.0.beta.1 spec/lib/appsignal/garbage_collection_profiler_spec.rb
appsignal-1.4.0.beta.1 spec/lib/appsignal/garbage_collection_profiler_spec.rb