@@ -35,69 +35,69 @@ public function analyze(array $runs): array
35
35
];
36
36
}
37
37
38
- private function filterOutliers (array $ runs ): array
38
+ private function filterOutliers (array $ runs ): array
39
39
{
40
40
$ times = array_column ($ runs , 'duration_ms ' );
41
-
41
+
42
42
if (count ($ times ) < 50 ) {
43
43
return $ runs ;
44
44
}
45
-
45
+
46
46
$ mean = array_sum ($ times ) / count ($ times );
47
47
$ variance = array_sum (array_map (fn ($ x ) => pow ($ x - $ mean , 2 ), $ times )) / count ($ times );
48
48
$ stdDev = sqrt ($ variance );
49
-
49
+
50
50
$ threshold = $ this ->config ->getOutlierThreshold ();
51
51
$ filteredRuns = [];
52
52
$ outliersRemoved = 0 ;
53
-
53
+
54
54
foreach ($ runs as $ run ) {
55
55
$ zScore = abs (($ run ['duration_ms ' ] - $ mean ) / $ stdDev );
56
-
56
+
57
57
if ($ zScore <= $ threshold ) {
58
58
$ filteredRuns [] = $ run ;
59
59
} else {
60
60
$ outliersRemoved ++;
61
61
}
62
62
}
63
-
63
+
64
64
if (count ($ filteredRuns ) < $ this ->config ->getMinRunsAfterFilter ()) {
65
65
return $ this ->filterOutliersWithThreshold ($ runs , $ threshold * 1.5 );
66
66
}
67
-
67
+
68
68
if (!empty ($ filteredRuns )) {
69
69
$ filteredRuns [0 ]['outliers_removed ' ] = $ outliersRemoved ;
70
70
$ filteredRuns [0 ]['original_run_count ' ] = count ($ runs );
71
71
}
72
-
72
+
73
73
return $ filteredRuns ;
74
74
}
75
-
75
+
76
76
private function filterOutliersWithThreshold (array $ runs , float $ threshold ): array
77
77
{
78
78
$ times = array_column ($ runs , 'duration_ms ' );
79
79
$ mean = array_sum ($ times ) / count ($ times );
80
80
$ variance = array_sum (array_map (fn ($ x ) => pow ($ x - $ mean , 2 ), $ times )) / count ($ times );
81
81
$ stdDev = sqrt ($ variance );
82
-
82
+
83
83
$ filteredRuns = [];
84
84
$ outliersRemoved = 0 ;
85
-
85
+
86
86
foreach ($ runs as $ run ) {
87
87
$ zScore = abs (($ run ['duration_ms ' ] - $ mean ) / $ stdDev );
88
-
88
+
89
89
if ($ zScore <= $ threshold ) {
90
90
$ filteredRuns [] = $ run ;
91
91
} else {
92
92
$ outliersRemoved ++;
93
93
}
94
94
}
95
-
95
+
96
96
if (!empty ($ filteredRuns )) {
97
97
$ filteredRuns [0 ]['outliers_removed ' ] = $ outliersRemoved ;
98
98
$ filteredRuns [0 ]['original_run_count ' ] = count ($ runs );
99
99
}
100
-
100
+
101
101
return $ filteredRuns ;
102
102
}
103
103
@@ -120,23 +120,27 @@ private function analyzeTiming(array $runs, array $times): array
120
120
121
121
private function analyzeMemoryUsage (array $ runs ): array
122
122
{
123
- $ memoryNets = array_column ($ runs , 'memory_net ' );
123
+ $ memoryNets = array_column ($ runs , 'memory_net ' );
124
+ $ memoryDeltas = array_column ($ runs , 'memory_delta ' );
125
+ $ memoryRetained = array_column ($ runs , 'memory_retained ' );
126
+
124
127
$ initializationCost = $ runs [0 ]['memory_net ' ] ?? 0 ;
125
128
126
- // Calculate growth after initialization (skip first run)
127
129
$ subsequentRuns = array_slice ($ memoryNets , 1 );
128
130
$ avgGrowthAfterInit = empty ($ subsequentRuns ) ? 0 : array_sum ($ subsequentRuns ) / count ($ subsequentRuns );
129
131
130
- $ hasLeak = $ avgGrowthAfterInit > 10240 ; // More than 10KB average growth
132
+ $ hasLeak = $ avgGrowthAfterInit > 10240 ;
131
133
132
134
return [
133
135
'initialization_cost ' => $ initializationCost ,
134
- 'avg_memory_delta ' => array_sum (array_column ($ runs , 'memory_delta ' )) / count ($ runs ),
135
- 'avg_memory_net ' => array_sum ($ memoryNets ) / count ($ memoryNets ),
136
+ 'avg_memory_delta ' => array_sum ($ memoryDeltas ) / count ($ memoryDeltas ),
137
+ 'avg_memory_net ' => array_sum ($ memoryNets ) / count ($ memoryNets ),
138
+ 'avg_memory_retained ' => array_sum ($ memoryRetained ) / count ($ memoryRetained ),
136
139
'avg_growth_after_init ' => $ avgGrowthAfterInit ,
137
140
'has_leak ' => $ hasLeak ,
138
141
'leak_severity ' => $ this ->getLeakSeverity ($ avgGrowthAfterInit ),
139
- 'peak_memory ' => max (array_column ($ runs , 'memory_after ' ))
142
+ 'peak_memory ' => max (array_column ($ runs , 'memory_after ' )),
143
+ 'peak_memory_after_gc ' => max (array_column ($ runs , 'memory_after_gc ' ))
140
144
];
141
145
}
142
146
0 commit comments