Exemplo n.º 1
0
 public function stopQuery()
 {
     if ($this->explainRunning) {
         return;
     }
     $keys = array_keys($this->queries);
     $key = end($keys);
     $this->queries[$key][self::TIME] = Debugger::timer('doctrine');
     $this->totalTime += $this->queries[$key][self::TIME];
     // get EXPLAIN for SELECT queries
     if ($this->doExplains) {
         if ($this->connection === NULL) {
             throw new InvalidStateException('You must set a Doctrine\\DBAL\\Connection to get EXPLAIN.');
         }
         $query = $this->queries[$key][self::SQL];
         if (!Strings::startsWith($query, 'SELECT')) {
             // only SELECTs are supported
             return;
         }
         // prevent logging explains & infinite recursion
         $this->explainRunning = TRUE;
         $params = $this->queries[$key][self::PARAMS];
         $types = $this->queries[$key][self::TYPES];
         $stmt = $this->connection->executeQuery('EXPLAIN ' . $query, $params, $types);
         $this->queries[$key][self::EXPLAIN] = $stmt->fetchAll();
         $this->explainRunning = FALSE;
     }
 }
Exemplo n.º 2
0
 public function startup()
 {
     parent::startup();
     \Tracy\Debugger::timer('global');
     // Log visits
     // todo: replace by mongoDB log_visit
     /*
     
     		$httpRequest = $this->getHttpRequest();
     		$requestHeaders = apache_request_headers();
     		$ipAddress = (empty($requestHeaders['X-Forwarded-For']) ? $httpRequest->getRemoteAddress() : $requestHeaders['X-Forwarded-For']);
     		// ip address can be more values divided by comma (it's path to remote server), take only the last (most accurate IP of visitor)
     		$ipAddressParts = explode(',', $ipAddress);
     		$ipAddress = array_pop($ipAddressParts);
     		if ($httpRequest->getUrl()->path != '/healthy-check') {
     
     
     			$this->lastLogItem = $this->logger->logVisit(
     				$httpRequest->getUrl()->path, // URL
     				$ipAddress,  // IP
     				$httpRequest->getHeader('User-Agent'),  // USER_AGENT
     				$httpRequest->getReferer()  // REFERRER
     			);
     		}*/
 }
Exemplo n.º 3
0
 /**
  * @param $tempTableName
  * @param $columns
  * @param CsvFile $csvFile
  * @param array $options
  *  - isManifest
  *  - copyOptions
  * @throws Exception
  * @throws \Exception
  */
 protected function importTable($tempTableName, $columns, CsvFile $csvFile, array $options)
 {
     if ($csvFile->getEnclosure() && $csvFile->getEscapedBy()) {
         throw new Exception('Invalid CSV params. Either enclosure or escapedBy must be specified for Redshift backend but not both.', Exception::INVALID_CSV_PARAMS, null);
     }
     try {
         Debugger::timer('copyToStaging');
         $copyOptions = ['isManifest' => $options['isManifest'], 'copyOptions' => isset($options['copyOptions']) ? $options['copyOptions'] : []];
         if ($options['isManifest']) {
             $manifest = $this->downloadManifest($csvFile->getPathname());
             // empty manifest handling - do nothing
             if (!count($manifest['entries'])) {
                 $this->addTimer('copyToStaging', Debugger::timer('copyToStaging'));
                 return;
             }
             $copyOptions['isGzipped'] = $this->isGzipped(reset($manifest['entries'])['url']);
         } else {
             $copyOptions['isGzipped'] = $this->isGzipped($csvFile->getPathname());
         }
         $this->query($this->generateCopyCommand($tempTableName, $columns, $csvFile, $copyOptions));
         $this->addTimer('copyToStaging', Debugger::timer('copyToStaging'));
     } catch (\Exception $e) {
         $result = $this->connection->query("SELECT * FROM stl_load_errors WHERE query = pg_last_query_id();")->fetchAll();
         if (!count($result)) {
             throw $e;
         }
         $messages = [];
         foreach ($result as $row) {
             $messages[] = "Line {$row['line_number']} - {$row['err_reason']}";
         }
         $message = "Load error: " . implode("\n", $messages);
         throw new Exception($message, Exception::INVALID_SOURCE_DATA, $e);
     }
 }
