Skip to content

Commit

Permalink
cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jul 16, 2024
1 parent 975b473 commit 7ab6239
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,6 @@ public function reset(string $timer): void
unset($this->timers[$timer]);
}

/**
* @param BatchTimer::TIMER_* $timer
*/
public function getStart(string $timer): float
{
if (!isset($this->timers[$timer])) {
throw new LogicException(sprintf('Timer "%s" is not started.', $timer));
}

return $this->timers[$timer] / 1e9;
}

/**
* @param BatchTimer::TIMER_* $timer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rekalogika\Rekapager\Batch\Event\InterruptEvent;
use Rekalogika\Rekapager\Batch\Event\ItemEvent;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
Expand All @@ -35,6 +36,8 @@ class CommandBatchProcessorDecorator extends BatchProcessorDecorator
private readonly BatchTimer $timer;
private int $pageNumber = 0;
private int $itemNumber = 0;
private ?\DateTimeInterface $startTime = null;
private readonly ProgressIndicator $progressIndicator;

/**
* @param BatchProcessorInterface<TKey,T> $decorated
Expand All @@ -47,21 +50,29 @@ public function __construct(
parent::__construct($decorated);

$this->timer = new BatchTimer();
$this->progressIndicator = new ProgressIndicator($this->io, 'very_verbose');
}

private static function formatTime(\DateTimeInterface $time): string
{
return $time->format('Y-m-d H:i:s T');
}

public function beforeProcess(BeforeProcessEvent $event): void
{
$this->startTime = new \DateTimeImmutable();
$this->timer->start(BatchTimer::TIMER_DISPLAY);
$this->timer->start(BatchTimer::TIMER_PROCESS);

$this->io->success('Starting batch process');

$this->io->definitionList(
['Start time' => self::formatTime($this->startTime)],
['Start page' => $event->getStartPageIdentifier() ?? '(first page)'],
['Progress file' => $this->progressFile ?? '(not used)'],
['Items per page' => $event->getPageable()->getItemsPerPage()],
// ['Total pages' => $event->getTotalPages() ?? '(unknown)'],
// ['Total items' => $event->getTotalItems() ?? '(unknown)'],
['Total pages' => $event->getPageable()->getTotalPages() ?? '(unknown)'],
['Total items' => $event->getPageable()->getTotalItems() ?? '(unknown)'],
);

$this->decorated->beforeProcess($event);
Expand All @@ -82,25 +93,30 @@ public function afterProcess(AfterProcessEvent $event): void
public function beforePage(BeforePageEvent $event): void
{
$this->pageNumber++;
$this->timer->start(BatchTimer::TIMER_PAGE);
// $this->timer->start(BatchTimer::TIMER_PAGE);

if ($this->progressFile !== null) {
file_put_contents($this->progressFile, $event->getEncodedPageIdentifier());
}

$this->progressIndicator->start(sprintf(
'Page <info>%s</info>, identifier <info>%s</info>',
$event->getPage()->getPageNumber() ?? '(unknown)',
$event->getEncodedPageIdentifier(),
));

$this->decorated->beforePage($event);
}

public function afterPage(AfterPageEvent $event): void
{
$this->decorated->afterPage($event);
$pageDuration = $this->timer->stop(BatchTimer::TIMER_PAGE);
// $pageDuration = $this->timer->stop(BatchTimer::TIMER_PAGE);

$this->io->writeln(sprintf(
'Page processed, page number: <info>%s</info>, identifier: <info>%s</info>, duration: <info>%s</info>',
$this->progressIndicator->finish(sprintf(
'Page <info>%s</info>, identifier <info>%s</info>',
$event->getPage()->getPageNumber() ?? '(unknown)',
$event->getEncodedPageIdentifier(),
Helper::formatTime($pageDuration)
));

$displayDuration = $this->timer->getDuration(BatchTimer::TIMER_DISPLAY);
Expand All @@ -115,6 +131,13 @@ public function processItem(ItemEvent $itemEvent): void
{
$this->itemNumber++;

$sinceLast = $this->timer->getDuration(BatchTimer::TIMER_ITEM);

if ($sinceLast === null || $sinceLast > 1) {
$this->timer->restart(BatchTimer::TIMER_ITEM);
$this->progressIndicator->advance();
}

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

Expand Down Expand Up @@ -147,15 +170,40 @@ public function onInterrupt(InterruptEvent $event): void
*/
private function showStats(AfterPageEvent|AfterProcessEvent|InterruptEvent $event): void
{
$this->io->writeln('');
if ($event instanceof AfterPageEvent) {
$this->io->writeln('');
}

$processDuration = $this->timer->getDuration(BatchTimer::TIMER_PROCESS);
$this->io->definitionList(
['Time elapsed' => Helper::formatTime($processDuration ?? 0)],

$stats = [];

if ($this->startTime !== null) {
$stats[] = ['Start time' => self::formatTime($this->startTime)];
}

if ($event instanceof AfterPageEvent) {
$stats[] = ['Current time' => self::formatTime(new \DateTimeImmutable())];
} else {
$stats[] = ['End time' => self::formatTime(new \DateTimeImmutable())];
}

if ($processDuration !== null) {
$stats[] = ['Time elapsed' => Helper::formatTime($processDuration)];
}

$stats = [
...$stats,
['Page processed' => $this->pageNumber],
['Item processed' => $this->itemNumber],
['Memory usage' => Helper::formatMemory(memory_get_usage(true))],
['Pages/minute' => round($this->pageNumber / $processDuration * 60, 2)],
['Items/minute' => round($this->itemNumber / $processDuration * 60, 2)],
);
];

if ($processDuration !== null) {
$stats[] = ['Pages/minute' => round($this->pageNumber / $processDuration * 60, 2)];
$stats[] = ['Items/minute' => round($this->itemNumber / $processDuration * 60, 2)];
}

$this->io->definitionList(...$stats);
}
}
2 changes: 1 addition & 1 deletion tests/src/App/BatchProcessor/PostBatchProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(private EntityManagerInterface $entityManager)

public function processItem(ItemEvent $itemEvent): void
{
usleep(20000);
usleep(50000);
}

public function afterPage(AfterPageEvent $event): void
Expand Down
2 changes: 1 addition & 1 deletion tests/src/App/Command/AppSimpleBatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function getPageable(InputInterface $input, OutputInterface $output):

public function processItem(ItemEvent $itemEvent): void
{
usleep(20000);
usleep(50000);
}

public function afterPage(AfterPageEvent $event): void
Expand Down

0 comments on commit 7ab6239

Please sign in to comment.