Skip to content

Commit 84efd5b

Browse files
committed
Added PerformanceMonitor
1 parent a56248e commit 84efd5b

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Eskrano\QuickbaseRest;
4+
5+
class PerformanceMonitor
6+
{
7+
/**
8+
* @var array
9+
*/
10+
protected $queries = [];
11+
12+
/**
13+
* @param array $query
14+
* @return $this
15+
*/
16+
public function save(array $query)
17+
{
18+
$this->queries[] = $query;
19+
20+
return $this;
21+
}
22+
23+
/**
24+
* @return array
25+
*/
26+
public function getQueries()
27+
{
28+
return $this->queries;
29+
}
30+
31+
/**
32+
* @return mixed|null
33+
*/
34+
public function getMostLongestQuery()
35+
{
36+
$longest = null;
37+
38+
foreach ($this->getQueries() as $query) {
39+
if ($longest !== null && $query['ms'] >= $longest['ms']) {
40+
$longest = $query;
41+
} else {
42+
$longest = $query;
43+
}
44+
}
45+
46+
return $longest;
47+
}
48+
49+
public function exportToFile(string $destination)
50+
{
51+
$put_result = file_put_contents(
52+
$destination,
53+
json_encode($this->getQueries(), JSON_PRETTY_PRINT)
54+
);
55+
56+
return $put_result;
57+
}
58+
}

src/QuickbaseRest/QuickbaseREST.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class QuickbaseREST
5858
*/
5959
public $convert_json_to_array = true;
6060

61+
/**
62+
* @var PerformanceMonitor
63+
*/
64+
protected $performance;
65+
6166
/**
6267
* QuickbaseREST constructor.
6368
* @param string $realm
@@ -89,6 +94,8 @@ public function __construct(
8994
$this->getSection($section);
9095
}
9196
}
97+
98+
$this->performance = new PerformanceMonitor();
9299
}
93100

94101
/**
@@ -133,10 +140,27 @@ public function tables(): Tables
133140
return $this->getSection(Tables::class);
134141
}
135142

143+
/**
144+
* @return PerformanceMonitor
145+
*/
146+
public function getPerformanceMonitor(): PerformanceMonitor
147+
{
148+
return $this->performance;
149+
}
136150

151+
/**
152+
* @param string $method
153+
* @param string $action
154+
* @param array $body
155+
* @param array $query_params
156+
* @return ResponseInterface
157+
* @throws \GuzzleHttp\Exception\GuzzleException
158+
*/
137159
public function query(string $method, string $action, array $body, array $query_params = []): ResponseInterface
138160
{
139161
try {
162+
$_start = microtime(true);
163+
140164
$response = $this->guzzle->request(
141165
$method,
142166
sprintf("%s?%s", $action, http_build_query($query_params)),
@@ -145,6 +169,14 @@ public function query(string $method, string $action, array $body, array $query_
145169
]
146170
);
147171

172+
$total_ms = microtime(true) - $_start;
173+
174+
$this->getPerformanceMonitor()
175+
->save([
176+
'query' => compact('method', 'action', 'body', 'query_params'),
177+
'ms' => $total_ms
178+
]);
179+
148180
return $response;
149181

150182
} catch (RequestException $exception) {

0 commit comments

Comments
 (0)