SiteController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use yii\web\Controller;
  6. class SiteController extends Controller
  7. {
  8. private function resJson($data) {
  9. header('Content-type: application/json');
  10. echo json_encode($data);
  11. }
  12. public function actionJson() {
  13. return $this->resJson(array('message'=>'Hello, World!'));
  14. }
  15. public function actionDb($queries = 1) {
  16. // Set up for Test
  17. // $cmd = Yii::$app->db->createCommand('insert into World (randomNumber) values (:v)');
  18. // for($i = 1; $i <=10000 ; $i ++ ) {
  19. // $cmd->bindValue(':v',mt_rand(1, 10000))->execute();
  20. // }
  21. $statement = Yii::$app->db->createCommand('select id,randomNumber from World where id = :id');
  22. if ($queries == 1) {
  23. $arr = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
  24. } else {
  25. if ($queries > 500) $queries = 500;
  26. elseif ($queries <= 0 ) $queries = 1;
  27. // Create an array with the response string.
  28. $arr = array();
  29. // For each query, store the result set values in the response array
  30. while (0 < $queries--) {
  31. // Store result in array.
  32. $arr[] = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
  33. }
  34. }
  35. return $this->resJson($arr);
  36. }
  37. private static function cmp($a, $b) {
  38. return ($a["message"] < $b["message"]) ? -1 : 1;
  39. }
  40. public function actionFortunes() {
  41. // Test Data
  42. // $arr = [
  43. // 11=>'<script>alert("This should not be displayed in a browser alert box");</script>',
  44. // 4=>'A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1',
  45. // 5=>'A computer program does what you tell it to do, not what you want it to do.',
  46. // 2=>'A computer scientist is someone who fixes things that aren\'t broken.',
  47. // 8=>'A list is only as strong as its weakest link. — Donald Knuth',
  48. // //0=>'Additional fortune added at request time.',
  49. // //0=>'Additional fortune added at request time.',
  50. // 3=>'After enough decimal places, nobody gives a damn.',
  51. // 7=>'Any program that runs right is obsolete.',
  52. // 10=>'Computers make very fast, very accurate mistakes.',
  53. // 6=>'Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen',
  54. // 9=>'Feature: A bug with seniority.',
  55. // 1=>'fortune: No such file or directory',
  56. // 12=>'フレームワークのベンチマーク'
  57. // ];
  58. // foreach($arr as $k=>$v) {
  59. // Yii::$app->db->createCommand('insert into Fortune (id,message) values (:id,:message)',[':id'=>$k,':message'=>$v])->execute();
  60. // }
  61. $arr = Yii::$app->db->createCommand('select id, message from Fortune')->queryAll();
  62. $arr[] = ['id'=>0,'message'=>'Additional fortune added at request time.'];
  63. usort($arr, array($this, 'cmp'));
  64. header("Content-Type: text/html; charset=utf-8");
  65. echo <<<EOM
  66. <!DOCTYPE html>
  67. <html>
  68. <head>
  69. <title>Fortunes</title>
  70. </head>
  71. <body>
  72. <table>
  73. <tr>
  74. <th>id</th>
  75. <th>message</th>
  76. </tr>
  77. EOM;
  78. foreach ( $arr as $val ) {
  79. echo '<tr>';
  80. echo '<td>'.$val['id'].'</td>';
  81. echo '<td>'.Html::encode($val['message']).'</td>';
  82. echo '</tr>';
  83. }
  84. echo <<<EOM
  85. </table>
  86. </body>
  87. </html>
  88. EOM;
  89. }
  90. public function actionUpdates($queries = 1) {
  91. if ($queries > 500) $queries = 500;
  92. elseif ($queries <= 0 ) $queries = 1;
  93. $selectCommand = Yii::$app->db->createCommand('select randomNumber from World where id = :id');
  94. $updateCommand = Yii::$app->db->createCommand('update World set randomNumber = :num where id = :id');
  95. $arr = [];
  96. while (0 < $queries--) {
  97. // Store result in array.
  98. $id = mt_rand(1,10000);
  99. $randomNumber = mt_rand(1, 1000);
  100. $selectCommand->bindParam(':id',$id)->queryScalar();
  101. $updateCommand->bindValues([':id'=>$id,':num'=>$randomNumber])->execute();
  102. $arr[] = array('id' => $id, 'randomNumber' => $randomNumber);
  103. }
  104. return $this->resJson($arr);
  105. }
  106. public function actionPlaintext() {
  107. header("Content-Type: text/plain;");
  108. echo 'Hello, World!';
  109. }
  110. }