Exemplo n.º 4
0
 protected function importDataToStagingTable($stagingTableName, $columns, $sourceData)
 {
     if (!isset($sourceData['schemaName'])) {
         throw new Exception('Invalid source data. schemaName must be set', Exception::INVALID_SOURCE_DATA);
     }
     if (!isset($sourceData['tableName'])) {
         throw new Exception('Invalid source data. schemaName must be set', Exception::INVALID_SOURCE_DATA);
     }
     $sql = "INSERT INTO " . $this->nameWithSchemaEscaped($stagingTableName) . " (" . implode(', ', array_map(function ($column) {
         return $this->quoteIdentifier($column);
     }, $columns)) . ") ";
     $sql .= "SELECT " . implode(',', array_map(function ($column) {
         return $this->quoteIdentifier($column);
     }, $columns)) . " FROM " . $this->nameWithSchemaEscaped($sourceData['tableName'], $sourceData['schemaName']);
     try {
         Debugger::timer('copyToStaging');
         $this->connection->query($sql);
         $rows = $this->connection->fetchAll(sprintf('SELECT COUNT(*) as "count" from %s.%s', $this->connection->quoteIdentifier($this->schemaName), $this->connection->quoteIdentifier($stagingTableName)));
         $this->importedRowsCount += (int) $rows[0]['count'];
         $this->addTimer('copyToStaging', Debugger::timer('copyToStaging'));
     } catch (\Exception $e) {
         // everything is user error
         throw new Exception($e->getMessage(), Exception::UNKNOWN_ERROR, $e);
     }
 }
Exemplo n.º 5
0
 /**
  * @param $tempDir
  * @param array $guzzleConfig
  * @return \GuzzleHttp\Client
  * @throws \Matyx\Guzzlette\GuzzletteException
  */
 public static function createGuzzleClient($tempDir, $guzzleConfig = [])
 {
     if (isset(static::$client)) {
         return static::$client;
     }
     if (Tracy\Debugger::isEnabled()) {
         $handler = NULL;
         if (isset($guzzleConfig['handler'])) {
             $handler = $guzzleConfig['handler'];
             if (!$handler instanceof GuzzleHttp\HandlerStack) {
                 throw new GuzzletteException("Handler must be instance of " . GuzzleHttp\HandlerStack::class);
             }
         } else {
             $handler = GuzzleHttp\HandlerStack::create();
         }
         $requestStack = new RequestStack();
         Tracy\Debugger::getBar()->addPanel(new TracyPanel($tempDir, $requestStack));
         $handler->push(function (callable $handler) use($requestStack) {
             return function ($request, array $options) use($handler, $requestStack) {
                 Tracy\Debugger::timer();
                 $guzzletteRequest = new Request();
                 $guzzletteRequest->request = $request;
                 return $handler($request, $options)->then(function ($response) use($requestStack, $guzzletteRequest) {
                     $guzzletteRequest->time = Tracy\Debugger::timer();
                     $guzzletteRequest->response = $response;
                     $requestStack->addRequest($guzzletteRequest);
                     return $response;
                 });
             };
         });
         $guzzleConfig['handler'] = $handler;
     }
     static::$client = new GuzzleHttp\Client($guzzleConfig);
     return static::$client;
 }
