Skip to content

Commit

Permalink
Added tests for the finalization exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanmontt committed Feb 24, 2024
1 parent b9b1ec0 commit 1a4a395
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/IllimaniProfiler-Tests/IllAbstractExporter.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Extension { #name : 'IllAbstractExporter' }

{ #category : '*IllimaniProfiler-Tests' }
IllAbstractExporter >> baseFileName [

^ baseFileName
]
68 changes: 68 additions & 0 deletions src/IllimaniProfiler-Tests/IllFinalizationExporterTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"
An IllFinalizationExporterTest is a test class for testing the behavior of IllFinalizationExporter
"
Class {
#name : 'IllFinalizationExporterTest',
#superclass : 'TestCase',
#instVars : [
'exporter',
'header',
'mockProfiler',
'mockGCMonitor'
],
#category : 'IllimaniProfiler-Tests',
#package : 'IllimaniProfiler-Tests'
}

{ #category : 'running' }
IllFinalizationExporterTest >> fakeAllocations [

| allocatedClasses |
allocatedClasses := 'Kernel' asPackage definedClasses asArray first: 10.
^ (1 to: 1000) collect: [ :i | IllEphemeron new
allocatedObjectClass: allocatedClasses atRandom;
sizeInBytes: 100;
finalizationTime: Time primUTCMicrosecondsClock;
updateStatistics;
yourself ]
]

{ #category : 'running' }
IllFinalizationExporterTest >> setUp [

super setUp.
mockGCMonitor := IllMockGCMonitor new.
mockProfiler := IllMockProfiler new
objectAllocations: self fakeAllocations;
gcMonitor: mockGCMonitor;
yourself.
exporter := IllFinalizationExporter new
profiler: mockProfiler;
yourself
]

{ #category : 'tests' }
IllFinalizationExporterTest >> testExportData [

| readStream csvReader actualHeader line createdFile |
header := #( 'finalizationTimeInMicroSeconds' 'sizeInBytes' 'initializationTimeInMicroSeconds'
'allocatedObjectClass' 'survivedScavenges' 'survivedFullGCs' 'forcedFinalization' ).
exporter exportData.

createdFile := (exporter baseFileName , '.csv') asFileReference.
self assert: createdFile exists.

readStream := createdFile readStream.
csvReader := NeoCSVReader on: readStream.

actualHeader := csvReader next.
line := csvReader next.

self assert: header equals: actualHeader.
self assert: line first asInteger > 100000000.
self assert: line second asInteger equals: 100 "size".
self assert: (line fourth asClassInEnvironment:Smalltalk globals) isClass.
self assert: line last equals: 'false'.

createdFile delete
]
13 changes: 13 additions & 0 deletions src/IllimaniProfiler-Tests/IllMockGCMonitor.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Class {
#name : 'IllMockGCMonitor',
#superclass : 'Object',
#instVars : [
'allocations'
],
#category : 'IllimaniProfiler-Tests',
#package : 'IllimaniProfiler-Tests'
}

{ #category : 'exporting' }
IllMockGCMonitor >> exportData: baseFileName [
]
64 changes: 64 additions & 0 deletions src/IllimaniProfiler-Tests/IllMockProfiler.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Class {
#name : 'IllMockProfiler',
#superclass : 'Object',
#instVars : [
'allocations',
'gcMonitor'
],
#category : 'IllimaniProfiler-Tests',
#package : 'IllimaniProfiler-Tests'
}

{ #category : 'accessing' }
IllMockProfiler >> gcActivityMonitor [

^ gcMonitor
]

{ #category : 'as yet unclassified' }
IllMockProfiler >> gcMonitor: anIllMockGCMonitor [

gcMonitor := anIllMockGCMonitor
]

{ #category : 'accessing' }
IllMockProfiler >> objectAllocations [

^ allocations
]

{ #category : 'accessing' }
IllMockProfiler >> objectAllocations: aCollection [

allocations := aCollection
]

{ #category : 'accessing' }
IllMockProfiler >> profiledCode [

^ 'TestProfiler testTheProfiler'
]

{ #category : 'accessing' }
IllMockProfiler >> samplingRate [

^ 100
]

{ #category : 'accessing' }
IllMockProfiler >> totalFullGCs [

^ 2
]

{ #category : 'accessing' }
IllMockProfiler >> totalScavenges [

^ 156
]

{ #category : 'accessing' }
IllMockProfiler >> totalTime [

^ 2000000 "2 microseconds"
]
14 changes: 10 additions & 4 deletions src/IllimaniProfiler/IllEphemeron.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ IllEphemeron >> mourn [
key := nil.

finalizationTime := Time primUTCMicrosecondsClock.
"When the vm calls the method mourn, it is right before of the collection of the object.
So we need to add 1 to the counter of scavenges."
survivedScavenges := Smalltalk vm incrementalGCCount - survivedScavenges + 1.
survivedFullGC := Smalltalk vm fullGCCount - survivedFullGC
self updateStatistics
]

{ #category : 'printing' }
Expand Down Expand Up @@ -175,3 +172,12 @@ IllEphemeron >> survivedScavenges [

^ survivedScavenges
]

{ #category : 'finalization' }
IllEphemeron >> updateStatistics [
"When the vm calls the method mourn, it is right before of the collection of the object.
So we need to add 1 to the counter of scavenges."

survivedScavenges := Smalltalk vm incrementalGCCount - survivedScavenges + 1.
survivedFullGC := Smalltalk vm fullGCCount - survivedFullGC
]
2 changes: 1 addition & 1 deletion src/IllimaniProfiler/IllFinalizationExporter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ IllFinalizationExporter class >> on: aProfiler [
IllFinalizationExporter >> headerOfAllocationModel [

^ #( #finalizationTimeInMicroSeconds #sizeInBytes #initializationTimeInMicroSeconds #allocatedObjectClass
#survivedScavenges #survivedFullGC #forcedFinalization )
#survivedScavenges #survivedFullGCs #forcedFinalization )
]

0 comments on commit 1a4a395

Please sign in to comment.