Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a sequencer for api calls to see which calls have latency and …

…how asynchronous the calls are.
  • Loading branch information...
commit b69ce312fdbccbb1f1dab7f0ec77cb59536f6c98 1 parent 6e07160
@jmathai authored
Showing with 113 additions and 5 deletions.
  1. +31 −5 EpiCurl.php
  2. +54 −0 EpiSequence.php
  3. +28 −0 tests/sequencerTest.php
View
36 EpiCurl.php
@@ -13,6 +13,7 @@ class EpiCurl
private $requests = array();
private $responses = array();
private $properties = array();
+ private static $timers = array();
function __construct()
{
@@ -38,6 +39,7 @@ public function addCurl($ch)
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));
$code = curl_multi_add_handle($this->mc, $ch);
+ $this->startTimer($key);
// (1)
if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM)
@@ -67,14 +69,14 @@ public function getResult($key = null)
while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM))
{
usleep($outerSleepInt);
- $outerSleepInt = max(1, ($outerSleepInt*$this->sleepIncrement));
+ $outerSleepInt = intval(max(1, ($outerSleepInt*$this->sleepIncrement)));
$ms=curl_multi_select($this->mc, 0);
if($ms > 0)
{
do{
$this->execStatus = curl_multi_exec($this->mc, $this->running);
usleep($innerSleepInt);
- $innerSleepInt = max(1, ($innerSleepInt*$this->sleepIncrement));
+ $innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement)));
}while($this->execStatus==CURLM_CALL_MULTI_PERFORM);
$innerSleepInt = 1;
}
@@ -90,6 +92,16 @@ public function getResult($key = null)
return false;
}
+ public static function getSequence()
+ {
+ return new EpiSequence(self::$timers);
+ }
+
+ public static function getTimers()
+ {
+ return self::$timers;
+ }
+
private function getKey($ch)
{
return (string)$ch;
@@ -113,6 +125,7 @@ private function storeResponses()
while($done = curl_multi_info_read($this->mc))
{
$key = (string)$done['handle'];
+ $this->stopTimer($key, $done);
$this->responses[$key]['data'] = curl_multi_getcontent($done['handle']);
foreach($this->properties as $name => $const)
{
@@ -123,6 +136,19 @@ private function storeResponses()
}
}
+ private function startTimer($key)
+ {
+ self::$timers[$key]['start'] = microtime(true);
+ }
+
+ private function stopTimer($key, $done)
+ {
+ self::$timers[$key]['end'] = microtime(true);
+ self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL);
+ self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME);
+ self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE);
+ }
+
static function getInstance()
{
if(self::$inst == null)
@@ -140,19 +166,19 @@ class EpiCurlManager
private $key;
private $epiCurl;
- function __construct($key)
+ public function __construct($key)
{
$this->key = $key;
$this->epiCurl = EpiCurl::getInstance();
}
- function __get($name)
+ public function __get($name)
{
$responses = $this->epiCurl->getResult($this->key);
return isset($responses[$name]) ? $responses[$name] : null;
}
- function __isset($name)
+ public function __isset($name)
{
$val = self::__get($name);
return empty($val);
View
54 EpiSequence.php
@@ -0,0 +1,54 @@
+<?php
+class EpiSequence
+{
+ private $timers, $min, $max, $width = 100;
+ public function __construct($timers)
+ {
+ $this->timers = $timers;
+
+ $min = PHP_INT_MAX;
+ $max = 0;
+ foreach($this->timers as $timer)
+ {
+ $min = min($timer['start'], $min);
+ $max = max($timer['end'], $max);
+ }
+ $this->min = $min;
+ $this->max = $max;
+ $this->range = $max-$min;
+ $this->step = floatval($this->range/$this->width);
+ }
+
+ public function renderAscii()
+ {
+ $tpl = '';
+ foreach($this->timers as $timer)
+ $tpl .= $this->tplAscii($timer);
+
+ return $tpl;
+ }
+
+ private function tplAscii($timer)
+ {
+ $lpad = $rpad = 0;
+ $lspace = $chars = $rspace = '';
+ if($timer['start'] > $this->min)
+ $lpad = intval(($timer['start'] - $this->min) / $this->step);
+ if($timer['end'] < $this->max)
+ $rpad = intval(($this->max - $timer['end']) / $this->step);
+ $mpad = $this->width - $lpad - $rpad;
+ if($lpad > 0)
+ $lspace = str_repeat(' ', $lpad);
+ if($mpad > 0)
+ $chars = str_repeat('=', $mpad);
+ if($rpad > 0)
+ $rspace = str_repeat(' ', $rpad);
+
+ $tpl = <<<TPL
+({$timer['api']} :: code={$timer['code']}, start={$timer['start']}, end={$timer['end']}, total={$timer['time']})
+[{$lspace}{$chars}{$rspace}]
+
+TPL;
+ return $tpl;
+ }
+}
View
28 tests/sequencerTest.php
@@ -0,0 +1,28 @@
+<?php
+include dirname(__FILE__).'/../EpiCurl.php';
+include dirname(__FILE__).'/../EpiOAuth.php';
+include dirname(__FILE__).'/../EpiTwitter.php';
+include dirname(__FILE__).'/../EpiSequence.php';
+$consumer_key = 'jdv3dsDhsYuJRlZFSuI2fg';
+$consumer_secret = 'NNXamBsBFG8PnEmacYs0uCtbtsz346OJSod7Dl94';
+$token = '25451974-uakRmTZxrSFQbkDjZnTAsxDO5o9kacz2LT6kqEHA';
+$secret= 'CuQPQ1WqIdSJDTIkDUlXjHpbcRao9lcKhQHflqGE8';
+$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $token, $secret);
+$twitterObj->useAsynchronous(true);
+?>
+
+Test sequencing diagram of api calls
+
+<?php
+ $creds = array();
+ $creds[] = $twitterObj->get('/direct_messages.json');
+ $creds[] = $twitterObj->get('/users/suggestions.json');
+ $creds[] = $twitterObj->get('/statuses/public_timeline.json');
+
+ foreach($creds as $cred) {
+ $cred->responseText;
+ }
+
+ $sequence = EpiCurl::getSequence();
+ echo $sequence->renderAscii();
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.