Skip to content

Commit

Permalink
restore metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jul 16, 2024
1 parent bdde843 commit 975b473
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rekalogika\Rekapager\Batch\BatchProcess;
use Rekalogika\Rekapager\Batch\BatchProcessFactoryInterface;
use Rekalogika\Rekapager\Batch\BatchProcessorInterface;
use Rekalogika\Rekapager\Symfony\Batch\Internal\CommandBatchProcessorDecorator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
* that was distributed with this source code.
*/

namespace Rekalogika\Rekapager\Symfony\Batch;
namespace Rekalogika\Rekapager\Symfony\Batch\Internal;

use Rekalogika\Contracts\Rekapager\Exception\LogicException;

/**
* @internal
*/
class BatchTimer
{
public const TIMER_PROCESS = 'process';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* that was distributed with this source code.
*/

namespace Rekalogika\Rekapager\Symfony\Batch;
namespace Rekalogika\Rekapager\Symfony\Batch\Internal;

use Rekalogika\Rekapager\Batch\BatchProcessorDecorator;
use Rekalogika\Rekapager\Batch\BatchProcessorInterface;
Expand All @@ -20,17 +20,21 @@
use Rekalogika\Rekapager\Batch\Event\BeforePageEvent;
use Rekalogika\Rekapager\Batch\Event\BeforeProcessEvent;
use Rekalogika\Rekapager\Batch\Event\InterruptEvent;
use Rekalogika\Rekapager\Batch\Event\ItemEvent;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @template TKey of array-key
* @template T
* @extends BatchProcessorDecorator<TKey,T>
* @internal
*/
class CommandBatchProcessorDecorator extends BatchProcessorDecorator
{
private readonly BatchTimer $timer;
private int $pageNumber = 0;
private int $itemNumber = 0;

/**
* @param BatchProcessorInterface<TKey,T> $decorated
Expand Down Expand Up @@ -71,14 +75,13 @@ public function afterProcess(AfterProcessEvent $event): void
unlink($this->progressFile);
}

$batchDuration = $this->timer->stop(BatchTimer::TIMER_PROCESS);

$this->io->success('Batch process completed');
$this->showStats($event);
}

public function beforePage(BeforePageEvent $event): void
{
$this->pageNumber++;
$this->timer->start(BatchTimer::TIMER_PAGE);

if ($this->progressFile !== null) {
Expand Down Expand Up @@ -108,6 +111,13 @@ public function afterPage(AfterPageEvent $event): void
}
}

public function processItem(ItemEvent $itemEvent): void
{
$this->itemNumber++;

$this->decorated->processItem($itemEvent);
}

public function onInterrupt(InterruptEvent $event): void
{
$this->decorated->onInterrupt($event);
Expand Down Expand Up @@ -138,13 +148,14 @@ public function onInterrupt(InterruptEvent $event): void
private function showStats(AfterPageEvent|AfterProcessEvent|InterruptEvent $event): void
{
$this->io->writeln('');
$processDuration = $this->timer->getDuration(BatchTimer::TIMER_PROCESS);
$this->io->definitionList(
// ['Time elapsed' => Helper::formatTime($event->getProcessDuration())],
['Time elapsed' => Helper::formatTime($processDuration ?? 0)],
['Page processed' => $this->pageNumber],
['Item processed' => $this->itemNumber],
['Memory usage' => Helper::formatMemory(memory_get_usage(true))],
// ['Pages processed' => $event->getPagesProcessed()],
// ['Items processed' => $event->getItemsProcessed()],
// ['Pages/minute' => round($event->getPagesProcessed() / $event->getProcessDuration() * 60, 2)],
// ['Items/minute' => round($event->getItemsProcessed() / $event->getProcessDuration() * 60, 2)],
['Pages/minute' => round($this->pageNumber / $processDuration * 60, 2)],
['Items/minute' => round($this->itemNumber / $processDuration * 60, 2)],
);
}
}

0 comments on commit 975b473

Please sign in to comment.