Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
# phpunit/php-timer
2
 
3
[![CI Status](https://github.com/sebastianbergmann/php-timer/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/php-timer/actions)
4
[![Type Coverage](https://shepherd.dev/github/sebastianbergmann/php-timer/coverage.svg)](https://shepherd.dev/github/sebastianbergmann/php-timer)
5
 
6
Utility class for timing things, factored out of PHPUnit into a stand-alone component.
7
 
8
## Installation
9
 
10
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
11
 
12
```
13
composer require phpunit/php-timer
14
```
15
 
16
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
17
 
18
```
19
composer require --dev phpunit/php-timer
20
```
21
 
22
## Usage
23
 
24
### Basic Timing
25
 
26
```php
27
require __DIR__ . '/vendor/autoload.php';
28
 
29
use SebastianBergmann\Timer\Timer;
30
 
31
$timer = new Timer;
32
 
33
$timer->start();
34
 
35
foreach (\range(0, 100000) as $i) {
36
    // ...
37
}
38
 
39
$duration = $timer->stop();
40
 
41
var_dump(get_class($duration));
42
var_dump($duration->asString());
43
var_dump($duration->asSeconds());
44
var_dump($duration->asMilliseconds());
45
var_dump($duration->asMicroseconds());
46
var_dump($duration->asNanoseconds());
47
```
48
 
49
The code above yields the output below:
50
 
51
```
52
string(32) "SebastianBergmann\Timer\Duration"
53
string(9) "00:00.002"
54
float(0.002851062)
55
float(2.851062)
56
float(2851.062)
57
int(2851062)
58
```
59
 
60
### Resource Consumption
61
 
62
#### Explicit duration
63
 
64
```php
65
require __DIR__ . '/vendor/autoload.php';
66
 
67
use SebastianBergmann\Timer\ResourceUsageFormatter;
68
use SebastianBergmann\Timer\Timer;
69
 
70
$timer = new Timer;
71
$timer->start();
72
 
73
foreach (\range(0, 100000) as $i) {
74
    // ...
75
}
76
 
77
print (new ResourceUsageFormatter)->resourceUsage($timer->stop());
78
```
79
 
80
The code above yields the output below:
81
 
82
```
83
Time: 00:00.002, Memory: 6.00 MB
84
```
85
 
86
#### Duration since PHP Startup (using unreliable `$_SERVER['REQUEST_TIME_FLOAT']`)
87
 
88
```php
89
require __DIR__ . '/vendor/autoload.php';
90
 
91
use SebastianBergmann\Timer\ResourceUsageFormatter;
92
 
93
foreach (\range(0, 100000) as $i) {
94
    // ...
95
}
96
 
97
print (new ResourceUsageFormatter)->resourceUsageSinceStartOfRequest();
98
```
99
 
100
The code above yields the output below:
101
 
102
```
103
Time: 00:00.002, Memory: 6.00 MB
104
```