Welcome Guest, Not a member yet? Register   Sign In
CI with Ajax is too slow ???
#1

[eluser]Unknown[/eluser]
I use CI (2.1.3) for a big project with several pages in Ajax en Jquery.

I have many sensors that are written in the database and I use Ajax to display the values ​​every 30 seconds.

But we have some problems with speed for the ajax pages
and more we use pages more the response time increases.

I did a simple test and I did benchmark
(i user console.time and console.timeEnd, see the code after)

(time in ms)

147 177 267 400 164 441
166 173 288 388 154 565
202 270 326 369 117 479
165 296 294 401 127 322
233 192 268 436 171 429
112 236 238 364 122 424
---------------------------------------------------
165 228 271 413 155 472 (average time in ms)



detail of result
165 ms : on the first page with PDO
228 ms : on the first page with CI
271 ms : after login
413 ms : after 2 minutes with a lot of click on pages
155 ms : after 4 minutes with PDO
472 ms : after 4 minutes with a lot of click on pages

I do not understand why time increases ?

I thought to rewrite the class Loader only for Ajax with removing the view, the translation ....
but I am not satisfied with the response time


PDO CI

22 130 My_Controller (with extend CI_Controller)
22 105 with CI_Controller (without extend CI_controller)
22 99 LoaderForAjax (make my new Loader class (copy of Core Loader without view, language, )
22 52 load page with controller en index
22 64 use CI with PDO
9 50 only one instruction : echo "hello world";

Solution :
The solution is to use the PDO on Ajax pages, but I did not want to rewrite all my queries, and especially I will not have the benefits of CI (security, DB ...)


Question :
Do you have another solution for me ?
or maybe I used wrong CI (see my code after) ?

Thank for your help

Cédric



code to call ajax
Code:
$('.ci').on('click', function() {

    console.time('fonctionTest');
    
$.ajax({
  url: "<?php echo site_url('testAjaxCI'); ?>",
  type: 'POST',
  async : false,
  data: '',
  success: function(data)
  {
      console.timeEnd('fonctionTest');
      $('div.dataPdoCi').html(data);
      //alert (data);
      
  }});
    
});

Code Ajax with CI and DB

Code:
<?php


class TestAjaxCI extends CI_Controller {

    /**
     * Constructor
     */
    function __construct()
    {
        parent::__construct();
            
        $this->load->database();
        $this->load->model('connexion','',TRUE);  
    }

    /**
     */
    function index()
    {
        $data = array();        
        $ret = $this->connexion->lastConnection(112, 10);  
        // SELECT date_cnx, adresse, information FROM connexion WHERE id_utilisateur = 112 ORDER BY date_cnx DESC LIMIT 10");
        
        echo "AVEC CI<br/>";
          
        foreach ($ret as $data)
        {
            echo '<h3>', $data->date_cnx, ' ', $data->adresse, '</h3>';        
        }
          
        // affichage de la vue
     //   $this->load->view('testPost', $data);
    }

}

Code Ajax with PDO

Code:
&lt;?php
// Connection au serveur
$dns = 'mysql:host=127.0.0.1;dbname=dbname';
$utilisateur = 'dbuser';
$motDePasse = 'dbpass';

try {
    $connection = new PDO( $dns, $utilisateur, $motDePasse );
}
catch ( Exception $e )
{
    echo "Connection à MySQL impossible : ", $e->getMessage();
    die();
}

// On envois la requète
$select = $connection->query("SELECT date_cnx, adresse, information FROM connexion WHERE id_utilisateur = 112 ORDER BY date_cnx DESC LIMIT 10");

// On indique que nous utiliserons les résultats en tant qu'objet
$select->setFetchMode(PDO::FETCH_OBJ);

echo "AVEC PDO<br/>";
// Nous traitons les résultats en boucle
while( $enregistrement = $select->fetch() )
{
    // Affichage d'un des champs
    echo '<h3>', $enregistrement->date_cnx, ' ', $enregistrement->adresse, '</h3>';
}
#2

[eluser]jonez[/eluser]
Was your testing done locally or on a server? You should should not use synchronous AJAX calls unless absolutely necessary.

Quote:and more we use pages more the response time increases.
If every user is calling a complex query on an interval it's going to hammer your server. Optimize the query, check for missing indexes or add additional ones. It could be that the faster queries are cached results (data hasn't changed) and the higher ones aren't.
#3

[eluser]Unknown[/eluser]
thank for your reply,

the time is given with a server on internet, I was 155ms for PDO and 475ms for CI.
And My little test is on local, 22ms PDO and 130 CI

Good point for asynchronous Ajax, but this is just an example for my test.

The query for the test is not complicated:
Code:
SELECT date_cnx, address, information FROM connection WHERE userid = 112 ORDER BY DESC LIMIT 10 date_cnx
I have indexes on all tables and I have little data in my tables

I use only Query Bindings:
Code:
$ sql = "SELECT * FROM WHERE id = some_table AND status = AND author =??"
$ this-> db-> query ($ sql, array (3, 'live', 'Rick'));
and it may be slower than:
Code:
$ this-> db-> query ("SELECT * FROM some_table WHERE id = 3 AND status = 'live' AND author = 'Rick'");

For cache management, great idea, I found the documentation, I try this
http://ellislab.com/codeigniter/user-gui...ching.html

see you soon for results




Theme © iAndrew 2016 - Forum software by © MyBB