0

This is a very simple script which is giving a button to some websites for Affiliate who are using Ajax/Javascript/jQuery to get there required plugin via this following code from my server. but its taking most of the site +/_ 20 seconds and other plugin's get loaded within 30ms.

I cant understand what is the part causing such slow loading? What am i doing wrong here? and how can you improve it?

<?php
header('Access-Control-Allow-Origin: *');  
$customerid = $_GET['id'];
$what = $_GET['button'];
$id = substr('3' . rand(0,9999), 0-$length);

switch ($customerid) {
  case 'customer1':    
    break;
  case 'customer2':
    break;  
  default:
    $da = "https://server0/1.php?r={$id}&endof=end";
    $db = "https://server1?r={$id}";    
    $imagelink = "http://server2.com:8080/images/button.png";

    $a = "<a href={$da} id={$} class={$customerid}abutton onclick=\"typeof(_gaq)=='undefined'?'':_gaq.push(['_trackEvent', 'Mycompany', 'ButtonClick']);typeof(_gat)=='undefined'?'':_gat._getTrackerByName()._setAllowLinker(true); window.open(typeof(_gat)=='undefined'?this.href+'?referrer='+escape(window.location.href):_gat._getTrackerByName()._getLinkerUrl(this.href+'?referrer='+escape(window.location.href)), '_blank', 'width=480,height=360,resizable=no,toolbar=no,menubar=no,location=no,status=no'); return false\"><img src=\"{$imagelink}\" /></a>";

    $b = "<a href={$db} id={$} class={$customerid}aabutton onclick=\"typeof(_gaq)=='undefined'?'':_gaq.push(['_trackEvent', 'Mycompany', 'ButtonClick']);typeof(_gat)=='undefined'?'':_gat._getTrackerByName()._setAllowLinker(true); window.open(typeof(_gat)=='undefined'?this.href+'?referrer='+escape(window.location.href):_gat._getTrackerByName()._getLinkerUrl(this.href+'?referrer='+escape(window.location.href)), '_blank', 'width=480,height=360,resizable=no,toolbar=no,menubar=no,location=no,status=no'); return false\"><img src=\"{$imagelink}\" /></a>";

    $section = file_get_contents("http://server2/ajax/getstatus");
    $section = json_decode($section);
    if ($section->result){
      switch($what) {
        case 'v':
          echo $b;
          break;
        case 'a':
          echo $a;
          break;
        case 'b':
          echo $a . $b;
          break;
      }

    } else {
      switch($what) {
        case 'v':
          break;
        case 'a':
          echo $a;
          break;
        case 'b':
          echo $a;
          break;
      }
    }   
    break;
}

?>

Get status: taking the most duration , what can i do to optimize it? taking almost 10 second on this.

  public function getstatusAction() {
    $this->_response->setHeader('Access-Control-Allow-Origin', '*');       
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();   
    $post = $this->getRequest()->getQuery();
    $data = (object) $post;
    $this->db = Application_Model_Db::db_load();

    $sql = "select *from sh_av_users where
              status='online'
        limit 1";
    $result = $this->db->fetchAll($sql);
    if (count($result) > 0) {
      $json = array(
          'result' =>true,
      );      
    }   else {
      $json = array(
          'result' =>false,
      );        
    } 

    $content = Zend_Json::encode($json);
    $this->getResponse()
            ->setHeader('Content-Type', 'application/json')
            ->setBody($content)
            ->sendResponse();

    exit;
  }
  • 7
    Have you checked the response time for requesting `http://server2/ajax/getstatus` directly to see if that is the bottleneck? – DorianFM Mar 17 '14 at 16:25
  • 1
    What are the repsonse times for `http://server2/ajax/getstatus` and how much data does it return? – Rhys Mar 17 '14 at 16:25
  • Please see my above EDIT. Zend framework is used for getStatus its takeing almost +/_12seconds –  Mar 17 '14 at 16:30
  • Same SQL only takes: MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0004 sec ) –  Mar 17 '14 at 16:30
  • this is only the data: {"result":false} –  Mar 17 '14 at 16:31
  • MySQL query takes 0.0004 sec, data is very small: `{"result":false}` but how come this is taking 15 seconds still? –  Mar 17 '14 at 16:32
  • 1
    In regards to @DorianFM : Check your **[Network Tab](http://stackoverflow.com/a/21617685/2191572)** for further diagnosis – MonkeyZeus Mar 17 '14 at 16:33
  • 1
    It could be that it's an issue with the session. If there are multiple pages using the same session, they have to wait for the other to finish. You can close the writing to the session to test if this is the problem and improves performance. I've had that issue in zend before. – towr Mar 17 '14 at 16:35
  • you have a typo in your sql statement, a missing space between `*` and `from`, should be: `select * from sh_av_users where status='online' limit 1`. – DorianFM Mar 17 '14 at 16:37
  • @DorianFM: i even changed that * into Id only instead of all columns which i was not using also removed the $data = (object) $post; which was not in use. But still same. –  Mar 17 '14 at 16:42
  • OK - its fixed. I applied MySQL index/key on status field + reduced code which is not required in ZF then finally reduced some code in JavaScript into mini JS. then now it became 69MS from 14 seconds. Thanks!! –  Mar 17 '14 at 16:45

0 Answers0