Skip to content

Commit

Permalink
Added roundPrecision method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Feb 3, 2023
1 parent f67dedd commit 8cba867
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 50 deletions.
113 changes: 78 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,44 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda
use DragonCode\RuntimeComparison\Comparator;

(new Comparator())->compare(
fn () => sleep(1),
fn () => sleep(1),
fn () => /* some code */,
fn () => /* some code */,
);

(new Comparator())->compare([
fn () => sleep(1),
fn () => sleep(1),
fn () => /* some code */,
fn () => /* some code */,
]);

(new Comparator())->compare([
'foo' => fn () => sleep(1),
'bar' => fn () => sleep(1),
'foo' => fn () => /* some code */,
'bar' => fn () => /* some code */,
]);
```

Result example:

```
----- -------- --------
# foo bar
----- -------- --------
1 1.014 1.016
2 1.015 1.016
3 1.015 1.016
4 1.015 1.015
5 1.015 1.015
----- -------- --------
min 1.014 1.015
max 1.015 1.016
avg 1.015 1.016
----- -------- --------
winner loser
----- -------- --------
----- ------------------- -------------------
# 0 1
----- ------------------- -------------------
1 0.011713027954102 0.015522003173828
2 0.014931917190552 0.015424013137817
3 0.015513896942139 0.014975070953369
4 0.015083789825439 0.014898061752319
5 0.014750003814697 0.014961004257202
6 0.015435934066772 0.015391111373901
7 0.015177965164185 0.014806985855103
8 0.014681816101074 0.01552677154541
9 0.014717102050781 0.015773057937622
10 0.015694856643677 0.014908075332642
----- ------------------- -------------------
min 0.011713027954102 0.014806985855103
max 0.015694856643677 0.015773057937622
avg 0.014770030975342 0.015218615531921
----- ------------------- -------------------
winner loser
----- ------------------- -------------------
```

The time is specified in seconds rounded to the third decimal place.
Expand All @@ -81,10 +86,10 @@ By default, the comparator performs 10 iterations per callback, but you can chan
use DragonCode\RuntimeComparison\Comparator;

(new Comparator())
->iterations(20)
->iterations(5)
->compare(
fn () => sleep(1),
fn () => sleep(1),
fn () => /* some code */,
fn () => /* some code */,
);
```

Expand All @@ -101,23 +106,61 @@ use DragonCode\RuntimeComparison\Comparator;
(new Comparator())
->withoutData()
->compare([
'foo' => fn () => sleep(1),
'bar' => fn () => sleep(1),
'foo' => fn () => /* some code */,
'bar' => fn () => /* some code */,
]);
```

Result example:

```
----- ------- --------
# foo bar
----- ------- --------
min 1.004 1.001
max 1.014 1.013
avg 1.01 1.009
----- ------- --------
loser winner
----- ------- --------
----- ------------------- -------------------
# 0 1
----- ------------------- -------------------
min 0.01220703125 0.014835119247437
max 0.015632152557373 0.015995979309082
avg 0.014835715293884 0.01535861492157
----- ------------------- -------------------
winner loser
----- ------------------- -------------------
```

### Round Precision

By default, the script does not round measurement results, but you can specify the number of decimal places to which rounding can be performed.

For example:

```php
use DragonCode\RuntimeComparison\Comparator;

