diff --git a/src/IllimaniProfiler-Tests/IllAbstractProfilerTest.class.st b/src/IllimaniProfiler-Tests/IllAbstractProfilerTest.class.st index 12c256e..7db7cd4 100644 --- a/src/IllimaniProfiler-Tests/IllAbstractProfilerTest.class.st +++ b/src/IllimaniProfiler-Tests/IllAbstractProfilerTest.class.st @@ -55,13 +55,10 @@ 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. @@ -69,7 +66,25 @@ IllAbstractProfilerTest >> testSamplingRate [ "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' } @@ -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. diff --git a/src/IllimaniProfiler/IllAbstractProfiler.class.st b/src/IllimaniProfiler/IllAbstractProfiler.class.st index a1be1be..0d65cf3 100644 --- a/src/IllimaniProfiler/IllAbstractProfiler.class.st +++ b/src/IllimaniProfiler/IllAbstractProfiler.class.st @@ -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' } @@ -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' }