44
55namespace DragonCode \Benchmark \Services ;
66
7+ use DragonCode \Benchmark \Data \DeviationData ;
78use DragonCode \Benchmark \Data \MetricData ;
89use DragonCode \Benchmark \Data \ResultData ;
910
@@ -30,18 +31,37 @@ public function calculate(array $collection): array
3031
3132 protected function map (array $ collection ): array
3233 {
33- return array_map (fn (array $ item ) => $ this ->make ($ item ), $ collection );
34+ return array_map (fn (array $ item ): ResultData => $ this ->make ($ item ), $ collection );
3435 }
3536
3637 protected function make (array $ item ): ResultData
3738 {
38- dd ($ item );
3939 return new ResultData (
40- min: $ this ->metric ()
40+ min : $ this ->metric ($ item , 'min ' ),
41+ max : $ this ->metric ($ item , 'max ' ),
42+ avg : $ this ->metric ($ item , 'avg ' ),
43+ total : $ this ->metric ($ item , 'total ' ),
44+ deviation: $ this ->deviationMetric ($ item ),
4145 );
4246 }
4347
44- protected function metric (float $ time , float $ memory ): MetricData {}
48+ protected function metric (array $ item , string $ key ): MetricData
49+ {
50+ return $ this ->result ->$ key (
51+ $ this ->result ->values ($ item [$ key ], 0 , false ),
52+ $ this ->result ->values ($ item [$ key ], 1 , false ),
53+ );
54+ }
55+
56+ protected function deviationMetric (array $ item ): DeviationData
57+ {
58+ return new DeviationData (
59+ avg: $ this ->result ->avg (
60+ $ this ->result ->values ($ item ['deviation ' ], 0 , false ),
61+ $ this ->result ->values ($ item ['deviation ' ], 1 , false ),
62+ ),
63+ );
64+ }
4565
4666 protected function flatten (array $ collection ): array
4767 {
@@ -62,8 +82,8 @@ protected function flatten(array $collection): array
6282 }
6383
6484 $ result [$ key ]['deviation ' ][] = [
65- $ this ->deviation ($ default [$ key ][0 ], $ item ->avg ->time ),
66- $ this ->deviation ($ default [$ key ][1 ], $ item ->avg ->memory ),
85+ $ this ->percentage ( $ default [ $ key ][ 0 ], $ this -> deviation ($ default [$ key ][0 ], $ item ->avg ->time ) ),
86+ $ this ->percentage ( $ default [ $ key ][ 1 ], $ this -> deviation ($ default [$ key ][1 ], $ item ->avg ->memory ) ),
6787 ];
6888 }
6989 }
@@ -80,4 +100,13 @@ protected function deviation(float $first, float $second): float
80100
81101 return sqrt (($ deviation1 + $ deviation2 ) / 2 );
82102 }
103+
104+ protected function percentage (float $ reference , float $ value ): float
105+ {
106+ if ($ reference === 0.0 ) {
107+ return 0 ;
108+ }
109+
110+ return ($ value - $ reference ) / $ reference * 100 ;
111+ }
83112}
0 commit comments