Exemplo n.º 6
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     Debugger::timer();
     $output->writeln('Running tests...');
     $runner = new Runner();
     $path = realpath($input->getArgument('path'));
     if ($path) {
         if (is_dir($path)) {
             $runner->setTestsRoot($path . '/');
             foreach (Finder::findFiles('*.php')->exclude('tester.php')->from($path) as $path => $fileInfo) {
                 $basePath = Helpers::stripExtension($path);
                 $runner->addTest($basePath);
             }
         } else {
             $basePath = Helpers::stripExtension($path);
             $runner->addTest($basePath);
         }
     } else {
         if ($path = realpath($input->getArgument('path') . '.php')) {
             $basePath = Helpers::stripExtension($path);
             $runner->addTest($basePath);
         } else {
             $output->writeln("<error>The given path isn't valid</error>");
             return 1;
         }
     }
     $runner->setOutput($output);
     $runner->runTests();
     $output->writeln('Completed in ' . round(Debugger::timer(), 2) . ' seconds');
     if ($runner->failed()) {
         return 1;
     } else {
         return 0;
     }
 }
Exemplo n.º 7
0
 protected function importDataToStagingTable($stagingTempTableName, $columns, $sourceData, array $options = [])
 {
     if (!isset($sourceData['schemaName'])) {
         throw new Exception('Invalid source data. schemaName must be set', Exception::INVALID_SOURCE_DATA);
     }
     if (!isset($sourceData['tableName'])) {
         throw new Exception('Invalid source data. schemaName must be set', Exception::INVALID_SOURCE_DATA);
     }
     $sourceColumnTypes = $this->describeTable(strtolower($sourceData['tableName']), strtolower($sourceData['schemaName']));
     $sql = "INSERT INTO " . $this->tableNameEscaped($stagingTempTableName) . " (" . implode(', ', array_map(function ($column) {
         return $this->quoteIdentifier($column);
     }, $columns)) . ") ";
     $sql .= "SELECT " . implode(',', array_map(function ($column) use($sourceColumnTypes) {
         if ($sourceColumnTypes[$column]['DATA_TYPE'] === 'bool') {
             return sprintf('DECODE(%s, true, 1, 0) ', $this->quoteIdentifier($column));
         } else {
             return "COALESCE(CAST({$this->quoteIdentifier($column)} as varchar), '') ";
         }
     }, $columns)) . " FROM " . $this->nameWithSchemaEscaped($sourceData['tableName'], $sourceData['schemaName']);
     try {
         Debugger::timer('copyToStaging');
         $this->query($sql);
         $this->addTimer('copyToStaging', Debugger::timer('copyToStaging'));
     } catch (\Exception $e) {
         if (strpos($e->getMessage(), 'Datatype mismatch') !== false) {
             throw new Exception($e->getMessage(), Exception::DATA_TYPE_MISMATCH, $e);
         }
         // everything is user error
         throw new Exception($e->getMessage(), Exception::UNKNOWN_ERROR, $e);
     }
 }
Exemplo n.º 8
0
 public function stopQuery()
 {
     if (Debugger::$productionMode) {
         return;
     }
     $keys = array_keys($this->queries);
     $key = end($keys);
     $this->queries[$key][2] = Debugger::timer('doctrine');
     $this->totalTime += $this->queries[$key][2];
 }
Exemplo n.º 9
0
 /**
  * subscribe.
  *
  * @method subscribe
  */
 public function subscribe()
 {
     $key = get_class($this);
     $timer = Debugger::timer($key);
     $this->laravel['events']->listen('*', function ($params) use($key) {
         $execTime = Debugger::timer($key);
         $firing = $this->laravel['events']->firing();
         $editorLink = self::editorLink(self::findSource());
         $this->totalTime += $execTime;
         $this->events[] = compact('execTime', 'firing', 'params', 'editorLink');
     });
 }
Exemplo n.º 10
0
 public function actionDefault()
 {
     $this->cronner->onTaskBegin[] = function (Cronner $cronner, Task $task) {
         echo '<h3>' . $task->getName() . '</h3>';
         Debugger::timer($task->getName());
     };
     $this->cronner->onTaskFinished[] = function (Cronner $cronner, Task $task) {
         echo '<p>Total time: ' . Debugger::timer($task->getName()) . ' s</p>';
     };
     $this->cronner->run();
     $this->terminate();
 }
