4
4
5
5
final class Measure implements MeasureInterface
6
6
{
7
+ use MeasureLocaleTrait;
8
+
7
9
const SIZE_KB = 1024 ;
8
10
const SIZE_MB = self ::SIZE_KB * 1024 ;
9
11
const SIZE_GB = self ::SIZE_MB * 1024 ;
10
12
const SIZE_TB = self ::SIZE_GB * 1024 ;
11
13
14
+ const TIME_NS_TO_MS = 1e+6 ;
15
+
12
16
public static int $ PRECISION_MEMORY = 2 ;
13
17
public static int $ PRECISION_TIME = 3 ;
14
18
15
- const TIME_NS_TO_MS = 1e+6 ;
16
-
17
19
/**
18
20
* Clean memory before measurement
19
21
* @return void
@@ -42,6 +44,7 @@ public static function getMemoryUsage(): int
42
44
return memory_get_usage (true );
43
45
}
44
46
47
+
45
48
/**
46
49
* Measure function execution
47
50
*
@@ -86,16 +89,16 @@ public static function formatResults(array $measurement, string $separator = "\n
86
89
87
90
$ output = [];
88
91
if (!empty ($ measurement ['name ' ])) {
89
- $ output [] = " Test : {$ measurement ['name ' ]}" ;
92
+ $ output [] = self :: _t ( ' test ' ) . " : {$ measurement ['name ' ]}" ;
90
93
}
91
94
92
95
if ($ show_results ) {
93
- $ output [] = " - Result : " . (is_scalar ($ measurement ['result ' ]) ? $ measurement ['result ' ] : gettype ($ measurement ['result ' ]));
96
+ $ output [] = " - " . self :: _t ( ' result ' ) . " : " . (is_scalar ($ measurement ['result ' ]) ? $ measurement ['result ' ] : gettype ($ measurement ['result ' ]));
94
97
}
95
98
96
- $ output [] = " - Time: " . self ::formatTime ($ timeNs / self ::TIME_NS_TO_MS );
97
- $ output [] = " - Memory used : " . self ::formatMemory ($ measurement ['memory_bytes ' ]);
98
- $ output [] = " - Peak memory : " . self ::formatMemory ($ measurement ['peak_memory_bytes ' ]);
99
+ $ output [] = " - " . self :: _t ( ' time ' ) . " : " . self ::formatTime ($ timeNs / self ::TIME_NS_TO_MS );
100
+ $ output [] = " - " . self :: _t ( ' memory_used ' ) . " : " . self ::formatMemory ($ measurement ['memory_bytes ' ]);
101
+ $ output [] = " - " . self :: _t ( ' peak_memory ' ) . " : " . self ::formatMemory ($ measurement ['peak_memory_bytes ' ]);
99
102
$ output [] = str_repeat ("- " , 50 ) . "\n" ;
100
103
101
104
return implode ("\n" , $ output );
@@ -110,9 +113,9 @@ public static function formatResults(array $measurement, string $separator = "\n
110
113
public static function formatTime ($ time_ms ):string
111
114
{
112
115
return match (true ) {
113
- $ time_ms < 1 => round ($ time_ms * 1000 , self ::$ PRECISION_TIME ) . " μs " ,
114
- $ time_ms < 1000 => round ($ time_ms , self ::$ PRECISION_TIME ) . " ms " ,
115
- default => round ($ time_ms / 1000 , self ::$ PRECISION_TIME ) . " sec"
116
+ $ time_ms < 1 => round ($ time_ms * 1000 , self ::$ PRECISION_TIME ) . ' ' . self :: _u ( ' μs ' ) ,
117
+ $ time_ms < 1000 => round ($ time_ms , self ::$ PRECISION_TIME ) . ' ' . self :: _u ( ' ms ' ) ,
118
+ default => round ($ time_ms / 1000 , self ::$ PRECISION_TIME ) . ' ' . self :: _u ( ' sec ' )
116
119
};
117
120
}
118
121
@@ -125,12 +128,12 @@ public static function formatTime($time_ms):string
125
128
*/
126
129
public static function formatMemory (int $ bytes , ?int $ precision = null ): string
127
130
{
128
- $ precision = $ precision ?: self ::$ PRECISION_TIME ;
131
+ $ precision = $ precision ?: self ::$ PRECISION_MEMORY ;
129
132
return match (true ) {
130
- $ bytes >= self ::SIZE_GB => round ($ bytes / self ::SIZE_GB , $ precision ) . ' Gb ' ,
131
- $ bytes >= self ::SIZE_MB => round ($ bytes / self ::SIZE_MB , $ precision ) . ' Mb ' ,
132
- $ bytes >= self ::SIZE_KB => round ($ bytes / self ::SIZE_KB , $ precision ) . ' Kb ' ,
133
- default => $ bytes . ' bytes '
133
+ $ bytes >= self ::SIZE_GB => round ($ bytes / self ::SIZE_GB , $ precision ) . ' ' . self :: _u ( ' gb ' ) ,
134
+ $ bytes >= self ::SIZE_MB => round ($ bytes / self ::SIZE_MB , $ precision ) . ' ' . self :: _u ( ' mb ' ) ,
135
+ $ bytes >= self ::SIZE_KB => round ($ bytes / self ::SIZE_KB , $ precision ) . ' ' . self :: _u ( ' kb ' ) ,
136
+ default => $ bytes . ' ' . self :: _u ( ' bytes ')
134
137
};
135
138
}
136
139
@@ -196,14 +199,14 @@ public static function getSystemInfo(): array
196
199
public static function showTimeline (array $ measurements ): string
197
200
{
198
201
if (empty ($ measurements )) {
199
- return " No measurements to display " ;
202
+ return self :: _t ( ' no_measurements ' ) ;
200
203
}
201
204
202
205
// Находим максимальное время для масштабирования
203
206
$ maxTime = max (array_column ($ measurements , 'time_ns ' ));
204
207
$ maxLength = 50 ; // Ширина timeline в символах
205
208
206
- $ output = " Execution Timeline: \n" ;
209
+ $ output = self :: _t ( ' timeline ' ) . " \n" ;
207
210
$ output .= str_repeat ("- " , $ maxLength + 20 ) . "\n" ;
208
211
209
212
foreach ($ measurements as $ name => $ measurement ) {
0 commit comments