/** * @param $event * @param $parameters * * @return mixed|void */ public function emit($event, $parameters) { self::$depth++; $this->stopwatch->openSection(); if (isset($this->callbacks[$event])) { if (!$this->callbacks[$event][0]) { usort($this->callbacks[$event][1], function ($A, $B) { if ($A[0] == $B[0]) { return 0; } return $A[0] > $B[0] ? 1 : -1; }); $this->callbacks[$event][0] = true; } foreach ($this->callbacks[$event][1] as $item) { $name = $this->getCallableName($item[1]); $this->stopwatch->start($name); $diagnoseEvent = Event::create()->setEvent($event)->setCallback($name)->setDepth(self::$depth); $this->events[] = $diagnoseEvent; call_user_func_array($item[1], $this->buildParameters($parameters)); $stopwatchEvent = $this->stopwatch->stop($name); $diagnoseEvent->setDuration($stopwatchEvent->getDuration())->setMemory($stopwatchEvent->getMemory()); } } $this->stopwatch->stopSection($event); self::$depth--; }
/** * Fixes all files for the given finder. * * @param ConfigInterface $config A ConfigInterface instance * @param bool $dryRun Whether to simulate the changes or not * @param bool $diff Whether to provide diff * * @return array */ public function fix(ConfigInterface $config, $dryRun = false, $diff = false) { $changed = array(); $fixers = $config->getFixers(); $this->stopwatch->openSection(); $fileCacheManager = new FileCacheManager($config->usingCache(), $config->getCacheFile(), $config->getRules()); $processed = array(); foreach ($config->getFinder() as $file) { $name = $this->getFileRelativePathname($file); if (in_array($name, $processed, true)) { continue; } $processed[] = $name; if ($file->isDir() || $file->isLink()) { continue; } $this->stopwatch->start($this->getFileRelativePathname($file)); if ($fixInfo = $this->fixFile($file, $fixers, $dryRun, $diff, $fileCacheManager)) { $changed[$name] = $fixInfo; } $this->stopwatch->stop($this->getFileRelativePathname($file)); } $this->stopwatch->stopSection('fixFile'); return $changed; }
/** * Fixes all files for the given finder. * * @param ConfigInterface $config A ConfigInterface instance * @param bool $dryRun Whether to simulate the changes or not * @param bool $diff Whether to provide diff * * @return array */ public function fix(ConfigInterface $config, $dryRun = false, $diff = false) { $fixers = $this->prepareFixers($config); $changed = array(); if ($this->stopwatch) { $this->stopwatch->openSection(); } $fileCacheManager = new FileCacheManager($config->usingCache(), $config->getDir()); foreach ($config->getFinder() as $file) { if ($file->isDir()) { continue; } if ($this->stopwatch) { $this->stopwatch->start($this->getFileRelativePathname($file)); } if ($fixInfo = $this->fixFile($file, $fixers, $dryRun, $diff, $fileCacheManager)) { $changed[$this->getFileRelativePathname($file)] = $fixInfo; } if ($this->stopwatch) { $this->stopwatch->stop($this->getFileRelativePathname($file)); } } if ($this->stopwatch) { $this->stopwatch->stopSection('fixFile'); } return $changed; }
/** * Fixes all files for the given finder. * * @param ConfigInterface $config A ConfigInterface instance * @param bool $dryRun Whether to simulate the changes or not * @param bool $diff Whether to provide diff * * @return array */ public function fix(ConfigInterface $config, $dryRun = false, $diff = false) { $fixers = $this->prepareFixers($config); $fixers = $this->sortFixers($fixers); $changed = array(); if ($this->stopwatch) { $this->stopwatch->openSection(); } $fileCacheManager = new FileCacheManager($config->usingCache(), $config->getDir(), $fixers); $finder = $config->getFinder(); $finderIterator = $finder instanceof \IteratorAggregate ? $finder->getIterator() : $finder; foreach (new UniqueFileIterator($finderIterator) as $file) { if ($this->stopwatch) { $this->stopwatch->start($this->getFileRelativePathname($file)); } if ($fixInfo = $this->fixFile($file, $fixers, $dryRun, $diff, $fileCacheManager)) { $changed[$this->getFileRelativePathname($file)] = $fixInfo; } if ($this->stopwatch) { $this->stopwatch->stop($this->getFileRelativePathname($file)); } } if ($this->stopwatch) { $this->stopwatch->stopSection('fixFile'); } return $changed; }
/** * @dataProvider providerDump */ public function testDump($config, $zipped, $configFormat, $expectedOutput, $expectedFiles, $expectedFilesInZip) { $output = new BufferedOutput(); $progress = new ProgressHelper(); $outputDir = static::$cacheDir . '/dump'; $stopwatch = new Stopwatch(); $stopwatch->openSection(); $this->dumpManager->setStopwatch($stopwatch); $this->dumpManager->setOutput($output); $this->dumpManager->setProgress($progress); $files = $this->dumpManager->dump($config, $outputDir, $zipped, $configFormat); $stopwatch->stopSection('generate-test'); $events = $stopwatch->getSectionEvents('generate-test'); $keys = ['dumping_config', 'initializing_files', 'generating_rows', 'finalizing_files', 'compressing_files']; foreach ($keys as $key) { $this->assertArrayHasKey($key, $events); } $outputContent = $output->fetch(); $this->assertCount(count($expectedFiles), $files); foreach ($expectedFiles as $format => $pattern) { $this->assertRegExp('#' . $outputDir . '/' . $pattern . '#', $files[$format]); } if ($zipped && count($expectedFilesInZip)) { $zippedFiles = $this->unzip($files['zip']); sort($expectedFilesInZip); foreach ($expectedFilesInZip as $format => $pattern) { $this->assertRegExp('#' . $pattern . '#', $zippedFiles[$format]); } } $this->assertEquals($expectedOutput, $outputContent); }
/** * @param string $id * @param int $invalidBehavior * * @return object */ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) { if (!$this->stopwatch && $this->has('stopwatch')) { $this->stopwatch = parent::get('stopwatch'); $this->stopwatch->openSection(); $this->hasStopwatch = TRUE; } if ('stopwatch' === $id) { return $this->stopwatch; } Timer::start($id); if ($this->hasStopwatch) { $e = $this->stopwatch->start($id, 'service'); } $service = parent::get($id, $invalidBehavior); $this->tracedData[$id] = Timer::stop($id); if ($this->hasStopwatch && $e->isStarted()) { $e->stop(); } return $service; }
/** * @expectedException \LogicException */ public function testReopenANewSectionShouldThrowAnException() { $stopwatch = new Stopwatch(); $stopwatch->openSection('section'); }
<?php require_once 'vendor/autoload.php'; require_once 'functions.php'; use Symfony\Component\Stopwatch\Stopwatch; $stopwatch = new Stopwatch(); $stopwatch->openSection(); $stopwatch->start('do_phase_1'); doSomeFunction(); $stopwatch->stopSection('step1'); $stopwatch->openSection(); $stopwatch->start('do_phase_1'); $totalLap = 10; for ($count = 0; $count < $totalLap; $count++) { doSomeFunction(); $stopwatch->lap('do_phase_1'); } $stopwatch->stopSection('step2'); echo '<p>Step 1 :</p>'; $events_1 = $stopwatch->getSectionEvents('step1'); echo '<ul>'; foreach ($events_1 as $id => $event) { echo '<li> phase ' . $id . ':' . $event->getDuration() . '</li>'; } echo '</ul>'; echo '<p>Step 2 :</p>'; $events_2 = $stopwatch->getSectionEvents('step2'); echo '<ul>'; foreach ($events_2 as $id => $event) { echo '<li> phase ' . $id . ':' . $event->getDuration() . '</li>'; }
protected function execute(InputInterface $input, OutputInterface $output) { $stopwatch = new Stopwatch(); $stopwatch->openSection(); $app = $this->getApplication()->getSilex(); $configFile = $input->getArgument('config'); if (!file_exists($configFile)) { throw new \InvalidArgumentException('The config file ' . $configFile . ' does not exists.'); } $configFileExtension = pathinfo($configFile, PATHINFO_EXTENSION); if (!in_array($configFileExtension, ['json', 'xml'])) { throw new \InvalidArgumentException('The config file ' . $configFile . ' must be a JSON or XML file.'); } $outputConfigFormat = $input->getOption('config-format'); if (!in_array($outputConfigFormat, ['json', 'xml', 'all', 'auto'])) { throw new \InvalidArgumentException('The output config file format ' . $outputConfigFormat . ' is not allowed.'); } if ($outputConfigFormat == 'auto') { $outputConfigFormat = $configFileExtension; } $output->writeln('Loading <info>' . $configFile . '</info> ...'); $noZip = $input->getOption('no-zip'); $fakeNumber = $input->getOption('number'); $stopwatch->start('loading_config', 'generate_dumps'); $serializer = $app['fakery.config_serializer']; $config = $serializer->load($configFile); $config->setFakerConfig($app['fakery.faker.config']); //Configuration file Validation $errors = $app['validator']->validate($config); if (count($errors) > 0) { $flatErrors = []; foreach ($errors as $error) { $flatErrors[] = $error->getPropertyPath() . ' : ' . $error->getMessage(); } throw new \InvalidArgumentException('The config file ' . $configFile . ' is not a valid Fakery generator config file.' . "\n\n" . implode("\n", $flatErrors)); } $stopwatch->stop('loading_config'); $outputDir = $input->getArgument('output-dir'); if (empty($outputDir) || file_exists($outputDir) && !is_dir($outputDir)) { $outputDir = 'dump/' . time() . '_' . uniqid() . '_' . $config->getClassName(true); } if (is_numeric($fakeNumber)) { $config->setFakeNumber($fakeNumber); } $dumpManager = $app['fakery.console_dumper_manager']; $dumpManager->setStopwatch($stopwatch); $dumpManager->setOutput($output); $dumpManager->setProgress($this->getHelperSet()->get('progress')); $files = $dumpManager->dump($config, $outputDir, !$noZip, $outputConfigFormat); $stopwatch->stopSection('generate'); $events = $stopwatch->getSectionEvents('generate'); foreach ($files as $file) { $output->writeln('<info>' . $file . '</info> generated'); } $output->writeln("\n" . 'Performance Summary :' . "\n"); $totalEvent = isset($events['__section__']) ? $events['__section__'] : null; unset($events['__section__']); foreach ($events as $name => $event) { $this->formatStopwatchEvent($name, $event, $output, false); } $this->formatStopwatchEvent('total', $totalEvent, $output, true); }