Skip to content

Commit c730af1

Browse files
author
Karel Wintersky
committed
0.99.1
Добавлена локализация
1 parent 06d74c2 commit c730af1

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

src/Measure.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
final class Measure implements MeasureInterface
66
{
7+
use MeasureLocaleTrait;
8+
79
const SIZE_KB = 1024;
810
const SIZE_MB = self::SIZE_KB * 1024;
911
const SIZE_GB = self::SIZE_MB * 1024;
1012
const SIZE_TB = self::SIZE_GB * 1024;
1113

14+
const TIME_NS_TO_MS = 1e+6;
15+
1216
public static int $PRECISION_MEMORY = 2;
1317
public static int $PRECISION_TIME = 3;
1418

15-
const TIME_NS_TO_MS = 1e+6;
16-
1719
/**
1820
* Clean memory before measurement
1921
* @return void
@@ -42,6 +44,7 @@ public static function getMemoryUsage(): int
4244
return memory_get_usage(true);
4345
}
4446

47+
4548
/**
4649
* Measure function execution
4750
*
@@ -86,16 +89,16 @@ public static function formatResults(array $measurement, string $separator = "\n
8689

8790
$output = [];
8891
if (!empty($measurement['name'])) {
89-
$output[] = "Test: {$measurement['name']}";
92+
$output[] = self::_t('test') . ": {$measurement['name']}";
9093
}
9194

9295
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']));
9497
}
9598

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']);
99102
$output[] = str_repeat("-", 50) . "\n";
100103

101104
return implode("\n", $output);
@@ -110,9 +113,9 @@ public static function formatResults(array $measurement, string $separator = "\n
110113
public static function formatTime($time_ms):string
111114
{
112115
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')
116119
};
117120
}
118121

@@ -125,12 +128,12 @@ public static function formatTime($time_ms):string
125128
*/
126129
public static function formatMemory(int $bytes, ?int $precision = null): string
127130
{
128-
$precision = $precision ?: self::$PRECISION_TIME;
131+
$precision = $precision ?: self::$PRECISION_MEMORY;
129132
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')
134137
};
135138
}
136139

@@ -196,14 +199,14 @@ public static function getSystemInfo(): array
196199
public static function showTimeline(array $measurements): string
197200
{
198201
if (empty($measurements)) {
199-
return "No measurements to display";
202+
return self::_t('no_measurements');
200203
}
201204

202205
// Находим максимальное время для масштабирования
203206
$maxTime = max(array_column($measurements, 'time_ns'));
204207
$maxLength = 50; // Ширина timeline в символах
205208

206-
$output = "Execution Timeline:\n";
209+
$output = self::_t('timeline') . "\n";
207210
$output .= str_repeat("-", $maxLength + 20) . "\n";
208211

209212
foreach ($measurements as $name => $measurement) {

src/MeasureInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
interface MeasureInterface
66
{
7+
public static function setLanguage(string $lang): void;
8+
79
public static function cleanMemory(): void;
810
public static function getMemoryUsage(): int;
911

src/MeasureLocaleTrait.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Arris\Toolkit;
4+
5+
trait MeasureLocaleTrait
6+
{
7+
public static string $LANGUAGE = 'en';
8+
public static array $LOCALE = [
9+
'en' => [
10+
'test' => 'Test',
11+
'result' => 'Result',
12+
'time' => 'Time',
13+
'memory_used' => 'Memory used',
14+
'peak_memory' => 'Peak memory',
15+
'max_time' => 'Max time',
16+
'timeline' => 'Execution Timeline',
17+
'no_measurements' => 'No measurements to display',
18+
'units' => [
19+
'μs' => 'μs',
20+
'ms' => 'ms',
21+
'sec' => 'sec',
22+
'bytes' => 'bytes',
23+
'kb' => 'KB',
24+
'mb' => 'MB',
25+
'gb' => 'GB'
26+
]
27+
],
28+
'ru' => [
29+
'test' => 'Тест',
30+
'result' => 'Результат',
31+
'time' => 'Время',
32+
'memory_used' => 'Использовано памяти',
33+
'peak_memory' => 'Пиковая память',
34+
'max_time' => 'Макс. время',
35+
'timeline' => 'Временная шкала выполнения',
36+
'no_measurements' => 'Нет данных для отображения',
37+
'units' => [
38+
'μs' => 'мкс',
39+
'ms' => 'мс',
40+
'sec' => 'сек',
41+
'bytes' => 'байт',
42+
'kb' => 'Кб',
43+
'mb' => 'Мб',
44+
'gb' => 'Гб'
45+
]
46+
]
47+
];
48+
49+
/**
50+
* Установка языка вывода
51+
*/
52+
public static function setLanguage(string $lang): void
53+
{
54+
self::$LANGUAGE = in_array($lang, ['en', 'ru']) ? $lang : 'en';
55+
}
56+
57+
/**
58+
* Получение локализованной строки
59+
*/
60+
protected static function _t(string $key): string
61+
{
62+
return self::$LOCALE[self::$LANGUAGE][$key] ?? $key;
63+
}
64+
65+
/**
66+
* Получение локализованной единицы измерения
67+
*/
68+
protected static function _u(string $unit): string
69+
{
70+
return self::$LOCALE[self::$LANGUAGE]['units'][$unit] ?? $unit;
71+
}
72+
73+
}

test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ function array_depth_recursion(array $array): int {
135135
return $max_depth;
136136
}
137137

138+
Measure::setLanguage('ru');
138139
// Запуск тестов
139140
$result = Measure::measure("generateTestArray", [ 100000, 50000 ], "Генерация массива");
140141
$array = $result['result'];

0 commit comments

Comments
 (0)