/** * @param $url * @param array $arguments * @param string $uniqueId * * @throws \Exception * * @return string */ public function rasterizeUrl($url, $arguments = array(), $uniqueId = "") { if ($uniqueId === "") { $uniqueId = uniqid("rasterize-"); } if ($this->stopwatch instanceof Stopwatch) { if ($this->stopwatch->isStarted($uniqueId)) { $this->stopwatch->lap($uniqueId); } else { $this->stopwatch->start($uniqueId); } } $process = $this->configHelper->buildProcess($url, $uniqueId, $arguments); $exitCode = $process->run(); if ($exitCode != 0) { throw new \Exception(sprintf("Rasterize script failed.\nCommandLine: %s\nExitCode: %d\nErrorOutput: %s", $process->getCommandLine(), $process->getExitCode(), $process->getErrorOutput())); } if ($this->stopwatch instanceof Stopwatch) { $this->stopwatch->stop($uniqueId); } $output = $this->configHelper->getOutputFilePath($uniqueId); $content = file_get_contents($output); unlink($output); return $content; }
/** * Ends the watch, extract logs and write them to the storage service chosen. * @param $identifier * @param OutputInterface|null $output If you want to write on output the logs * @param array $extra * * @return array */ public function end($identifier, OutputInterface $output = null, $extra = array()) { $logs = array(); if ($this->stopWatch->isStarted($identifier)) { $stopWatchEvent = $this->stopWatch->stop($identifier); $logs = $this->extractLog($stopWatchEvent, $output, $extra); $this->writer->write($logs, $identifier); } return $logs; }
/** * Mark action for the event on Stopwatch. * * @param mixed $input pass through if added to filter * * @return mixed */ static function tick($input = null) { global $wp_filter; $filter = current_filter(); $priority = key($wp_filter[$filter]); $event = wp_parse_args(self::$events[$filter][$priority], array('action' => 'start', 'category' => null)); if ('stop' === $event['action'] && !self::$stopwatch->isStarted($event['event'])) { return $input; } self::$stopwatch->{$event['action']}($event['event'], $event['category']); return $input; }
public static function stopwatch(Stopwatch $stopwatch) { return function (callable $handler) use($stopwatch) { return function (RequestInterface $request, array $options) use($handler, $stopwatch) { $uri = (string) $request->getUri(); if (!$stopwatch->isStarted($uri)) { $stopwatch->start($uri); } return $handler($request, $options)->then(function (ResponseInterface $response) use($stopwatch, $uri) { $stopwatch->stop($uri); return $response; }); }; }; }
/** * Finish current licenser process log and display results in the output. */ public function finishProcess() { $total = $this->countTotal(); $additions = $this->countAdditions(); $updates = $this->countUpdates(); $untouched = $this->countUntouched(); $formatter = new FormatterHelper(); //summary $this->output->writeln('', OutputInterface::VERBOSITY_VERBOSE); $this->output->writeln(sprintf('<fg=green>[+] Additions: %s</>', $additions), OutputInterface::VERBOSITY_VERBOSE); $this->output->writeln(sprintf('<fg=cyan>[u] Updates: %s</>', $updates), OutputInterface::VERBOSITY_VERBOSE); $this->output->writeln(sprintf('<fg=yellow>[=] Untouched: %s</>', $untouched), OutputInterface::VERBOSITY_VERBOSE); $this->output->writeln(''); if ($this->watch->isStarted('licenser')) { $event = $this->watch->stop('licenser'); $processMessage = sprintf('%s file(s) has been processed in %s ms, memory usage %.2F MiB', $total, $event->getDuration(), $event->getMemory() / 1024 / 1024); if ($this->mode & Licenser::MODE_NORMAL || $this->mode & Licenser::MODE_DRY_RUN) { $style = new OutputFormatterStyle('white', $this->mode === Licenser::MODE_DRY_RUN ? 'cyan' : 'green', ['bold']); $this->output->getFormatter()->setStyle('success', $style); $formattedBlock = $formatter->formatBlock($processMessage, 'success', true); $this->output->writeln($formattedBlock); } elseif ($this->mode & Licenser::MODE_CHECK_ONLY) { $needUpdate = $additions + $updates > 0; if ($needUpdate) { $successMsg = sprintf('[WARN] %s file(s) should be updated.', $additions + $updates); } else { $successMsg = '[OK] All files contains a valid license header.'; } $style = new OutputFormatterStyle('white', $needUpdate ? 'red' : 'green', ['bold']); $this->output->getFormatter()->setStyle('success', $style); $formattedBlock = $formatter->formatBlock([$successMsg, $processMessage], 'success', true); $this->output->writeln($formattedBlock); } if ($this->mode === Licenser::MODE_DRY_RUN) { $this->output->writeln(''); $this->output->writeln('<fg=yellow>NOTE: The command run in dry-run mode, it not made any changes.</>'); } } }
public function testIsNotStarted() { $stopwatch = new Stopwatch(); $this->assertFalse($stopwatch->isStarted('foo')); }
/** * Render interface and add to the toolbar. * * @param \WP_Admin_Bar $wp_admin_bar */ public static function admin_bar_menu($wp_admin_bar) { if (!apply_filters('laps_can_see', current_user_can('manage_options'))) { return; } global $timestart, $wpdb; $mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader(dirname(__DIR__) . '/views'), 'cache' => new Mustache_Cache_FrozenCache(dirname(__DIR__) . '/views/cache'))); if (self::$stopwatch->isStarted('Toolbar')) { self::$stopwatch->stop('Toolbar'); } $events = self::$stopwatch->getSectionEvents('__root__'); $start = $timestart * 1000; $end = microtime(true) * 1000; $total = $end - $start; $event_data = array(); $http_data = array(); foreach ($events as $name => $event) { $offset = round(($event->getOrigin() - $start) / $total * 100, 2); $duration = $event->getDuration(); $width = round($duration / $total * 100, 2); $category = $event->getCategory(); if ('http' === $category) { $http_data[] = compact('name', 'offset', 'duration', 'width', 'category'); continue; } $memory = $event->getMemory() / 1024 / 1024; $event_data[] = compact('name', 'offset', 'duration', 'width', 'category', 'memory'); } $query_data = array(); $last_query_end = 0; $last_offset = 0; $last_duration = 0; if (defined('SAVEQUERIES') && SAVEQUERIES) { foreach ($wpdb->queries as $key => $query) { $query_start = isset(self::$query_starts[$key]) ? self::$query_starts[$key] : $last_query_end; list($sql, $duration, $trace) = $query; $sql = trim($sql); $category = 'query-read'; if (0 === stripos($sql, 'INSERT') || 0 === stripos($sql, 'UPDATE')) { $category = 'query-write'; } $duration *= 1000; $last_query_end = $query_start + $duration; $offset = round(($query_start - $start) / $total * 100, 2); // if query is indistinguishably close to previous then stack it if ($offset === $last_offset) { $key = count($query_data) - 1; $query_data[$key]['sql'] .= '<br />' . $sql; $last_duration += $duration; $width = round($last_duration / $total * 100, 2); $query_data[$key]['width'] = $width; continue; } $width = round($duration / $total * 100, 2); $last_offset = $offset; $last_duration = $duration; $query_data[] = compact('sql', 'duration', 'offset', 'width', 'category'); } } $html = $mustache->render('laps', array('events' => $event_data, 'queries' => $query_data, 'savequeries' => defined('SAVEQUERIES') && SAVEQUERIES, 'http' => $http_data, 'savehttp' => !empty($http_data))); $wp_admin_bar->add_node(array('id' => 'laps', 'title' => sprintf('Lap: %ss', round($total / 1000, 3)))); $wp_admin_bar->add_node(array('id' => 'laps_output', 'parent' => 'laps', 'meta' => array('html' => $html))); }