<?php /** * Created by PhpStorm. * User: Jiang Yu * Date: 2015/07/01 * Time: 12:26 PM */ require __DIR__ . '/../bootstrap.php'; $worker = new \Worker\CurlWorker(); $worker->addTasks([]); $worker->run();
function parseSnsid($url) { $arr = explode('/', parse_url($url, PHP_URL_PATH)); return (string) $arr[3]; } $snsidList = ['100001349218797', '675097095878591', '675097095878592', '675097095878593', '675097095878594']; $taskFactory = new \Worker\Model\TaskFactory(); $tasks = array_map(function ($snsid) use($taskFactory) { $url = sprintf('http://52.19.73.190:9200/farm/user:tw/%s/_update', $snsid); $postData = sprintf('{"doc":{"status":"%d"}}', time()); return $taskFactory->create($url, [CURLOPT_URL => $url, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $postData, CURLOPT_RETURNTRANSFER => 1]); }, $snsidList); //dump($tasks); PHP_Timer::start(); $curlWorker = new \Worker\CurlWorker(new \Worker\Queue\HttpTracer()); $curlWorker->addTasks($tasks); $responseList = []; $trace = $curlWorker->run($responseList, function () { echo microtime(true) . ' got response' . PHP_EOL; }); $taskWallTime = PHP_Timer::stop(); array_walk($responseList, function (array $response) { dump(['url' => $response['url'], 'http_code' => $response['http_code'], 'content' => $response['content']]); }); $httpCost = 0; array_walk($trace, function (\Worker\Queue\HttpTracer $httpTracer, $url) use(&$httpCost) { $httpCost += $httpTracer->getElapsedTime(); dump(parseSnsid($url) . ' => ' . $httpTracer); }); dump('time cost on http: ' . PHP_Timer::secondsToTimeString($httpCost)); dump('wall time on http: ' . PHP_Timer::secondsToTimeString($taskWallTime));
protected function onBatch(array $batch) { $tasks = $this->taskBatchFactory($batch); PHP_Timer::start(); $curlWorker = new \Worker\CurlWorker(new \Worker\Queue\HttpTracer()); $curlWorker->addTasks($tasks, $this->concurrency); $responseList = []; $trace = $curlWorker->run($responseList, function () { printf("%10d/%d\r", ++$this->processed, $this->total); }); $this->taskWallTime += PHP_Timer::stop(); array_walk($trace, function (\Worker\Queue\HttpTracer $httpTracer) { $this->httpCost += $httpTracer->getElapsedTime(); }); return array_map(function (array $response) { return (int) $response['http_code']; }, $responseList); }
* User: Jiang Yu * Date: 2015/06/30 * Time: 3:37 PM. */ use Worker\EndlessTasks; require __DIR__ . '/../bootstrap.php'; $fireNotifications = function ($jsonArray) { $requestFactory = new \Worker\Model\TaskFactory(); $tasks = []; foreach ($jsonArray as $jsonString) { $options = json_decode($jsonString, true); if (!is_array($options)) { continue; } $tasks[] = $requestFactory->create($options[CURLOPT_URL], $options); } PHP_Timer::start(); $worker = new \Worker\CurlWorker(); $worker->addTasks($tasks); $worker->run(); $delta = PHP_Timer::stop(); echo PHP_Timer::resourceUsage() . ' fire notif cost: ' . PHP_Timer::secondsToTimeString($delta) . PHP_EOL; }; $facebookOptions = \Application\Facade::getInstance()->getFBGatewayOptions(); dump($facebookOptions); $taskGenerator = new EndlessTasks($facebookOptions['queueLocation']); $bufferedNotifications = $taskGenerator->get(); /** @var string[] $tasks */ foreach ($bufferedNotifications as $tasks) { call_user_func($fireNotifications, $tasks); }