Exemplo n.º 11
0
 public function call($type, $service, $params = [])
 {
     $url = $this->protocol . $this->host . "/?presenter={$type}&action={$service}";
     if (count($params) > 0) {
         foreach ($params as $name => $value) {
             $url .= "&{$name}={$value}";
         }
     }
     Debugger::timer();
     $data = $this->sendGet($url);
     $time = Debugger::timer();
     return ['data' => $data, 'url' => $url, 'time' => $time];
 }
Exemplo n.º 12
0
 public function subscribe(Dispatcher $event)
 {
     $key = get_class($this);
     $timer = Debugger::timer($key);
     $event->listen('*', function ($params) use($key, $event) {
         $execTime = Debugger::timer($key);
         // $dispatcher = static::findDispatcher();
         // $firing = array_get($dispatcher, 'dispatcher.args.0');
         $firing = $event->firing();
         $editorLink = Helper::getEditorLink(Helper::findSource());
         $this->attributes['totalTime'] += $execTime;
         $this->attributes['events'][] = compact('execTime', 'firing', 'params', 'editorLink');
     });
 }
Exemplo n.º 13
0
 protected function execute($command)
 {
     $time = $timerName = '';
     if (class_exists('\\Tracy\\Debugger')) {
         \Tracy\Debugger::timer($timerName = md5($command));
     }
     $this->logCmd($command, 'Executing');
     $output = shell_exec($command);
     if (class_exists('\\Tracy\\Debugger')) {
         $time = \Tracy\Debugger::timer($timerName);
     }
     $this->logCmd($output, 'Result', $time);
     return $output;
 }
Exemplo n.º 14
0
 public function subscribe()
 {
     $key = get_class($this);
     $timer = Debugger::timer($key);
     $event = $this->app['events'];
     $event->listen('*', function ($params) use($key, $event) {
         $execTime = Debugger::timer($key);
         $firing = $event->firing();
         $editorLink = Helper::getEditorLink(Helper::findSource());
         $this->attributes['count']++;
         $this->attributes['totalTime'] += $execTime;
         $this->attributes['logs'][] = compact('execTime', 'firing', 'params', 'editorLink');
     });
 }
Exemplo n.º 15
0
 /**
  * Stops the timer and logs event to syslog.
  *
  * @param string $name The timer name.
  * @return float The current timer value.
  */
 public static function stop($name, $data = [])
 {
     $point = Debugger::timer($name);
     $measure = self::add($point, $name);
     if (self::$profile) {
         $tags = [];
         if (isset($data['tags'])) {
             $tags = $data['tags'];
             unset($data['tags']);
         }
         if (function_exists('syslog')) {
             syslog(LOG_INFO, json_encode(['type' => self::$indexName, 'tags' => explode(' ', $name) + $tags, 'host' => gethostname(), 'dur' => round($measure * 1000, 1), 'mem' => memory_get_peak_usage(), 'php' => PHP_VERSION, 'timestamp' => (string) time(), 'data' => array_merge(self::$defaults, $data)]));
         }
     }
     return $measure;
 }
Exemplo n.º 16
0
 protected function importTable($tableName, CsvFile $csvFile)
 {
     if ($csvFile->getEnclosure() && $csvFile->getEscapedBy()) {
         throw new Exception('Invalid CSV params. Either enclosure or escapedBy must be specified for Snowflake backend but not both.', Exception::INVALID_CSV_PARAMS, null);
     }
     try {
         $timerName = 'copyToStaging-' . $csvFile->getBasename();
         Debugger::timer($timerName);
         $results = $this->connection->fetchAll($this->generateCopyCommand($tableName, $csvFile));
         foreach ($results as $result) {
             $this->importedRowsCount += (int) $result['rows_loaded'];
         }
         $this->addTimer($timerName, Debugger::timer($timerName));
     } catch (\Exception $e) {
         throw new Exception('Load error: ' . $e->getMessage(), Exception::INVALID_SOURCE_DATA, $e);
     }
 }