(new Comparator())
->iterations(5)
->roundPrecision(4)
->compare(
fn () => /* some code */,
fn () => /* some code */,
);
```

Result example:

```
----- -------- --------
# 0 1
----- -------- --------
1 0.0112 0.015
2 0.0147 0.0155
3 0.0153 0.0153
4 0.0157 0.015
5 0.0154 0.0158
----- -------- --------
min 0.0112 0.015
max 0.0157 0.0158
avg 0.0144 0.0153
----- -------- --------
winner loser
----- -------- --------
```

## License
Expand Down
9 changes: 8 additions & 1 deletion src/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Comparator
protected array $result = [];

public function __construct(
protected Runner $runner = new Runner(),
protected Runner $runner = new Runner(),
protected Transformer $transformer = new Transformer()
) {
$this->view = new View(new SymfonyStyle(
Expand All @@ -40,6 +40,13 @@ public function iterations(int $count): self
return $this;
}

public function roundPrecision(?int $precision): self
{
$this->transformer->setRoundPrecision($precision);

return $this;
}

public function withoutData(): self
{
$this->withData = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface Transformer
{
public function transform(array $data): array;
public function transform(array $data, ?int $roundPrecision): array;
}
4 changes: 1 addition & 3 deletions src/Services/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

class Runner
{
protected int $precision = 3;

public function call(callable $callback): float
{
$startAt = $this->time();
Expand All @@ -19,7 +17,7 @@ public function call(callable $callback): float

protected function diff(float $startAt): float
{
return round($this->time() - $startAt, $this->precision);
return $this->time() - $startAt;
}

protected function time(): float
Expand Down
5 changes: 5 additions & 0 deletions src/Transformers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ protected function put(array &$items, string $key, mixed $name, callable $callba
$items[$key]['#'] = $key;
$items[$key][$name] = $callback();
}

protected function round(float $value, ?int $precision): float
{
return is_numeric($precision) ? round($value, $precision) : $value;
}
}
2 changes: 1 addition & 1 deletion src/Transformers/Separator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Separator extends Base
{
public function transform(array $data): array
public function transform(array $data, ?int $roundPrecision): array
{
return [new TableSeparator()];
}
Expand Down
10 changes: 5 additions & 5 deletions src/Transformers/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ class Stats extends Base
'avg',
];

public function transform(array $data): array
public function transform(array $data, ?int $roundPrecision): array
{
return $this->calculate($data);
return $this->calculate($data, $roundPrecision);
}

protected function calculate(array $data): array
protected function calculate(array $data, ?int $roundPrecision): array
{
$items = [];

foreach ($data as $name => $iterations) {
foreach ($this->methods as $method) {
$this->put($items, $method, $name, fn () => $this->{$method}($iterations));
$this->put($items, $method, $name, fn () => $this->round(call_user_func([$this, $method], $iterations), $roundPrecision));
}
}

Expand All @@ -42,6 +42,6 @@ protected function max(array $values): float

protected function avg(array $values): float
{
return round(array_sum($values) / count($values), 3);
return array_sum($values) / count($values);
}
}
4 changes: 2 additions & 2 deletions src/Transformers/Times.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class Times extends Base
{
public function transform(array $data): array
public function transform(array $data, ?int $roundPrecision): array
{
$items = [];

foreach ($data as $name => $values) {
foreach ($values as $iteration => $time) {
$items[$iteration]['#'] = $iteration;
$items[$iteration][$name] = $time;
$items[$iteration][$name] = $this->round($time, $roundPrecision);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/Transformers/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class Transformer
{
protected ?int $roundPrecision = null;

public function setRoundPrecision(?int $precision): void
{
$this->roundPrecision = $precision;
}

public function forTime(array $data): array
{
return $this->resolve(Times::class, $data);
Expand Down Expand Up @@ -50,6 +57,6 @@ public function merge(array ...$arrays): array

protected function resolve(TransformerContract|string $transformer, array $data): array
{
return (new $transformer())->transform($data);
return (new $transformer())->transform($data, $this->roundPrecision);
}
}
2 changes: 1 addition & 1 deletion src/Transformers/Winner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Winner extends Base
{
public function transform(array $data): array
public function transform(array $data, ?int $roundPrecision): array
{
$values = $data['avg'];

Expand Down
10 changes: 10 additions & 0 deletions tests/Comparator/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ public function testAsArrayWithoutData(): void

$this->assertTrue(true);
}

public function testAsArrayRoundPrecision(): void
{
$this->comparator()->roundPrecision(4)->compare([
'first' => fn () => $this->work(),
'second' => fn () => $this->work(),
]);

$this->assertTrue(true);
}
}
20 changes: 20 additions & 0 deletions tests/Comparator/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,24 @@ public function testAsArrayWithoutData(): void

$this->assertTrue(true);
}

public function testAsPropertiesRound(): void
{
$this->comparator()->roundPrecision(4)->compare(
fn () => $this->work(),
fn () => $this->work(),
);

$this->assertTrue(true);
}

public function testAsArrayRound(): void
{
$this->comparator()->roundPrecision(4)->compare([
fn () => $this->work(),
fn () => $this->work(),
]);

$this->assertTrue(true);
}
}

0 comments on commit 8cba867

Please sign in to comment.