Skip to content

Commit

Permalink
Merge pull request #267 from HendrikPrinsZA/feature/profiling-memory
Browse files Browse the repository at this point in the history
Refactored memory & test
  • Loading branch information
HendrikPrinsZA committed Sep 20, 2023
2 parents 48423d0 + e6c3db1 commit f9d1b8e
Show file tree
Hide file tree
Showing 83 changed files with 1,434 additions and 459 deletions.
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ commands:
setup-php:
steps:
- run:
name: Installing PHP modules
name: Installing PHP redis
command: |
yes 'no' | sudo pecl install -f redis || true
- run:
name: Installing PHP xdebug
command: |
sudo pecl install xdebug
- run:
name: Installing PHP pcov
command: |
Expand Down
2 changes: 2 additions & 0 deletions .env.circleci
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ DB_HOST_OVERRIDE=127.0.0.1
LK_PROGRESS_BAR_DISABLED=true
LK_DD_MAX_USERS=1000
LK_DD_MAX_USER_BLOGS=10
XDEBUG_MODE=profile,trace
SAIL_XDEBUG_MODE=profile,trace
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ LOG_LEVEL=debug
# CI_MODE=local
# LK_RUN_MODE=debug

# Xdebug
SAIL_XDEBUG_MODE=profile,trace
SAIL_XDEBUG_CONFIG="client_host=host.docker.internal output_dir=/var/www/html/storage/logs/xdebug"

# Database / Main
DB_CONNECTION=mysql
DB_HOST=mysql
Expand Down
6 changes: 4 additions & 2 deletions app/Challenges/A/CleanCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class CleanCode extends KataChallenge
{
protected const MAX_INTERATIONS = 150;

public function productsMake(int $iteration): float
{
$productController = app()->make(ProductController::class);
Expand All @@ -26,7 +28,7 @@ public function productsMake(int $iteration): float
$sum += $product->price;
}

return $this->return($sum);
return $sum;
}

public function shapes(int $iteration): float
Expand All @@ -43,6 +45,6 @@ public function shapes(int $iteration): float
$shapeRectangle->areaPlusCircumference();
}

return $this->return($sum);
return $sum;
}
}
4 changes: 2 additions & 2 deletions app/Challenges/A/CleanCodeDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function productsCreate(int $iteration): float
$sum += $product->price;
}

return $this->return($sum);
return $sum;
}

public function memoryAllocation(int $iteration): array
Expand All @@ -51,6 +51,6 @@ public function memoryAllocation(int $iteration): array
$resultArray[] = strrev($item);
}

return $this->return($resultArray);
return $resultArray;
}
}
41 changes: 8 additions & 33 deletions app/Challenges/A/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,51 @@

class Eloquent extends KataChallenge
{
protected const MAX_INTERATIONS = 100;
protected const MAX_INTERATIONS = 50;

protected const EXPECTED_MODELS = [
User::class,
ExchangeRate::class,
];

/**
* Eloquent collections / Average
*/
public function getCollectionAverage(int $iteration): float
{
$value = ExchangeRate::where('id', '<=', $iteration)
return ExchangeRate::where('id', '<=', $iteration)
->get()
->average('id');

return $this->return($value);
}

/**
* Eloquent collections / Unique
*/
public function getCollectionUnique(int $iteration): iterable
{
$value = ExchangeRate::all()
return ExchangeRate::query()
->where('id', '<=', $iteration)
->get()
->pluck('id')
->unique();

return $this->return($value);
}

/**
* Eloquent collections / Count
*/
public function getCollectionCount(int $iteration): int
{
$value = ExchangeRate::query()
return ExchangeRate::query()
->where('id', '<=', $iteration)
->get()
->count();

return $this->return($value);
}

/**
* Eloquent collections / Count
*
* Don’t use a collection to count the number of related entries.
*
* See https://codeburst.io/how-to-use-laravels-eloquent-efficiently-d46f5c392ca8
*/
public function getCollectionRelatedCount(int $iteration): int
{
$value = User::all()
return User::all()
->where('id', '<=', $iteration)
->last()
?->blogs->count() ?? 0;

return $this->return($value);
}

public function getMaxVersusOrder(int $iteration): float
{
$value = ExchangeRate::query()
return ExchangeRate::query()
->where('id', '<=', $iteration)
->orderByDesc('rate')
->first()?->rate;

return $this->return($value);
}

public function eagerLoading(int $iteration): float
Expand All @@ -89,6 +64,6 @@ public function eagerLoading(int $iteration): float
$value += $user->blogs()->count();
}

return $this->return($value);
return $value;
}
}
2 changes: 1 addition & 1 deletion app/Challenges/A/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function modelMutationVersusServiceMultiple(): float
]);
}

return $this->return($total);
return $total;
}

protected function getCountryByIndex(int $iteration): Country
Expand Down
4 changes: 2 additions & 2 deletions app/Challenges/A/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function orVersusIn(int $iteration): array

$value = $this->select($sql, $params);

return $this->return($value);
return $value;
}

public function orVersusInAggregate(int $iteration): float
Expand All @@ -59,7 +59,7 @@ public function orVersusInAggregate(int $iteration): float

$value = $this->selectOne($sql, $params)->rate;

return $this->return($value);
return $value;
}