Exemplo n.º 17
0
 public function process($html)
 {
     if (!$this->enabled) {
         return $html;
     }
     if ($this->addTimeMark) {
         Debugger::timer('tidy');
     }
     $this->parseString($html, $this->config, 'utf8');
     if ($this->runCleanRepair) {
         $this->cleanRepair();
     }
     $output = tidy_get_output($this);
     if ($this->addTimeMark) {
         $elapsed = Debugger::timer('tidy');
         $output .= "\n\n<!-- Tidy formatting took: " . number_format($elapsed * 1000, 2) . " ms -->";
     }
     return $output;
 }
Exemplo n.º 18
0
 protected function insertOrUpdateTargetTable($sourceTable, $targetTable, $importColumns)
 {
     Debugger::timer('csvImport.insertIntoTargetTable');
     $connection = $this->connection;
     $columnsListEscaped = function ($columns, $prefix = null) use($connection) {
         return implode(', ', array_map(function ($columnName) use($connection, $prefix) {
             return ($prefix ? $prefix . '.' : '') . $this->quoteIdentifier($columnName);
         }, $columns));
     };
     $sql = 'INSERT INTO ' . $this->quoteIdentifier($targetTable) . ' (';
     $sql .= $columnsListEscaped($importColumns);
     $sql .= ') ';
     $sql .= 'SELECT ' . $columnsListEscaped($importColumns, 't') . ' FROM ' . $this->quoteIdentifier($sourceTable) . ' t ';
     $sql .= 'ON DUPLICATE KEY UPDATE ';
     $sql .= implode(', ', array_map(function ($columnName) use($connection) {
         return $this->quoteIdentifier($columnName) . ' = t.' . $this->quoteIdentifier($columnName);
     }, $importColumns));
     $this->query($sql);
     $this->addTimer('insertIntoTargetTable', Debugger::timer('csvImport.insertIntoTargetTable'));
 }
Exemplo n.º 19
0
 public function end()
 {
     $time = Debugger::timer(self::TIMER_NAME);
     if ($query = end($this->queries)) {
         $query->time = $time;
     }
     $this->totalTime += $time;
 }
<?php

/** @var \Nette\DI\Container $container */
$container = (require_once __DIR__ . '/../app/bootstrap.php');
$emailQueue = $container->getByType(\App\EmailQueue::class);
\Tracy\Debugger::timer('messages');
for ($i = 0; $i < 1000000; $i++) {
    $emailQueue->publish(sprintf('*****@*****.**', $i), '*****@*****.**', "AHOOOJ");
}
print_r(['time' => \Tracy\Debugger::timer('messages')]);
Exemplo n.º 21
0
 /**
  * @param null $name
  */
 function time_bench($name = NULL)
 {
     Debugger::timer($name);
 }
Exemplo n.º 22
0
 /**
  * @param $ic
  * @return bool|\stdClass
  */
 function aresIc($ic)
 {
     if (!$this->ic($ic)) {
         return FALSE;
     }
     if (!is_null($address = $this->getCacheIc($ic))) {
         return $address;
     }
     $parser = new \Edge\Ares\Parser\AddressParser();
     $provider = new \Edge\Ares\Provider\HttpProvider();
     $ares = new \Edge\Ares\Ares($parser, $provider);
     try {
         Debugger::timer('ares-curl');
         /** @var \Edge\Ares\Ares $ares */
         /** @var \Edge\Ares\Container\Address $address */
         $address = $ares->fetchSubjectAddress($ic);
         $out = (object) ["ico" => $address->getIco(), "dic" => $address->getDic(), "firma" => $address->getFirma(), "ulice" => $address->getUlice(), "cisloOrientacni" => $address->getCisloOrientacni(), "cisloPopisne" => $address->getCisloPopisne(), "mesto" => $address->getMesto(), "castObce" => $address->getCastObce(), "psc" => $address->getPsc()];
         $this->onAres($this, ["time" => Debugger::timer('ares-curl'), "ic" => $ic, "data" => $out]);
         $this->setCacheIc($ic, $out);
     } catch (\Edge\Ares\Exception\ExceptionInterface $e) {
         // Do some error handling here.
         $this->onAres($this, ["time" => Debugger::timer('ares-curl'), "ic" => $ic, "exception" => $e]);
         return FALSE;
     }
     return $out;
 }
