protected function execute(InputInterface $input, OutputInterface $output) { $stopwatch = new \Symfony\Component\Stopwatch\Stopwatch(); $stopwatch->start('DbClean'); $pdo = $this->getService('sugarcrm.pdo'); $this->getService('sugarcrm.entrypoint'); // go to sugar folder to make sure we are in the right folder if ($input->getOption('remove-deleted') === false && $input->getOption('clean-cstm') === false && $input->getOption('clean-history') === false && $input->getOption('clean-activities') === false) { $msg = 'You need to set at least --remove-deleted or --clean-xxxxxx'; throw new \InvalidArgumentException($msg); } // Make sure that we don't anonymize production if ($this->askConfirmation($input, $output) === false) { return; } $tables = $input->getOption('table'); if (empty($tables)) { $tables = $this->getNonAuditAndCacheTables($pdo); } foreach ($tables as $table) { if ($this->tableExists($pdo, $table) === false) { $output->writeln("<error>{$table} does not exist</error>"); continue; } $this->cleanDeletedAndCustomTable($input, $output, $pdo, $table); } $tables = $input->getOption('table'); $auditJobQueueAndTrackerTables = $this->getAuditJobQueueAndTrackersTables($input, $pdo); foreach ($auditJobQueueAndTrackerTables as $table) { if (!empty($tables) && in_array($table, $tables) || empty($tables)) { $pdo->query("TRUNCATE TABLE `{$table}`"); $output->writeln("<info>Removed everything from {$table}</info>"); } } $tables = $input->getOption('table'); $activitiesTables = $this->getActivitiesTables($input, $pdo); foreach ($activitiesTables as $table) { if (!empty($tables) && in_array($table, $tables) || empty($tables)) { $pdo->query("TRUNCATE TABLE `{$table}`"); $output->writeln("<info>Removed everything from {$table}</info>"); } } // Get memory and execution time information $event = $stopwatch->stop('DbClean'); $memory = round($event->getMemory() / 1024 / 1024, 2); $time = round($event->getDuration() / 1000, 2); $time = $time > 180 ? round($time / 60, 2) . 'mins' : "{$time} sec"; // Final message $output->writeln(PHP_EOL . "<comment>Done in {$time} (consuming {$memory}Mb)</comment>"); }
<?php require __DIR__ . '/../../vendor/autoload.php'; $userAgents = array('Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0 IceDragon/20.0.1.14'); shuffle($userAgents); $stopwatch = new \Symfony\Component\Stopwatch\Stopwatch(); $stopwatch->start('benchmark'); $factory = new \DeviceDetectorIO\DeviceDetector\Factory\DeviceUserAgentFactory(); foreach ($userAgents as $userAgent) { dump($factory->getDevice($userAgent)); } $event = $stopwatch->stop('benchmark'); printf("Benchmark results for 1 useragents:\n"); printf("Duration %s milliseconds.\n", $event->getDuration()); printf("Memory %s bytes.\n", $event->getMemory());
function run() { $config = $this->config; $context = $this->context; /* @var $context ExecutionContext */ $write = function ($s) { echo $s; }; $writeln = function ($s) { $d = new \DateTime(); echo $d->format(\DateTime::ISO8601) . ' ' . $s . "\n"; }; $c = $color = new \Colors\Color(); /* @var $c \Colors\Color */ $writeln($c('Running agent `' . $this->name)->bold() . '`'); // generate an array with handlers $writeln('Loading handlers'); $handlers = []; $handlersDir = $config->handlers_dir; if ($handlersDir) { $finder = new \Symfony\Component\Finder\Finder(); foreach ($finder->in($handlersDir)->name('*.php')->sortByName() as $file) { /* @var $file \SplFileInfo */ $handlerId = $file->getBasename('.php'); $handler = (include $file); $handlers[$handlerId] = $handler; $writeln(' setting up handler ' . $color($handlerId)->bold() . $color->yellow(' (in ' . $file . ')')); $eventName = SqsEvents::generateEventForAgentId(SqsEvents::EVENT_SQSAGENT_SQSMESSAGERECEIVED, $this->getId()); $context->getEventDispatcher()->addListener($eventName, function (\T24\Event\SqsMessageReceivedEvent $event) use($write, $writeln, $color, $handler, $handlerId, $eventName) { $writeln($color->bg('blue', $color->white('invoking handler ' . $handlerId . ' for event ' . $eventName))); /* @var Callable $handler */ $result = $handler($event); // write comments $comments = $event->getComments(); foreach ($comments as $comment) { $writeln($color(' comment: ')->yellow() . $comment); } $writeln(' - handler ' . $handlerId . ' had been invoked.'); if ($event->isPropagationStopped()) { $writeln(' - ' . $color->red('the event was stopped from further propagation.')); } else { $writeln(' - the event was not stopped. proceed to next handler'); } return $result; }); } $writeln('Loaded ' . count($handlers) . ' handlers.'); } else { $writeln('default handlers dir not specified, no handlers defined..'); } // @todo: get aws params. $region = ''; $sqsQueueUrl = ''; $sqs = SqsClient::factory(['region' => $config->aws_region, 'version' => '2012-11-05', 'credentials' => ['key' => $config->aws_key, 'secret' => $config->aws_secret]]); // start the process $ttl = (int) $config->ttl; $ttl = min($ttl, 300); $ttl = max($ttl, 5); $sleep = (int) $config->sleep; $sleep = min($sleep, $ttl - 5); $sleep = max($sleep, 0); $stopwatch = new \Symfony\Component\Stopwatch\Stopwatch(); $b = $stopwatch->start('loop'); while ($b->getDuration() < $ttl * 1000) { // get an sqs message from the queue $writeln($c('Polling Sqs for messages')); $result = $sqs->receiveMessage(['QueueUrl' => $config->sqs_queue_url, 'AttributeNames' => ['All'], 'MessageAttributeNames' => ['All'], 'MaxNumberOfMessages' => 1]); /* @var $result \Aws\Result */ if (!$result['Messages'] || $result['Messages'] == 0) { $writeln($c('no messages found in sqs queue')); } else { $writeln($c(sprintf('%d message(s) retrieved from queue.', count($result['Messages'])))); foreach ($result['Messages'] as $message) { // set up an event that the message is received. // the default subscriber will pass the event to the event handlers in the handlers dir $event = new SqsMessageReceivedEvent($context, $message); $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_SQSMESSAGERECEIVED, $event); $context->getEventDispatcher()->dispatch(SqsEvents::generateEventForAgentId(SqsEvents::EVENT_SQSAGENT_SQSMESSAGERECEIVED, $this->getId()), $event); /* $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_SQSMESSAGEHANDLED, $event2); if ($event->isProcessed()) { $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_SQSMESSAGEPROCESSED, $event2); } */ if ($event->getRemoveFromQueue()) { $writeln(' - the event handlers indicate the sqs message should be removed from the queue.'); // remove from sqs $sqsMessage = $event->getSqsMessage(); $removed = $sqs->deleteMessage(['QueueUrl' => $config->sqs_queue_url, 'ReceiptHandle' => $sqsMessage['ReceiptHandle']]); $writeln(' - ' . ($removed ? $c->green('succesfully removed the message from SQS') : $c->red('failed to remove the message from SQS'))); } else { $writeln(' - the event handlers indicate the sqs messageshould not be removed from the queue.'); } } } $writeln($color->cyan('sleeping for ' . $sleep . ' seconds')); sleep($sleep); } // end stopwatch while loop $writeln('Stopped after ' . (int) ($b->getDuration() / 1000) . ' secs.'); // send the finish event. $event = new \T24\Event\AgentEvent($context); $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_FINISH, $event); $context->getEventDispatcher()->dispatch(SqsEvents::generateEventForAgentId(SqsEvents::EVENT_SQSAGENT_FINISH, $this->getId()), $event); }
<?php require_once __DIR__ . '/vendor/autoload.php'; $watch = new \Symfony\Component\Stopwatch\Stopwatch(); define('ITERATIONS', 10000); $results = []; /** * Using pecl-uuid by itself */ $watch->start('pecl'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = uuid_create(UUID_TYPE_TIME); } $results['pecl'] = $watch->stop('pecl'); /** * Using the older Rhumsaa\Uuid version of the library */ $watch->start('rhumsaa'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = (string) \Rhumsaa\Uuid\Uuid::uuid1(); } $results['rhumsaa'] = $watch->stop('rhumsaa'); /** * Using Ramsey\Uuid with default time generator */ $watch->start('ramsey-default'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = (string) \Ramsey\Uuid\Uuid::uuid1(); } $results['ramsey-default'] = $watch->stop('ramsey-default'); /**
protected function execute(InputInterface $input, OutputInterface $output) { $stopwatch = new \Symfony\Component\Stopwatch\Stopwatch(); $stopwatch->start('Anon'); $pdo = $this->getService('sugarcrm.pdo'); $this->getService('sugarcrm.entrypoint'); // go to sugar folder to make sure we are in the right folder // Make sure that we don't anonymize production if ($input->getOption('force') === true && $this->askConfirmation($input, $output) === false) { return; } // Anon READER $reader = new \Inet\Neuralyzer\Configuration\Reader($input->getOption('file')); // Now work on the DB $anon = new \Inet\Neuralyzer\Anonymizer\DB($pdo); $anon->setConfiguration($reader); // Get tables $tables = $input->getOption('table'); if (empty($tables)) { $tables = $reader->getEntities(); } $pretend = $input->getOption('force') === true ? false : true; foreach ($tables as $table) { // Do I need to clean the table ? $this->cleanTable($input, $output, $pdo, $table); // Start to work $result = $pdo->query($sql = "SELECT COUNT(1) FROM {$table}"); if ($result === false) { throw new \PDOException("Error in SQL: {$sql}, are you sure {$table} exists ?"); } $data = $result->fetchAll(\PDO::FETCH_COLUMN); $total = (int) $data[0]; if ($total === 0) { $output->writeln("<info>{$table} is empty</info>" . PHP_EOL); continue; } $bar = new ProgressBar($output, $total); $bar->setRedrawFrequency($total > 100 ? 100 : 0); $output->writeln("<info>Anonymizing {$table}</info>"); $queries = $anon->processEntity($table, function () use($bar) { $bar->advance(); }, $pretend, $input->getOption('sql')); $output->writeln(PHP_EOL); if ($input->getOption('sql')) { $output->writeln('<comment>Queries:</comment>'); $output->writeln(implode(PHP_EOL, $queries)); $output->writeln(PHP_EOL); } } $this->cleanAuditAndTrackers($output, $pdo); // Get memory and execution time information $event = $stopwatch->stop('Anon'); $memory = round($event->getMemory() / 1024 / 1024, 2); $time = round($event->getDuration() / 1000, 2); $time = $time > 180 ? round($time / 60, 2) . 'mins' : "{$time} sec"; // Final message $output->writeln(PHP_EOL . "<comment>Done in {$time} (consuming {$memory}Mb)</comment>"); $db = $this->getDb($pdo); if ($input->getOption('force') === true) { $output->writeln(PHP_EOL . '<comment>To export the db run: </comment>'); $output->writeln(" mysqldump --skip-lock-tables {$db} | bzip2 > {$db}." . date('Ymd-Hi') . '.sql.bz2'); } else { $output->writeln(PHP_EOL . "<error>The anonymization didn't run. Use --force to run it.</error>"); } }
<?php require_once __DIR__ . '/vendor/autoload.php'; $watch = new \Symfony\Component\Stopwatch\Stopwatch(); define('ITERATIONS', 10000); $results = []; $watch->start('pecl'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = uuid_create(UUID_TYPE_RANDOM); } $results['pecl'] = $watch->stop('pecl'); $watch->start('rhumsaa'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = (string) \Rhumsaa\Uuid\Uuid::uuid4(); } $results['rhumsaa'] = $watch->stop('rhumsaa'); $watch->start('ramsey-pecl'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = (string) \Ramsey\Uuid\Uuid::uuid4(); } $results['ramsey-pecl'] = $watch->stop('ramsey-pecl'); $watch->start('ramsey-nopecl'); for ($i = 0; $i < ITERATIONS; ++$i) { \Ramsey\Uuid\Uuid::setFactory(new \Ramsey\Uuid\UuidFactory()); $x = (string) \Ramsey\Uuid\Uuid::uuid4(); } $results['ramsey-nopecl'] = $watch->stop('ramsey-nopecl'); $watch->start('ramsey-randomlib'); for ($i = 0; $i < ITERATIONS; ++$i) { $uuidFactory = new \Ramsey\Uuid\UuidFactory(); // Using low-strength generator by default
<?php require_once __DIR__ . '/vendor/autoload.php'; use GraphAware\Reco4PHP\Demo\Github\RecommendationEngine; use GraphAware\Reco4PHP\RecommenderService; $rs = RecommenderService::create("http://localhost:7474"); $rs->registerRecommendationEngine(new RecommendationEngine()); $stopwatch = new \Symfony\Component\Stopwatch\Stopwatch(); $input = $rs->findInputBy('User', 'login', 'jakzal'); $engine = $rs->getRecommender("github_who_to_follow"); $stopwatch->start('reco'); $recommendations = $engine->recommend($input); $e = $stopwatch->stop('reco'); //echo $recommendations->size() . ' found in ' . $e->getDuration() . 'ms' .PHP_EOL; foreach ($recommendations->getItems(10) as $reco) { echo $reco->item()->get('login') . PHP_EOL; echo $reco->totalScore() . PHP_EOL; foreach ($reco->getScores() as $name => $score) { echo "\t" . $name . ':' . $score->score() . PHP_EOL; } }
<?php require_once __DIR__ . '/vendor/autoload.php'; $watch = new \Symfony\Component\Stopwatch\Stopwatch(); define('ITERATIONS', 10000); $results = []; /** * Using pecl-uuid by itself */ $watch->start('pecl'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = uuid_create(UUID_TYPE_RANDOM); } $results['pecl'] = $watch->stop('pecl'); /** * Using the older Rhumsaa\Uuid version of the library */ $watch->start('rhumsaa-openssl'); for ($i = 0; $i < ITERATIONS; ++$i) { $x = (string) \Rhumsaa\Uuid\Uuid::uuid4(); } $results['rhumsaa-openssl'] = $watch->stop('rhumsaa-openssl'); /** * Using the older Rhumsaa\Uuid version of the library without OpenSSL */ $watch->start('rhumsaa-mtrand'); \Rhumsaa\Uuid\Uuid::$forceNoOpensslRandomPseudoBytes = true; for ($i = 0; $i < ITERATIONS; ++$i) { $x = (string) \Rhumsaa\Uuid\Uuid::uuid4(); } $results['rhumsaa-mtrand'] = $watch->stop('rhumsaa-mtrand');