Skip to content

Commit

Permalink
Re-done sampling support
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanmontt committed Oct 20, 2023
1 parent f937380 commit 9638297
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
32 changes: 25 additions & 7 deletions src/IllimaniProfiler-Tests/IllAbstractProfilerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,36 @@ IllAbstractProfilerTest >> testProfileOnSimple [
IllAbstractProfilerTest >> testSamplingRate [

| allocatedByteSrings |
"Sampling is not enable for the moment"
self flag: 'Skip'.
self skip.

profiler
samplingRate: 33;
profileOn: [ 90 timesRepeat: [ ByteString new ] ].
profileOn: [ 100 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. So we cannot chack that the
allocations are exacty 1/3 of the total."
self assert: (allocatedByteSrings >= 30) & (allocatedByteSrings < 40)
self assert: (allocatedByteSrings >= 33) & (allocatedByteSrings < 40)
]

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

| allocatedByteSrings |

profiler
samplingRate: 75;
profileOn: [ 100 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. So we cannot chack that the
allocations are exacty 1/3 of the total."
self assert: (allocatedByteSrings >= 75) & (allocatedByteSrings < 80)
]

{ #category : 'tests' }
Expand All @@ -80,8 +95,11 @@ IllAbstractProfilerTest >> testSamplingRateVariable [

"Sample at 20%"
profiler samplingRate: 20.
"Sample at 20% is the same as sample each 5 allocations"
self assert: profiler samplingRate equals: 5.
self assert: profiler samplingRate equals: 20 / 100.

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

"Do not sample. The same as sampling 100%".
profiler samplingRate: 100.
Expand Down
18 changes: 12 additions & 6 deletions src/IllimaniProfiler/IllAbstractProfiler.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,17 @@ IllAbstractProfiler >> profileOn: aBlock [
{ #category : 'profiling' }
IllAbstractProfiler >> registerAllocation: anObject [

"Sampling"
"samplingCounter := samplingCounter + 1.
samplingCounter % samplingRate = 0 ifFalse: [ ^ anObject ]."

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

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

(samplingCounter / 100) / samplingRate <= 1
ifTrue: [ self internalRegisterAllocation: anObject ]
ifFalse: [ ^ self ]
]

{ #category : 'accessing - statistics' }
Expand Down Expand Up @@ -237,7 +243,7 @@ IllAbstractProfiler >> samplingRate [
IllAbstractProfiler >> samplingRate: anInteger [
"The anInteger needs to be an integer number between 1 and 100. "

samplingRate := (100 / anInteger) asInteger
samplingRate := (anInteger / 100)
]

{ #category : 'profiling' }
Expand Down

0 comments on commit 9638297

Please sign in to comment.