SiteController.php 4.7 KB

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