Skip to content

Commit

Permalink
Update Environment::VMCache helpers for Ruby 3.2 metrics
Browse files Browse the repository at this point in the history
Issue #2534
  • Loading branch information
ivoanjo committed Feb 6, 2023
1 parent 3834b19 commit db24d70
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
17 changes: 17 additions & 0 deletions lib/datadog/core/environment/vm_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module VMCache
# Whenever a constant creation busts the global constant cache
# this value is incremented. This has a measurable performance impact
# and thus show be avoided after application warm up.
#
# This was removed in Ruby 3.2.
# @see https://github.com/ruby/ruby/blob/master/doc/NEWS/NEWS-3.2.0.md#implementation-improvements
def global_constant_state
::RubyVM.stat[:global_constant_state]
end
Expand All @@ -38,6 +41,20 @@ def global_method_state
::RubyVM.stat[:global_method_state]
end

# Introduced in Ruby 3.2 to match an improved cache implementation.
#
# @see https://bugs.ruby-lang.org/issues/18589
def constant_cache_invalidations
::RubyVM.stat[:constant_cache_invalidations]
end

# Introduced in Ruby 3.2 to match an improved cache implementation.
#
# @see https://bugs.ruby-lang.org/issues/18589
def constant_cache_misses
::RubyVM.stat[:constant_cache_misses]
end

def available?
defined?(::RubyVM) && ::RubyVM.respond_to?(:stat)
end
Expand Down
52 changes: 49 additions & 3 deletions spec/datadog/core/environment/vm_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,67 @@
describe '.global_constant_state' do
subject(:global_constant_state) { described_class.global_constant_state }

it { is_expected.to be_a_kind_of(Integer) }
context 'on Ruby <= 3.1' do
before { skip('Test only runs on Ruby <= 3.1') if RUBY_VERSION >= '3.2.0' }

it { is_expected.to be_a_kind_of(Integer) }
end

context 'on Ruby >= 3.2' do
before { skip('Test only runs on Ruby >= 3.2') if RUBY_VERSION < '3.2.0' }

it { is_expected.to be nil }
end
end

describe '.global_method_state' do
subject(:global_method_state) { described_class.global_method_state }

context 'with Ruby < 3', if: RUBY_VERSION < '3.0.0' do
context 'on Ruby 2.x' do
before { skip('Test only runs on Ruby 2.x') unless RUBY_VERSION.start_with?('2.') }

it { is_expected.to be_a_kind_of(Integer) }
end

context 'with Ruby >= 3', if: RUBY_VERSION >= '3.0.0' do
context 'on Ruby >= 3.x' do
before { skip('Test only runs on Ruby >= 3.x') if RUBY_VERSION.start_with?('2.') }

it 'has moved to a per-class method cache' do
is_expected.to be_nil
end
end
end

describe '.constant_cache_invalidations' do
subject(:constant_cache_invalidations) { described_class.constant_cache_invalidations }

context 'on Ruby <= 3.1' do
before { skip('Test only runs on Ruby <= 3.1') if RUBY_VERSION >= '3.2.0' }

it { is_expected.to be nil }
end

context 'on Ruby >= 3.2' do
before { skip('Test only runs on Ruby >= 3.2') if RUBY_VERSION < '3.2.0' }

it { is_expected.to be_a_kind_of(Integer) }
end
end

describe '.constant_cache_misses' do
subject(:constant_cache_misses) { described_class.constant_cache_misses }

context 'on Ruby <= 3.1' do
before { skip('Test only runs on Ruby <= 3.1') if RUBY_VERSION >= '3.2.0' }

it { is_expected.to be nil }
end

context 'on Ruby >= 3.2' do
before { skip('Test only runs on Ruby >= 3.2') if RUBY_VERSION < '3.2.0' }

it { is_expected.to be_a_kind_of(Integer) }
end
end
end
end

0 comments on commit db24d70

Please sign in to comment.