Saturday, September 7, 2013

Codeigniter: Benchmarking record count.

 Menggunakan database SQL Server 2012, dengan jumlah record diatas 1juta. Juga ditambah dengan menggunakan MemoryCache untuk process yang nomor 4.
function get_test() {
$this->benchmark->mark('one');
$this->db->select('*')->from('big_data')->order_by('id', 'asc');
$num_row1 = $this->db->get()->num_rows();
$this->benchmark->mark('two');
$this->benchmark->mark('three');
$this->db->flush_cache();
$this->db->select('COUNT(*) AS rec_count', FALSE)->from('big_data');
$num_row2 = $this->db->get()->row()->rec_count;
$this->benchmark->mark('four');
$this->benchmark->mark('five');
$this->db->flush_cache();
$num_row3 = $this->db->query("select count(*) as rec_count from big_data")->row()->rec_count;
$this->benchmark->mark('six');
$this->benchmark->mark('seven');
$num_row4 = $this->cache->memcached->get('num_row4');
if ( !$num_row4 ) {
$this->db->flush_cache();
$this->db->select('COUNT(*) AS rec_count', FALSE)->from('big_data');
$num_row4 = $this->db->get()->row()->rec_count;
$this->cache->memcached->save('num_row4', $num_row4, 60);
}
$this->benchmark->mark('eight');
$this->benchmark->mark('nine');
$this->db->flush_cache();
$num_row5 = $this->db->get('big_data')->num_rows;
$this->benchmark->mark('ten');
echo "PROCESS #1 : ".$this->benchmark->elapsed_time('one', 'two')."<br />";
echo "REC. COUNT #1 : $num_row1"."<br />";
echo "PROCESS #2 : ".$this->benchmark->elapsed_time('three', 'four')."<br />";
echo "REC. COUNT #2 : $num_row2"."<br />";
echo "PROCESS #3 : ".$this->benchmark->elapsed_time('five', 'six')."<br />";
echo "REC. COUNT #3 : $num_row3"."<br />";
echo "PROCESS #4 : ".$this->benchmark->elapsed_time('seven', 'eight')."<br />";
echo "REC. COUNT #4 : $num_row4"."<br />";
echo "PROCESS #5 : ".$this->benchmark->elapsed_time('nine', 'ten')."<br />";
echo "REC. COUNT #5 : $num_row5"."<br />"; }
RESULT #1
====================
PROCESS #1 : 8.0914
REC. COUNT #1 : 1077960
PROCESS #2 : 0.2156
REC. COUNT #2 : 1077960
PROCESS #3 : 0.1757
REC. COUNT #3 : 1077960
PROCESS #4 : 0.1782
REC. COUNT #4 : 1077960
PROCESS #5 : 7.8618
REC. COUNT #5 : 1077960

RESULT #2
====================
PROCESS #1 : 8.1435
REC. COUNT #1 : 1077960
PROCESS #2 : 0.2062
REC. COUNT #2 : 1077960
PROCESS #3 : 0.1807
REC. COUNT #3 : 1077960
PROCESS #4 : 0.0018
REC. COUNT #4 : 1077960
PROCESS #5 : 8.1332
REC. COUNT #5 : 1077960

Result #1, data di memorycache belum tersimpan (PROCESS #4 : 0.1782).
Result #2, data di memorycache sudah tersimpan (PROCESS #4 : 0.0018).

WOW.....!!!

No comments:

Post a Comment