Exemplo n.º 23
0
 public function stopQueryTimer($index)
 {
     if (isset($this->queries[$index])) {
         $time = Debugger::timer(__CLASS__ . ":{$index}");
         $this->total_time += $time;
         $this->queries[$index][2] = $time;
     }
 }
Exemplo n.º 24
0
 /**
  * Performs merge operation according to http://docs.aws.amazon.com/redshift/latest/dg/merge-specify-a-column-list.html
  * @param $stagingTableName
  * @param $targetTableName
  * @param $columns
  */
 private function insertOrUpdateTargetTable($stagingTableName, $targetTableName, $columns, $useTimestamp = true)
 {
     $this->connection->query('BEGIN TRANSACTION');
     $nowFormatted = $this->getNowFormatted();
     $targetTableNameWithSchema = $this->nameWithSchemaEscaped($targetTableName);
     $stagingTableNameWithSchema = $this->nameWithSchemaEscaped($stagingTableName);
     $primaryKey = $this->connection->getTablePrimaryKey($this->schemaName, $targetTableName);
     if (!empty($primaryKey)) {
         // Update target table
         $sql = "UPDATE " . $targetTableNameWithSchema . " AS \"dest\" SET ";
         $columnsSet = [];
         foreach ($columns as $columnName) {
             $columnsSet[] = sprintf('%s = COALESCE("src".%s, \'\')', $this->quoteIdentifier($columnName), $this->quoteIdentifier($columnName));
         }
         $sql .= implode(', ', $columnsSet);
         if ($useTimestamp) {
             $sql .= ", " . $this->quoteIdentifier(self::TIMESTAMP_COLUMN_NAME) . " = '{$nowFormatted}' ";
         }
         $sql .= " FROM " . $stagingTableNameWithSchema . ' AS "src" ';
         $sql .= " WHERE ";
         $pkWhereSql = [];
         foreach ($primaryKey as $pkColumn) {
             $pkWhereSql[] = sprintf('"dest".%s = "src".%s', $this->quoteIdentifier($pkColumn), $this->quoteIdentifier($pkColumn));
         }
         $sql .= implode(' AND ', $pkWhereSql) . " ";
         // update only changed rows - mysql TIMESTAMP ON UPDATE behaviour simulation
         $columnsComparsionSql = array_map(function ($columnName) {
             return sprintf('"dest".%s != COALESCE("src".%s, \'\')', $this->quoteIdentifier($columnName), $this->quoteIdentifier($columnName));
         }, $columns);
         $sql .= " AND (" . implode(' OR ', $columnsComparsionSql) . ") ";
         Debugger::timer('updateTargetTable');
         $this->connection->query($sql);
         $this->addTimer('updateTargetTable', Debugger::timer('updateTargetTable'));
         // Delete updated rows from staging table
         $sql = "DELETE FROM " . $stagingTableNameWithSchema . ' "src" ';
         $sql .= "USING " . $targetTableNameWithSchema . ' AS "dest" ';
         $sql .= "WHERE " . implode(' AND ', $pkWhereSql);
         Debugger::timer('deleteUpdatedRowsFromStaging');
         $this->connection->query($sql);
         $this->addTimer('deleteUpdatedRowsFromStaging', Debugger::timer('deleteUpdatedRowsFromStaging'));
         // Dedup staging table
         Debugger::timer('dedupStaging');
         $this->dedupe($stagingTableName, $columns, $primaryKey);
         $this->addTimer('dedupStaging', Debugger::timer('dedupStaging'));
     }
     // Insert from staging to target table
     $insColumns = $useTimestamp ? array_merge($columns, [self::TIMESTAMP_COLUMN_NAME]) : $columns;
     $sql = "INSERT INTO " . $targetTableNameWithSchema . ' (' . implode(', ', array_map(function ($column) {
         return $this->quoteIdentifier($column);
     }, $insColumns)) . ")";
     $columnsSetSql = [];
     foreach ($columns as $columnName) {
         $columnsSetSql[] = sprintf('COALESCE("src".%s, \'\')', $this->quoteIdentifier($columnName));
     }
     $sql .= " SELECT " . implode(',', $columnsSetSql);
     if ($useTimestamp) {
         $sql .= ", '{$nowFormatted}' ";
     }
     $sql .= "FROM " . $stagingTableNameWithSchema . ' AS "src"';
     Debugger::timer('insertIntoTargetFromStaging');
     $this->connection->query($sql);
     $this->addTimer('insertIntoTargetFromStaging', Debugger::timer('insertIntoTargetFromStaging'));
     $this->connection->query('COMMIT');
 }