protected function selectOne(string $sql, array $params = []): mixed
Expand Down
4 changes: 2 additions & 2 deletions app/Challenges/A/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function nativeRange(int $iteration): int
$range[] = $i;
}

return $this->return(count($range));
return count($range);
}

/**
Expand All @@ -39,7 +39,7 @@ public function nativeSum(int $iteration): int
$total += $number;
}

return $this->return($total);
return $total;
}

protected function getRangeLimit(int $iteration): int
Expand Down
10 changes: 5 additions & 5 deletions app/Challenges/A/Sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function calculatePi(): float
$denominator += 2;
}

return $this->return(round($sum, 2));
return round($sum, 2);
}

public function fizzBuzz(int $iteration): string
Expand Down Expand Up @@ -53,23 +53,23 @@ public function fizzBuzz(int $iteration): string
$result .= $output.'|';
}

return $this->return($result);
return $result;
}

public function memoryAllocation(int $iteration): array
public function memoryAllocation(int $iteration): string
{
$largeArray = range(1, $iteration);
$tempArray = [];

foreach ($largeArray as $item) {
$tempArray[] = str_repeat($item, 100);
$tempArray[] = str_repeat($item, floor($iteration / 10) + 1);
}

$resultArray = [];
foreach ($tempArray as $item) {
$resultArray[] = strrev($item);
}

return $this->return($resultArray);
return md5(implode('|', $resultArray));
}
}
4 changes: 2 additions & 2 deletions app/Challenges/B/CleanCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function productsMake(int $iteration): float
$sum += $product->price;
}

return $this->return($sum);
return $sum;
}

public function shapes(int $iteration): float
Expand All @@ -39,6 +39,6 @@ public function shapes(int $iteration): float
rectangle_area_plus_circumference(3.0 * ($i + 1), 7.0 * ($i + 1));
}

return $this->return($sum);
return $sum;
}
}
4 changes: 2 additions & 2 deletions app/Challenges/B/CleanCodeDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function productsCreate(int $iteration): float
$sum += $product->price;
}

return $this->return($sum);
return $sum;
}

public function memoryAllocation(int $iteration): array
Expand All @@ -36,6 +36,6 @@ public function memoryAllocation(int $iteration): array
$resultArray[] = strrev(str_repeat($item, 100));
}

return $this->return($resultArray);
return $resultArray;
}
}
29 changes: 9 additions & 20 deletions app/Challenges/B/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,40 @@ class Eloquent extends AEloquent
{
public function getCollectionAverage(int $iteration): float
{
$value = ExchangeRate::where('id', '<=', $iteration)->avg('id');

return $this->return($value);
return ExchangeRate::where('id', '<=', $iteration)->avg('id');
}

public function getCollectionUnique(int $iteration): iterable
{
$value = ExchangeRate::select('id')
return ExchangeRate::select('id')
->distinct()
->where('id', '<=', $iteration)
->pluck('id');

return $this->return($value);
}

public function getCollectionCount(int $iteration): int
{
$value = ExchangeRate::where('id', '<=', $iteration)->count();

return $this->return($value);
return ExchangeRate::where('id', '<=', $iteration)->count();
}

public function getCollectionRelatedCount(int $iteration): int
{
$value = User::where('id', '<=', $iteration)
return User::where('id', '<=', $iteration)
->orderByDesc('id')
->first()
?->blogs()->count() ?? 0;

return $this->return($value);
}

public function getMaxVersusOrder(int $iteration): float
{
$value = ExchangeRate::where('id', '<=', $iteration)->max('rate');

return $this->return($value);
return ExchangeRate::where('id', '<=', $iteration)->max('rate');
}

public function eagerLoading(int $iteration): float
{
$value = User::with('blogs')->where('id', '<=', $iteration)->get()
->reduce(
fn (int $total, User $user) => $total + $user->blogs->count(), 0);

return $this->return($value);
return User::with('blogs')
->where('id', '<=', $iteration)
->get()
->reduce(fn (int $total, User $user) => $total + $user->blogs->count(), 0);
}
}
2 changes: 1 addition & 1 deletion app/Challenges/B/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function modelMutationVersusServiceMultiple(): float
$total += array_sum($countryService->getExchangeRatesAggregates($country));
}

return $this->return($total);
return $total;
}
}
4 changes: 2 additions & 2 deletions app/Challenges/B/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function orVersusIn(int $iteration): array

$value = $this->select($sql, $params);

return $this->return($value);
return $value;
}

public function orVersusInAggregate(int $iteration): float
Expand All @@ -45,6 +45,6 @@ public function orVersusInAggregate(int $iteration): float

$value = $this->selectOne($sql, $params)->rate;

return $this->return($value);
return $value;
}
}
8 changes: 2 additions & 6 deletions app/Challenges/B/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ class Php extends APhp
{
public function nativeRange(int $iteration): int
{
$value = count(range(0, $this->getRangeLimit($iteration)));

return $this->return($value);
return count(range(0, $this->getRangeLimit($iteration)));
}

public function nativeSum(int $iteration): int
{
$numbers = range(0, $this->getRangeLimit($iteration));

return $this->return(array_sum($numbers));
return array_sum(range(0, $this->getRangeLimit($iteration)));
}
}
Loading

0 comments on commit f9d1b8e

Please sign in to comment.