Class: Perfgate::Execution::SampleContext

Inherits:
Object
  • Object
show all
Defined in:
lib/perfgate/execution/sample_context.rb

Overview

Thread-local scratch space that Perfgate.measure writes into when called from inside a running workload sample, per the explicit measurement contract in spec section 9.1 / 13.1: metrics other than the wall-clock duration fallback are only ever collected during the explicit Perfgate.measure block, never for the workload as a whole (Milestone 2 exit criterion: "metrics are correctly isolated to the measurement block").

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metrics: [:duration]) ⇒ SampleContext

Returns a new instance of SampleContext.



35
36
37
38
39
# File 'lib/perfgate/execution/sample_context.rb', line 35

def initialize(metrics: [:duration])
  @collectors = Instrumentation.collectors_for(metrics)
  @data = {}
  @measured = false
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



57
58
59
# File 'lib/perfgate/execution/sample_context.rb', line 57

def data
  @data
end

Class Method Details

.currentObject



15
16
17
# File 'lib/perfgate/execution/sample_context.rb', line 15

def self.current
  Thread.current[:baseline_sample_context]
end

.current=(context) ⇒ Object



19
20
21
# File 'lib/perfgate/execution/sample_context.rb', line 19

def self.current=(context)
  Thread.current[:baseline_sample_context] = context
end

.with_new(metrics: [:duration]) ⇒ Object

Runs the block with a fresh context active for the given metric names, restoring whatever was active beforehand (nil, normally) once the block finishes.



26
27
28
29
30
31
32
33
# File 'lib/perfgate/execution/sample_context.rb', line 26

def self.with_new(metrics: [:duration])
  previous = current
  context = new(metrics: metrics)
  self.current = context
  yield context
ensure
  self.current = previous
end

Instance Method Details

#measureObject

Runs the block with every configured collector started beforehand and stopped immediately after, merging their results into this sample's data. Safe to call more than once per sample; repeated calls accumulate (numeric values are summed).



45
46
47
48
49
50
51
# File 'lib/perfgate/execution/sample_context.rb', line 45

def measure
  @measured = true
  started = @collectors.map { |collector| [collector, collector.start] }
  result = yield
  started.each { |collector, state| accumulate(collector.finish(state)) }
  result
end

#measured?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/perfgate/execution/sample_context.rb', line 53

def measured?
  @measured
end