Exemplo n.º 25
0
 /**
  * @param null $name
  */
 function time_bench($name = null)
 {
     Debugger::timer($name);
 }
Exemplo n.º 26
0
 /**
  * @return array
  */
 public function stopQuery()
 {
     $keys = array_keys($this->queries);
     $key = end($keys);
     $this->queries[$key][2] = $time = Debugger::timer('doctrine');
     $this->totalTime += $time;
     return $this->queries[$key] + array_fill_keys(range(0, 4), NULL);
 }
Exemplo n.º 27
0
 /**
  * Log a message.
  *
  * @param string $message
  */
 public function log($message)
 {
     if (TracyDebugger::$logDirectory) {
         TracyDebugger::log(sprintf($message, TracyDebugger::timer() * 1000));
     }
 }
Exemplo n.º 28
0
 /**
  * Performs merge operation according to http://docs.aws.amazon.com/redshift/latest/dg/merge-specify-a-column-list.html
  * @param $stagingTempTableName
  * @param $targetTableName
  * @param $columns
  * @param $useTimestamp
  */
 private function insertOrUpdateTargetTable($stagingTempTableName, $targetTableName, array $primaryKey, $columns, $useTimestamp = true)
 {
     $this->connection->beginTransaction();
     $nowFormatted = $this->getNowFormatted();
     $targetTableNameWithSchema = $this->nameWithSchemaEscaped($targetTableName);
     $stagingTableNameEscaped = $this->tableNameEscaped($stagingTempTableName);
     if (!empty($primaryKey)) {
         // Update target table
         $sql = "UPDATE " . $targetTableNameWithSchema . " SET ";
         $columnsSet = [];
         foreach ($columns as $columnName) {
             $columnsSet[] = sprintf("%s = %s.%s", $this->quoteIdentifier($columnName), $stagingTableNameEscaped, $this->quoteIdentifier($columnName));
         }
         $sql .= implode(', ', $columnsSet);
         if ($useTimestamp) {
             $sql .= ", _timestamp = '{$nowFormatted}' ";
         }
         $sql .= " FROM " . $stagingTableNameEscaped . " ";
         $sql .= " WHERE ";
         $pkWhereSql = [];
         foreach ($primaryKey as $pkColumn) {
             $pkWhereSql[] = sprintf("%s.%s = %s.%s", $targetTableNameWithSchema, $this->quoteIdentifier($pkColumn), $stagingTableNameEscaped, $this->quoteIdentifier($pkColumn));
         }
         $sql .= implode(' AND ', $pkWhereSql) . " ";
         // update only changed rows - mysql TIMESTAMP ON UPDATE behaviour simulation
         $columnsComparsionSql = array_map(function ($columnName) use($targetTableNameWithSchema, $stagingTableNameEscaped) {
             return sprintf("%s.%s != %s.%s", $targetTableNameWithSchema, $this->quoteIdentifier($columnName), $stagingTableNameEscaped, $this->quoteIdentifier($columnName));
         }, $columns);
         $sql .= " AND (" . implode(' OR ', $columnsComparsionSql) . ") ";
         Debugger::timer('updateTargetTable');
         $this->query($sql);
         $this->addTimer('updateTargetTable', Debugger::timer('updateTargetTable'));
         // Delete updated rows from staging table
         $sql = "DELETE FROM " . $stagingTableNameEscaped . " ";
         $sql .= "USING " . $targetTableNameWithSchema . " ";
         $sql .= "WHERE " . implode(' AND ', $pkWhereSql);
         Debugger::timer('deleteUpdatedRowsFromStaging');
         $this->query($sql);
         $this->addTimer('deleteUpdatedRowsFromStaging', Debugger::timer('deleteUpdatedRowsFromStaging'));
         // Dedup staging table
         Debugger::timer('dedupStaging');
         $this->dedup($stagingTempTableName, $columns, $primaryKey);
         $this->addTimer('dedupStaging', Debugger::timer('dedupStaging'));
     }
     // Insert from staging to target table
     $sql = "INSERT INTO " . $targetTableNameWithSchema . " (" . implode(', ', array_map(function ($column) {
         return $this->quoteIdentifier($column);
     }, $columns));
     $sql .= $useTimestamp ? ", _timestamp) " : ")";
     $columnsSetSql = [];
     foreach ($columns as $columnName) {
         $columnsSetSql[] = sprintf("%s.%s", $stagingTableNameEscaped, $this->quoteIdentifier($columnName));
     }
     $sql .= "SELECT " . implode(',', $columnsSetSql);
     $sql .= $useTimestamp ? ", '{$nowFormatted}' " : "";
     $sql .= "FROM " . $stagingTableNameEscaped;
     Debugger::timer('insertIntoTargetFromStaging');
     $this->query($sql);
     $this->addTimer('insertIntoTargetFromStaging', Debugger::timer('insertIntoTargetFromStaging'));
     $this->connection->commit();
 }
