Skip to content

Commit fc04aa5

Browse files
committed
testing
1 parent 0e3dcce commit fc04aa5

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ By default, all entries older than 24 hours will be pruned. You may use the hour
175175
$schedule->command('dev:prune --hours=48')->daily();
176176
```
177177

178+
179+
## Profiling
180+
181+
As a developer sometimes you have to measure the memory usage or time consumed for such action. Laravel Developer helps you to do so:
182+
183+
```php
184+
measure_memory(function() {
185+
// some code
186+
});
187+
```
188+
189+
And time measure:
190+
191+
```php
192+
measure_timing(function() {
193+
// some code
194+
})
195+
```
196+
178197
## Testing
179198

180199
``` bash

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": "^7.4|^8.0",
2020
"illuminate/contracts": "^8.0",
2121
"illuminate/support": "^7.0|^8.0",
22-
"laravel/slack-notification-channel": "^2.3"
22+
"laravel/slack-notification-channel": "^2.3",
23+
"symfony/stopwatch": "^3.4"
2324
},
2425
"require-dev": {
2526
"orchestra/testbench": "^6.0",
@@ -30,7 +31,10 @@
3031
"psr-4": {
3132
"Binarcode\\LaravelDeveloper\\": "src",
3233
"Binarcode\\LaravelDeveloper\\Database\\Factories\\": "database/factories"
33-
}
34+
},
35+
"files": [
36+
"src/helpers.php"
37+
]
3438
},
3539
"autoload-dev": {
3640
"psr-4": {

src/Profiling/ServerTiming.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function measure(string $key)
4141
return $this->stop($key);
4242
}
4343

44-
public function start(string $key)
44+
public function start(string $key = 'measure')
4545
{
4646
$this->stopwatch->start($key);
4747

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Tests\Profiling;
4+
5+
use Binarcode\LaravelDeveloper\Profiling\ServerMemory;
6+
use Binarcode\LaravelDeveloper\Profiling\ServerTiming;
7+
use Binarcode\LaravelDeveloper\Tests\TestCase;
8+
use Illuminate\Support\Collection;
9+
10+
class ServerMemoryTest extends TestCase
11+
{
12+
public function test_can_measure_memory()
13+
{
14+
/**
15+
* @var ServerMemory $memory
16+
*/
17+
$memory = ServerMemory::measure();
18+
19+
$i = Collection::times(10000, function ($i) {
20+
return $i;
21+
})->all();
22+
23+
collect($i);
24+
25+
$memory->stop();
26+
27+
$this->assertEquals(
28+
1,
29+
round($memory->getMemory())
30+
);
31+
32+
}
33+
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Tests\Profiling;
4+
5+
use Binarcode\LaravelDeveloper\Profiling\ServerTiming;
6+
use Binarcode\LaravelDeveloper\Tests\TestCase;
7+
8+
class ServerTimingTest extends TestCase
9+
{
10+
public function test_can_measure_timing()
11+
{
12+
/**
13+
* @var ServerTiming $timing
14+
*/
15+
$timing = ServerTiming::startWithoutKey();
16+
17+
sleep(1);
18+
19+
$timing->stopAllUnfinishedEvents();
20+
21+
$this->assertEquals(
22+
1,
23+
(int)$timing->getDuration()
24+
);
25+
26+
}
27+
28+
}

0 commit comments

Comments
 (0)