1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelRequestBenchmark\Controllers; |
4
|
|
|
|
5
|
|
|
class BenchmarkResultsController |
6
|
|
|
{ |
7
|
|
|
private $bestRequests = []; |
8
|
|
|
|
9
|
|
|
private $worstRequests = []; |
10
|
|
|
|
11
|
|
|
public function index() |
12
|
|
|
{ |
13
|
|
|
$file = config('request-benchmark.storage_path') . '/request-benchmark.json'; |
14
|
|
|
$requests = []; |
15
|
|
|
$history = []; |
16
|
|
|
|
17
|
|
|
if (file_exists($file)) { |
18
|
|
|
$data = json_decode(file_get_contents($file), true); |
19
|
|
|
|
20
|
|
|
if (isset($data['history'])) { |
21
|
|
|
$history = $data['history']; |
22
|
|
|
|
23
|
|
|
unset($data['history']); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$requests = $data; |
27
|
|
|
|
28
|
|
|
$this->compareRequests($history); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return view('request-benchmark::results', [ |
32
|
|
|
'requests' => $requests, |
33
|
|
|
'history' => $history, |
34
|
|
|
'bestRequests' => $this->bestRequests, |
35
|
|
|
'worstRequests' => $this->worstRequests, |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function compareRequests(array $history): array |
40
|
|
|
{ |
41
|
|
|
$bestRequests = []; |
42
|
|
|
|
43
|
|
|
foreach ($history as $request => $requestHistory) { |
44
|
|
|
if (count($requestHistory) <= 1) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($requestHistory as $timestamp => $result) { |
49
|
|
|
$this->compare($request, $timestamp, $result); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $bestRequests; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function compare(string $request, string $timestamp, array $result) |
58
|
|
|
{ |
59
|
|
|
if ($this->firstInHistory($request)) { |
60
|
|
|
$this->storeBestRequest($request, $timestamp, $result); |
61
|
|
|
$this->storeWorstRequest($request, $timestamp, $result); |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$time = $this->getTime($result['time']); |
67
|
|
|
$memoryUsage = $this->getMemoryUsage($result['actual_memory_usage']); |
68
|
|
|
|
69
|
|
|
if ($this->isBest($request, $time, $memoryUsage)) { |
70
|
|
|
$this->storeBestRequest($request, $timestamp, $result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->isWorst($request, $time, $memoryUsage)) { |
74
|
|
|
$this->storeWorstRequest($request, $timestamp, $result); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function firstInHistory(string $request): bool |
79
|
|
|
{ |
80
|
|
|
return (empty($this->bestRequests) && empty($this->worstRequests)) |
81
|
|
|
|| (! isset($this->bestRequests[$request]) && ! isset($this->worstRequests[$request])); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
private function isBest(string $request, float $time, float $memoryUsage): bool |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$bestTime = $this->getTime($this->bestRequests[$request]->result->time); |
87
|
|
|
$bestMemoryUsage = $this->getMemoryUsage( |
88
|
|
|
$this->bestRequests[$request]->result->actual_memory_usage |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
if ($memoryUsage <= $bestMemoryUsage) { |
92
|
|
|
$difference = $bestMemoryUsage - $memoryUsage; |
93
|
|
|
|
94
|
|
|
return $difference > 1000 || $time < $bestTime; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
private function isWorst(string $request, float $time, float $memoryUsage): bool |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$worstTime = $this->getTime($this->worstRequests[$request]->result->time); |
103
|
|
|
$worstMemoryUsage = $this->getMemoryUsage( |
104
|
|
|
$this->worstRequests[$request]->result->actual_memory_usage |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
if ($memoryUsage >= $worstMemoryUsage) { |
108
|
|
|
$difference = $memoryUsage - $worstMemoryUsage; |
109
|
|
|
|
110
|
|
|
return $difference > 1000 || $worstTime < $time; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function getTime(string $time): float |
117
|
|
|
{ |
118
|
|
|
return (float) filter_var( |
119
|
|
|
$time, |
120
|
|
|
FILTER_SANITIZE_NUMBER_FLOAT, |
121
|
|
|
FILTER_FLAG_ALLOW_FRACTION |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getMemoryUsage(string $actualMemoryUsage): float |
126
|
|
|
{ |
127
|
|
|
$memoryUsage = filter_var( |
128
|
|
|
$actualMemoryUsage, |
129
|
|
|
FILTER_SANITIZE_NUMBER_FLOAT, |
130
|
|
|
FILTER_FLAG_ALLOW_FRACTION |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
if (strpos($actualMemoryUsage, 'Mb') !== false) { |
134
|
|
|
return (float) $memoryUsage * 1024; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return (float) $memoryUsage; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function storeBestRequest(string $request, string $timestamp, array $result) |
141
|
|
|
{ |
142
|
|
|
$this->bestRequests[$request] = (object) [ |
143
|
|
|
'timestamp' => $timestamp, |
144
|
|
|
'result' => (object) $result, |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function storeWorstRequest(string $request, string $timestamp, array $result) |
149
|
|
|
{ |
150
|
|
|
$this->worstRequests[$request] = (object) [ |
151
|
|
|
'timestamp' => $timestamp, |
152
|
|
|
'result' => (object) $result, |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.