Exemplo n.º 29
0
 /**
  * Sets up the environment
  *   
  * @return void
  */
 static function setup()
 {
     if (!static::$set) {
         assert_options(ASSERT_ACTIVE, 1);
         assert_options(ASSERT_QUIET_EVAL, 1);
         assert_options(ASSERT_WARNING, 0);
         register_shutdown_function(function () {
             $time = \Tracy\Debugger::timer(static::NAME);
             static::printLine("");
             static::printLine("Total run time: {$time} second(s)");
         });
         \Tracy\Debugger::timer(static::NAME);
         static::$mode = PHP_SAPI == "cli" ? "cli" : "http";
         static::$set = true;
     } else {
         static::printLine("Warning: Testing Environment was already set up.");
     }
 }
Exemplo n.º 30
0
 private function generateEntities($entity, $count)
 {
     $entities = [];
     Debugger::timer();
     for ($i = 0; $i < $count; $i++) {
         $entities[] = $this->generateEntity($entity);
     }
     WP_CLI::success("Generating ({$entity}): " . Debugger::timer());
     $insertQueries = $this->buildInsertQueries($this->database->prefix . $entity, $entities);
     WP_CLI::success("Building queries ({$entity}): " . Debugger::timer());
     $connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
     $connection->query("SET GLOBAL max_allowed_packet=100*1024*1024");
     $chunks = array_chunk($insertQueries, 50);
     foreach ($chunks as $chunk) {
         $connection->multi_query(join(" ", $chunk));
         while ($connection->next_result()) {
             // flush multi_queries
             if (!$connection->more_results()) {
                 break;
             }
         }
     }
     WP_CLI::success("Queries ({$entity}): " . Debugger::timer());
     $connection->close();
 }