Skip to content
This repository
Browse code

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
Jaisen Mathai authored

Showing 3 changed files with 113 additions and 5 deletions. Show diff stats Hide diff stats

  1. +31 5 EpiCurl.php
  2. +54 0 EpiSequence.php
  3. +28 0 tests/sequencerTest.php
36 EpiCurl.php
@@ -13,6 +13,7 @@ class EpiCurl
13 13 private $requests = array();
14 14 private $responses = array();
15 15 private $properties = array();
  16 + private static $timers = array();
16 17
17 18 function __construct()
18 19 {
@@ -38,6 +39,7 @@ public function addCurl($ch)
38 39 curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));
39 40
40 41 $code = curl_multi_add_handle($this->mc, $ch);
  42 + $this->startTimer($key);
41 43
42 44 // (1)
43 45 if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM)
@@ -67,14 +69,14 @@ public function getResult($key = null)
67 69 while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM))
68 70 {
69 71 usleep($outerSleepInt);
70   - $outerSleepInt = max(1, ($outerSleepInt*$this->sleepIncrement));
  72 + $outerSleepInt = intval(max(1, ($outerSleepInt*$this->sleepIncrement)));
71 73 $ms=curl_multi_select($this->mc, 0);
72 74 if($ms > 0)
73 75 {
74 76 do{
75 77 $this->execStatus = curl_multi_exec($this->mc, $this->running);
76 78 usleep($innerSleepInt);
77   - $innerSleepInt = max(1, ($innerSleepInt*$this->sleepIncrement));
  79 + $innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement)));
78 80 }while($this->execStatus==CURLM_CALL_MULTI_PERFORM);
79 81 $innerSleepInt = 1;
80 82 }
@@ -90,6 +92,16 @@ public function getResult($key = null)
90 92 return false;
91 93 }
92 94
  95 + public static function getSequence()
  96 + {
  97 + return new EpiSequence(self::$timers);
  98 + }
  99 +
  100 + public static function getTimers()
  101 + {
  102 + return self::$timers;
  103 + }
  104 +
93 105 private function getKey($ch)
94 106 {
95 107 return (string)$ch;
@@ -113,6 +125,7 @@ private function storeResponses()
113 125 while($done = curl_multi_info_read($this->mc))
114 126 {
115 127 $key = (string)$done['handle'];
  128 + $this->stopTimer($key, $done);
116 129 $this->responses[$key]['data'] = curl_multi_getcontent($done['handle']);
117 130 foreach($this->properties as $name => $const)
118 131 {
@@ -123,6 +136,19 @@ private function storeResponses()
123 136 }
124 137 }
125 138
  139 + private function startTimer($key)
  140 + {
  141 + self::$timers[$key]['start'] = microtime(true);
  142 + }
  143 +
  144 + private function stopTimer($key, $done)
  145 + {
  146 + self::$timers[$key]['end'] = microtime(true);
  147 + self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL);
  148 + self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME);
  149 + self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE);
  150 + }
  151 +
126 152 static function getInstance()
127 153 {
128 154 if(self::$inst == null)
@@ -140,19 +166,19 @@ class EpiCurlManager
140 166 private $key;
141 167 private $epiCurl;
142 168
143   - function __construct($key)
  169 + public function __construct($key)
144 170 {
145 171 $this->key = $key;
146 172 $this->epiCurl = EpiCurl::getInstance();
147 173 }
148 174
149   - function __get($name)
  175 + public function __get($name)
150 176 {
151 177 $responses = $this->epiCurl->getResult($this->key);
152 178 return isset($responses[$name]) ? $responses[$name] : null;
153 179 }
154 180
155   - function __isset($name)
  181 + public function __isset($name)
156 182 {
157 183 $val = self::__get($name);
158 184 return empty($val);
54 EpiSequence.php
... ... @@ -0,0 +1,54 @@
  1 +<?php
  2 +class EpiSequence
  3 +{
  4 + private $timers, $min, $max, $width = 100;
  5 + public function __construct($timers)
  6 + {
  7 + $this->timers = $timers;
  8 +
  9 + $min = PHP_INT_MAX;
  10 + $max = 0;
  11 + foreach($this->timers as $timer)
  12 + {
  13 + $min = min($timer['start'], $min);
  14 + $max = max($timer['end'], $max);
  15 + }
  16 + $this->min = $min;
  17 + $this->max = $max;
  18 + $this->range = $max-$min;
  19 + $this->step = floatval($this->range/$this->width);
  20 + }
  21 +
  22 + public function renderAscii()
  23 + {
  24 + $tpl = '';
  25 + foreach($this->timers as $timer)
  26 + $tpl .= $this->tplAscii($timer);
  27 +
  28 + return $tpl;
  29 + }
  30 +
  31 + private function tplAscii($timer)
  32 + {
  33 + $lpad = $rpad = 0;
  34 + $lspace = $chars = $rspace = '';
  35 + if($timer['start'] > $this->min)
  36 + $lpad = intval(($timer['start'] - $this->min) / $this->step);
  37 + if($timer['end'] < $this->max)
  38 + $rpad = intval(($this->max - $timer['end']) / $this->step);
  39 + $mpad = $this->width - $lpad - $rpad;
  40 + if($lpad > 0)
  41 + $lspace = str_repeat(' ', $lpad);
  42 + if($mpad > 0)
  43 + $chars = str_repeat('=', $mpad);
  44 + if($rpad > 0)
  45 + $rspace = str_repeat(' ', $rpad);
  46 +
  47 + $tpl = <<<TPL
  48 +({$timer['api']} :: code={$timer['code']}, start={$timer['start']}, end={$timer['end']}, total={$timer['time']})
  49 +[{$lspace}{$chars}{$rspace}]
  50 +
  51 +TPL;
  52 + return $tpl;
  53 + }
  54 +}
28 tests/sequencerTest.php
... ... @@ -0,0 +1,28 @@
  1 +<?php
  2 +include dirname(__FILE__).'/../EpiCurl.php';
  3 +include dirname(__FILE__).'/../EpiOAuth.php';
  4 +include dirname(__FILE__).'/../EpiTwitter.php';
  5 +include dirname(__FILE__).'/../EpiSequence.php';
  6 +$consumer_key = 'jdv3dsDhsYuJRlZFSuI2fg';
  7 +$consumer_secret = 'NNXamBsBFG8PnEmacYs0uCtbtsz346OJSod7Dl94';
  8 +$token = '25451974-uakRmTZxrSFQbkDjZnTAsxDO5o9kacz2LT6kqEHA';
  9 +$secret= 'CuQPQ1WqIdSJDTIkDUlXjHpbcRao9lcKhQHflqGE8';
  10 +$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $token, $secret);
  11 +$twitterObj->useAsynchronous(true);
  12 +?>
  13 +
  14 +Test sequencing diagram of api calls
  15 +
  16 +<?php
  17 + $creds = array();
  18 + $creds[] = $twitterObj->get('/direct_messages.json');
  19 + $creds[] = $twitterObj->get('/users/suggestions.json');
  20 + $creds[] = $twitterObj->get('/statuses/public_timeline.json');
  21 +
  22 + foreach($creds as $cred) {
  23 + $cred->responseText;
  24 + }
  25 +
  26 + $sequence = EpiCurl::getSequence();
  27 + echo $sequence->renderAscii();
  28 +?>

0 comments on commit b69ce31

Please sign in to comment.
Something went wrong with that request. Please try again.