From 1a4a3953732946486170e2e44b3a9df42c016c42 Mon Sep 17 00:00:00 2001 From: jordanmontt Date: Sat, 24 Feb 2024 20:16:20 +0100 Subject: [PATCH] Added tests for the finalization exporter --- .../IllAbstractExporter.extension.st | 7 ++ .../IllFinalizationExporterTest.class.st | 68 +++++++++++++++++++ .../IllMockGCMonitor.class.st | 13 ++++ .../IllMockProfiler.class.st | 64 +++++++++++++++++ src/IllimaniProfiler/IllEphemeron.class.st | 14 ++-- .../IllFinalizationExporter.class.st | 2 +- 6 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 src/IllimaniProfiler-Tests/IllAbstractExporter.extension.st create mode 100644 src/IllimaniProfiler-Tests/IllFinalizationExporterTest.class.st create mode 100644 src/IllimaniProfiler-Tests/IllMockGCMonitor.class.st create mode 100644 src/IllimaniProfiler-Tests/IllMockProfiler.class.st diff --git a/src/IllimaniProfiler-Tests/IllAbstractExporter.extension.st b/src/IllimaniProfiler-Tests/IllAbstractExporter.extension.st new file mode 100644 index 0000000..6b71c8c --- /dev/null +++ b/src/IllimaniProfiler-Tests/IllAbstractExporter.extension.st @@ -0,0 +1,7 @@ +Extension { #name : 'IllAbstractExporter' } + +{ #category : '*IllimaniProfiler-Tests' } +IllAbstractExporter >> baseFileName [ + + ^ baseFileName +] diff --git a/src/IllimaniProfiler-Tests/IllFinalizationExporterTest.class.st b/src/IllimaniProfiler-Tests/IllFinalizationExporterTest.class.st new file mode 100644 index 0000000..622ca51 --- /dev/null +++ b/src/IllimaniProfiler-Tests/IllFinalizationExporterTest.class.st @@ -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 +] diff --git a/src/IllimaniProfiler-Tests/IllMockGCMonitor.class.st b/src/IllimaniProfiler-Tests/IllMockGCMonitor.class.st new file mode 100644 index 0000000..90bfe8f --- /dev/null +++ b/src/IllimaniProfiler-Tests/IllMockGCMonitor.class.st @@ -0,0 +1,13 @@ +Class { + #name : 'IllMockGCMonitor', + #superclass : 'Object', + #instVars : [ + 'allocations' + ], + #category : 'IllimaniProfiler-Tests', + #package : 'IllimaniProfiler-Tests' +} + +{ #category : 'exporting' } +IllMockGCMonitor >> exportData: baseFileName [ +] diff --git a/src/IllimaniProfiler-Tests/IllMockProfiler.class.st b/src/IllimaniProfiler-Tests/IllMockProfiler.class.st new file mode 100644 index 0000000..5aa291e --- /dev/null +++ b/src/IllimaniProfiler-Tests/IllMockProfiler.class.st @@ -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" +] diff --git a/src/IllimaniProfiler/IllEphemeron.class.st b/src/IllimaniProfiler/IllEphemeron.class.st index e6e82e4..ca9ae9c 100644 --- a/src/IllimaniProfiler/IllEphemeron.class.st +++ b/src/IllimaniProfiler/IllEphemeron.class.st @@ -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' } @@ -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 +] diff --git a/src/IllimaniProfiler/IllFinalizationExporter.class.st b/src/IllimaniProfiler/IllFinalizationExporter.class.st index 30295cc..eac4765 100644 --- a/src/IllimaniProfiler/IllFinalizationExporter.class.st +++ b/src/IllimaniProfiler/IllFinalizationExporter.class.st @@ -18,5 +18,5 @@ IllFinalizationExporter class >> on: aProfiler [ IllFinalizationExporter >> headerOfAllocationModel [ ^ #( #finalizationTimeInMicroSeconds #sizeInBytes #initializationTimeInMicroSeconds #allocatedObjectClass - #survivedScavenges #survivedFullGC #forcedFinalization ) + #survivedScavenges #survivedFullGCs #forcedFinalization ) ]