Skip to content

Commit

Permalink
Improving sampling rate support
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanmontt committed Mar 7, 2024
1 parent 21ea91a commit 531b884
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
37 changes: 27 additions & 10 deletions src/IllimaniProfiler-Tests/IllAbstractProfilerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ IllAbstractProfilerTest >> testSamplingRate [
| allocatedByteSrings |

profiler
samplingRate: 33;
samplingRate: 33 / 100;
profileOn: [ 100 timesRepeat: [ ByteString new ] ].

allocatedByteSrings := (profiler objectAllocations select:
Expand All @@ -69,13 +69,30 @@ IllAbstractProfilerTest >> testSamplingRate [
self assert: (allocatedByteSrings >= 33) & (allocatedByteSrings < 40)
]

{ #category : 'tests' }
IllAbstractProfilerTest >> testSamplingRate1in1000 [

| allocatedByteSrings |

profiler
samplingRate: 1/1000;
profileOn: [ 1000 timesRepeat: [ ByteString new ] ].

allocatedByteSrings := (profiler objectAllocations select:
[ :e | e allocatedObjectClass = ByteString ]) size.

"We are cheking in this range becase the profiler makes some allocations that are
necessary for the profiler to work, like Durations objects."
self assert: (allocatedByteSrings >= 1) & (allocatedByteSrings < 4)
]

{ #category : 'tests' }
IllAbstractProfilerTest >> testSamplingRateOtherPercentage [

| allocatedByteSrings |

profiler
samplingRate: 75;
samplingRate: 75 / 100;
profileOn: [ 100 timesRepeat: [ ByteString new ] ].

allocatedByteSrings := (profiler objectAllocations select:
Expand All @@ -91,17 +108,17 @@ IllAbstractProfilerTest >> testSamplingRateOtherPercentage [
IllAbstractProfilerTest >> testSamplingRateVariable [

"By default, do not sample"
self assert: profiler samplingRate equals: 100.
self assert: profiler samplingRate equals: 1.

"Sample at 20%"
profiler samplingRate: 20.
self assert: profiler samplingRate equals: 20.
profiler samplingRate: 20 / 100.
self assert: profiler samplingRate equals: (20 / 100).

"Sample at 75%"
profiler samplingRate: 75.
self assert: profiler samplingRate equals: 75.
profiler samplingRate: 75 / 100.
self assert: profiler samplingRate equals: (75 / 100).

"Do not sample. The same as sampling 100%".
profiler samplingRate: 100.
self assert: profiler samplingRate equals: 100.
"Do not sample. The same as sampling 100%"
profiler samplingRate: 1.
self assert: profiler samplingRate equals: 1.
]
20 changes: 18 additions & 2 deletions src/IllimaniProfiler-Tests/IllAllocationRateProfilerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ IllAllocationRateProfilerTest >> testSamplingRate [
| allocatedByteSrings |

profiler
samplingRate: 33;
samplingRate: 33 / 100;
profileOn: [ 100 timesRepeat: [ ByteString new ] ].

"We don't have the classes on this profiler"
Expand All @@ -30,13 +30,29 @@ IllAllocationRateProfilerTest >> testSamplingRate [
self assert: (allocatedByteSrings >= 33) & (allocatedByteSrings < 40)
]

{ #category : 'tests' }
IllAllocationRateProfilerTest >> testSamplingRate1in1000 [

| allocatedByteSrings |

profiler
samplingRate: 1/1000;
profileOn: [ 1000 timesRepeat: [ ByteString new ] ].

allocatedByteSrings := profiler objectAllocations size.

"We are cheking in this range becase the profiler makes some allocations that are
necessary for the profiler to work, like Durations objects."
self assert: (allocatedByteSrings >= 1) & (allocatedByteSrings < 4)
]

{ #category : 'tests' }
IllAllocationRateProfilerTest >> testSamplingRateOtherPercentage [

| allocatedByteSrings |

profiler
samplingRate: 75;
samplingRate: 75 / 100;
profileOn: [ 100 timesRepeat: [ ByteString new ] ].

"We don't have the class on this profiler"
Expand Down
16 changes: 8 additions & 8 deletions src/IllimaniProfiler/IllAbstractProfiler.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ IllAbstractProfiler >> initialize [
yourself.
self initializeMethodProxies: handler.
samplingCounter := 0.
samplingRate := 100.
samplingRate := 1.
gcActivityMonitor := NullGCActivityMonitor new
]

Expand Down Expand Up @@ -174,15 +174,15 @@ IllAbstractProfiler >> profiledCode [
{ #category : 'profiling' }
IllAbstractProfiler >> registerAllocation: anObject [

samplingRate = 100 ifTrue: [
samplingRate = 1 ifTrue: [
"Sampling 100% capture all"
self internalRegisterAllocation: anObject.
^ self ].

samplingCounter := samplingCounter + 1.
samplingCounter > 100 ifTrue: [ samplingCounter := 1 ].
samplingCounter > samplingRate denominator ifTrue: [ samplingCounter := 1 ].

samplingCounter <= samplingRate
samplingCounter <= samplingRate numerator
ifTrue: [ self internalRegisterAllocation: anObject ]
ifFalse: [ ^ self ]
]
Expand Down Expand Up @@ -269,11 +269,11 @@ IllAbstractProfiler >> samplingRate [
]

{ #category : 'api' }
IllAbstractProfiler >> samplingRate: anInteger [
"The anInteger needs to be an integer number between 1 and 100. This number represents the
sampling rate *percentage*"
IllAbstractProfiler >> samplingRate: aFraction [
"I expect my argument to be a fraction. For example 4 / 5. That will mean that I will
register 4 out of 5 allocations."

samplingRate := anInteger
samplingRate := aFraction
]

{ #category : 'profiling' }
Expand Down

0 comments on commit 531b884

Please sign in to comment.