Why does YII consume so much CPU via apache?

A simple statement in Yii make apache consume CPU 100%

Test Environment:

1032

OS.png

CPU:Q9400(2.66 4Core)

OS: Windows server 2008(64)

PHP:5.2.14

Web Server:Apache2.0

No APC, YII:

Cache off, debug off, log off,

Test Method:

page a : testa.php,




<?php 

	for ($index = 0; $index < 10; $index++) {

		echo $index;

	}

	echo "<br/>";

	for ($index = 0; $index < 30; $index++) {

		echo $index." ";

	}

	echo "<br/>";

	for ($index = 0; $index < 50; $index++) {

		echo $index." ";

	}

	echo "<br/>";

	echo time();

 ?>



page b : testb.php, a simple page return data from database by PDO without yii




<?php 

	function createPDOConnect()

    {	

		$connection;

		try{

			$connection = new PDO("oci:dbname=192.168.1.13/ORCL;charset=AL32UTF8", 'WDM_APP', '1234');

			$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

			$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

			$connection->setAttribute(PDO::ATTR_TIMEOUT,60*60*10);

		}catch(PDOException $e){

			echo ("Error:".$e->getMessage()."<br>");

			die();

		}

        return $connection;

    }

	

	$dbh = createPDOConnect();

	$sql = "SELECT * FROM WEBSITE t WHERE ROWNUM<=10";

	

	$stmt = $dbh->prepare( $sql );

	$stmt->execute();

	$selectDataArray = $stmt->fetchAll(PDO::FETCH_ASSOC);

	echo count($selectDataArray);

	foreach($selectDataArray as $d){

		echo $d['WEBSITE_ID'];

	}


 ?>



page c : testc.php, a simple page return "Hello world" with yii

SiteController.php




public function actionIndex()

	{

		$this->render('index');

	}



index.php




<?php 

	$this->layout=false;

	echo "Hello World!;

?>




1034

Apache.png

page d : testd.html a simple html page without yii




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Detail</title>

<link rel="stylesheet" type="text/css" href="/wm/css/info_en_us.css" />

</head>

<body>

1234567890

1234567890

1234567890

</body>

</html>



Client Thread Count: 100

Test Result:

test on page a, apache cpu 5%

test on page b, apache cpu 35%

test on page c, apache cpu 95%

test on page d, apache cpu 3%

I also test it on apache 2.2, the same result!

Why does YII consume so much CPU via apache?

http://www.yiiframework.com/forum/index.php?/topic/13717-realselfcom-15-million-monthly-visitors/

@Antonio: thanks for sharing with us (again! :]) address to this wonderful post, as we are all (or most of us) still shocked of results that are shown there, but look into the code! Post author uses Oracle while you are pointing him to MySQL demonstration.

@climb: You are using Oracle! Oracle is deadly, ugly, stupidly ssssslow (at least in comparison to other RDBMS). I wouldn’t be surprised if your CPU load would not come from Yii but from Oracle. Even more - I would be surprised, if you would show up with some evidences that it is not coming from Oracle. Oracle is slooooow!

I’m using Oracle in my currently developed application and I must admit that I’m shocked! The very same code, byte by byte, executed with DB set to Oracle in some cases is working over one hundred times slower than in MySQL. Look at above comparison, results from my internal tests done last weekend:


Oracle: 

Done selecting 300 records (160 columns) using AR! Execution time:  2,38169789314 secs. 

Done selecting 300 records (1 column) using AR! Execution time:  0,05867600441 secs. 


MySQL: 

Done selecting 2300 records (160 columns) using AR! Execution time:  0,24194717407 secs. 

Done selecting 2300 records (1 column) using AR! Execution time:  0,16201305389 secs. 

It took ten times longer Oracle to select 300 records than MySQL to select nine times more records!

And I have Oracle installed on separate machine, not on the same as Apache, PHP and Yii. If you have it on the same, connecting via localhost I wouldn’t be surprised if Oracle would be your main cause for such processor load. Oracle is slow! At least for web or PHP and in comparison to other database systems!

Thanks everyone!The problems have been solved!

The reason is PDO_OCI Driver is unstable!

You are using PDO_OCI or OCI8 driver? I also heard (as it is stated on website) that this driver is unstable, experimental and many developers still advices to use old OCI8 even for 11+ Oracle databases. But my personal experience is, that it work a bit better than OCI8 and I would propose you to double check if it is truly driver problem or maybe somewhere between it and the server.

Edit: If you are interested in this topic, please continue in this Wiki article, where one of possible solutions (for speeding up ActiveRecord on Oracle) is presented.