Oh where to begin... most of the points made so far have been very valid, you ideally should optimizing your application by unbinding unused models etc, only loading those you need, and since you have the overhead of using the framework, again ideally, use your models(and the framework as a whole) to their full potential a bit more, less raw sql etc. ( re: raw sql - the exception here being really tricky joins & complicated sql statements, sometimes running these querries raw can save you a lot of overhead of loading and binding all sorts of crazy associations)
But it still stands that cake is a thirsty beast, even when optimized just the overhead of initializing the framework is still going to result in a hefty cpu hit. I have personally developed a number of large scale commercial applications with the framework and have now just come to accept that there are performance hits that i wouldnt get if i was using some simple hand rolled solution. So to make it worth your while, you need to take advantage of all the clever little things that cake does, all in all helping you write less code to achieve more - and the only real way your going to get to this point is learning the framework more comprehensively in order to use it more efficiently. So, not a really an answer to your question, but gives you some insight into the experience I have had with the framework & its performance at least. The fact is, the framework introduces a large overhead to execute what can seem to be simple code, it then just becomes a question of making it run more efficiently, and there are many aspects to this. I would suggest at least searching around for info on some of the following: Unbinding uneeded associations APC & opcode caching Query caching & persistent models Maybe some others can share some more details on how they have optimized their cake app's, it always seems to be a hot topic. Cake is great, but my best tip is learn to use it efficiently, otherwise it'll bite you in terms of performance. HTH, Paul. On Jun 23, 2:33 pm, PD <[email protected]> wrote: > Hello Everyone, > > I am developing a simple cake application which is causing CPU > spikes. I dont know what I am doing wrong. I have pasted my controller > code below. > > class DatasyncController extends AppController > { > > public $uses = array('LiveServer', 'LivePhone', 'SyncLog', 'Server', > 'Phone'); > > public function index() { > echo 'This is Datasync->index()'; > exit(); > } > > public function serversync() { > // sync server > $traverseOrder = array('LiveServer.result_id ASC'); // > traverse the > list starting with the first record > > $current = $this->LiveServer->find('first', array('order' => > $traverseOrder, 'fields' => array('LiveServer.result_id', > 'LiveServer.server_id', 'LiveServer.reported'))); > $hasNext = true; > > do > { > //process current > if (!$this->SyncLog->find('first', array('conditions' > => array('id' > => $current['LiveServer']['result_id'])))) > { > if (!$this->Server->find('first', > array('conditions' => array('id' > => $current['LiveServer']['server_id'])))) > $this->Server->save(array('Server' => > array('id' => $current > ['LiveServer']['server_id'], 'first_log_date' => $current['LiveServer'] > ['reported'], 'last_log_date' => $current['LiveServer']['reported'], > 'total_logs' => '1'))); > else > $this->Server->query(" UPDATE > {$this->Server->useTable} > > SET last_log_date = '{$current['LiveServer']['reported']}', > total_logs = total_logs + 1 > > WHERE id = '{$current['LiveServer']['server_id']}' > > LIMIT 1" > > ); > > // record the result id to keep track; > $this->SyncLog->save(array('SyncLog' => > array('id' => $current > ['LiveServer']['result_id']))); > echo " + "; > //echo "Inserted > {$current['LiveServer']['result_id']} - {$current > ['LiveServer']['server_id']}<br />"; > } > > // find next > $neighbors = $this->LiveServer->find('neighbors', > array('field' => > 'result_id', 'value' => $current['LiveServer']['result_id'], 'order' > => $traverseOrder, 'fields' => array('LiveServer.result_id', > 'LiveServer.server_id', 'LiveServer.reported'))); > $hasNext = is_array($neighbors['next']); > $current = $neighbors['next']; > > } while ($hasNext); > echo "done";exit(); > // sync phones > } > > public function phonestats() { > > // sync phones > $traverseOrder = array('LivePhone.phone_id'); > > $current = $this->LivePhone->find('first', array('order' => > $traverseOrder, 'fields' => array('DISTINCT LivePhone.mac', > 'LivePhone.useragent', ))); > $hasNext = true; > do { > foreach ($current['LivePhone'] as &$value) $value = > trim($value); > $phoneTypes = array( "Aastra", "Cisco", > "Grandstream", "Polycom", > "Linksys", "snom", "X-Lite", "Zoiper", "eyeBeam", "Sipura", "Siemens", > "Pirelli"); > foreach ($phoneTypes as $phoneType) { > if > (stripos($current['LivePhone']['useragent'], $phoneType)) { > > $q = " UPDATE > {$this->PhoneStat->useTable} > SET value = value + 1 > WHERE id = > '{$current['LivePhone']['server_id']}' > LIMIT 1"; > $c = "CREATE TABLE stats ( > id int(11) NOT NULL, > type enum('server', > 'phone') NOT NULL, > attribute > varchar(255) NOT NULL, > )"; > $this->Phone->query($q); > //echo "<br />" . $q; > break; > } > } > > // find next > $neighbors = $this->LivePhone->find('neighbors', > array('field' => > 'phone_id', 'value' => $current['LivePhone']['phone_id'], 'order' => > $traverseOrder, 'fields' => array('LivePhone.server_id', > 'LivePhone.mac', 'LivePhone.add_date'))); > $hasNext = is_array($neighbors['next']); > $current = $neighbors['next']; > } while ($hasNext); > exit(); > } > > public function phonesync() { > // sync phones > $traverseOrder = array('LivePhone.phone_id'); > > $current = $this->LivePhone->find('first', array('order' => > $traverseOrder, 'fields' => array('LivePhone.server_id', > 'LivePhone.mac', 'LivePhone.add_date'))); > $hasNext = true; > do { > foreach ($current['LivePhone'] as &$value) $value = > trim($value); > > // if this record has not been processed - process it > if (!$this->SyncLog->find('first', array('conditions' > => array('id' > => $current['LivePhone']['phone_id']))) && strlen($current['LivePhone'] > ['mac'])) { > if (!$this->Phone->find('first', > array('conditions' => array('mac' > => $current['LivePhone']['mac'])))) { > echo "<br />added new phone"; > $phoneData = array('Phone' => > > array( > > 'server_id' => $current['LivePhone']['server_id'], > > 'mac' => $current['LivePhone']['mac'], > > 'first_log_date' => > $current['LivePhone']['add_date'], > > 'last_log_date' => > $current['LivePhone']['add_date'], > > 'total_logs' => '1' > > ) > > ); > $this->Phone->save($phoneData); > $q = " UPDATE > {$this->Server->useTable} > SET total_phones = > total_phones + 1 > WHERE id = > '{$current['LivePhone']['server_id']}' > LIMIT 1"; > $this->Phone->query($q); > //echo "<br />" . $q; > } else { > echo "<br />updated existing phone"; > $q = " UPDATE > {$this->Phone->useTable} > SET last_log_date = > '{$current['LivePhone']['add_date']}', > total_logs = total_logs + 1 > WHERE mac = > '{$current['LivePhone']['mac']}' > LIMIT 1"; > $this->Phone->query($q); > //echo "<br />" . $q; > } > > // record the processed phone id to keep > track; > $this->SyncLog->save(array('SyncLog' => > array('id' => $current > ['LivePhone']['phone_id']))); > //echo > "<pre>";print_r($current['LivePhone']);echo "</pre>"; > echo " + "; > > // find next > $neighbors = > $this->LivePhone->find('neighbors', array('field' => > 'phone_id', 'value' => $current['LivePhone']['phone_id'], 'order' => > $traverseOrder, 'fields' => array('LivePhone.server_id', > 'LivePhone.mac', 'LivePhone.add_date'))); > $hasNext = is_array($neighbors['next']); > $current = $neighbors['next']; > } > } while ($hasNext); > exit(); > } > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
