Skip to content

Commit 585db6c

Browse files
committed
Add request time and memory usage
1 parent 0eea269 commit 585db6c

File tree

7 files changed

+99
-19
lines changed

7 files changed

+99
-19
lines changed

config/api-debugger.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313

1414
// Profile custom events.
1515
\Lanin\Laravel\ApiDebugger\Collections\ProfilingCollection::class,
16+
17+
// Memory usage.
18+
\Lanin\Laravel\ApiDebugger\Collections\MemoryCollection::class,
1619
],
1720
];

phpunit.xml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnFailure="false"
11-
syntaxCheck="false"
1211
>
1312
<testsuites>
1413
<testsuite name="Package Test Suite">

src/Collections/MemoryCollection.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Lanin\Laravel\ApiDebugger\Collections;
4+
5+
use Lanin\Laravel\ApiDebugger\Collection;
6+
7+
class MemoryCollection implements Collection
8+
{
9+
/**
10+
* Collection name.
11+
*
12+
* @return string
13+
*/
14+
public function name()
15+
{
16+
return 'memory';
17+
}
18+
19+
/**
20+
* Returns resulting collection.
21+
*
22+
* @return array
23+
*/
24+
public function items()
25+
{
26+
return [
27+
'usage' => memory_get_usage(),
28+
'peak' => memory_get_peak_usage(),
29+
];
30+
}
31+
}

src/Collections/ProfilingCollection.php

+34-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class ProfilingCollection implements Collection
1111
{
12+
const REQUEST_TIMER = 'request-time';
13+
1214
/**
1315
* @var Dispatcher
1416
*/
@@ -61,17 +63,42 @@ public function items()
6163
*/
6264
public function listen()
6365
{
66+
if (defined('LARAVEL_START')) {
67+
$this->start(static::REQUEST_TIMER, LARAVEL_START);
68+
}
69+
6470
$this->dispatcher->listen(StartProfiling::class, function (StartProfiling $event) {
65-
$this->started[$event->name] = microtime(true);
71+
$this->start($event->name);
6672
});
6773

6874
$this->dispatcher->listen(StopProfiling::class, function (StopProfiling $event) {
69-
if (array_key_exists($event->name, $this->started)) {
70-
$this->timers[] = [
71-
'event' => $event->name,
72-
'time' => microtime(true) - $this->started[$event->name],
73-
];
74-
}
75+
$this->stop($event->name);
7576
});
7677
}
78+
79+
/**
80+
* Start timer.
81+
*
82+
* @param string $name
83+
* @param null $time
84+
*/
85+
protected function start($name, $time = null)
86+
{
87+
$this->started[$name] = $time ?: microtime(true);
88+
}
89+
90+
/**
91+
* Stop timer.
92+
*
93+
* @param string $name
94+
*/
95+
protected function stop($name)
96+
{
97+
if (array_key_exists($name, $this->started)) {
98+
$this->timers[] = [
99+
'event' => $name,
100+
'time' => microtime(true) - $this->started[$name],
101+
];
102+
}
103+
}
77104
}

src/Debugger.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Http\Events\RequestHandled;
77
use Illuminate\Http\JsonResponse;
88
use Illuminate\Http\Request;
9+
use Lanin\Laravel\ApiDebugger\Collections\ProfilingCollection;
910
use Lanin\Laravel\ApiDebugger\Events\StopProfiling;
1011
use Lanin\Laravel\ApiDebugger\Events\StartProfiling;
1112
use Symfony\Component\HttpFoundation\Response;
@@ -65,7 +66,6 @@ public function dump()
6566
* Start profiling event.
6667
*
6768
* @param string $name
68-
* @return mixed
6969
*/
7070
public function startProfiling($name)
7171
{
@@ -106,6 +106,8 @@ public function profileMe($name, \Closure $action)
106106
*/
107107
protected function updateResponse(Request $request, Response $response)
108108
{
109+
$this->stopProfiling(ProfilingCollection::REQUEST_TIMER);
110+
109111
if ($this->needToUpdateResponse($response)) {
110112
$data = $response->getData(true) ?: [];
111113
$data[$this->responseKey] = $this->storage->getData();

src/Support/helpers.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ function lad()
2020
* Start profiling event.
2121
*
2222
* @param string $name
23-
* @param Closure|null $action
2423
* @return void
2524
*/
26-
function lad_pr_start($name, \Closure $action = null)
25+
function lad_pr_start($name)
2726
{
28-
app(Debugger::class)->startProfiling($name, $action);
27+
app(Debugger::class)->startProfiling($name);
2928
}
3029
}
3130

tests/DebuggerTest.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77

88
class DebuggerTest extends TestCase
99
{
10+
/** @test */
11+
public function it_can_dump_memory_usage()
12+
{
13+
$this->app['router']->get('foo', function () {
14+
return response()->json(['foo' => 'bar']);
15+
});
16+
17+
$this->json('get', '/foo')
18+
->assertStatus(200)
19+
->assertJsonStructure([
20+
'debug' => [
21+
'memory' => [
22+
'usage',
23+
'peak',
24+
],
25+
],
26+
]);
27+
}
28+
1029
/** @test */
1130
public function it_can_dump_var_via_helper()
1231
{
@@ -21,7 +40,7 @@ public function it_can_dump_var_via_helper()
2140
->assertJsonFragment([
2241
'dump' => [
2342
'baz',
24-
]
43+
],
2544
]);
2645
}
2746

@@ -39,7 +58,7 @@ public function it_can_dump_multiple_vars()
3958
->assertJsonFragment([
4059
'dump' => [
4160
['baz1', 'baz2'],
42-
]
61+
],
4362
]);
4463
}
4564

@@ -164,8 +183,8 @@ public function it_can_show_cache_events()
164183
'keys',
165184
'total',
166185
],
167-
]
168-
]
186+
],
187+
],
169188
])
170189
->assertJsonFragment([
171190
'miss' => [
@@ -174,10 +193,10 @@ public function it_can_show_cache_events()
174193
'tags' => ['foo'],
175194
'key' => 'bar',
176195
],
177-
'bar'
196+
'bar',
178197
],
179-
'total' => 2
180-
]
198+
'total' => 2,
199+
],
181200
]);
182201
}
183202
}

0 commit comments

Comments
 (0)