Skip to content

Instantly share code, notes, and snippets.

@motooka
Last active August 24, 2017 07:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motooka/02a4f2d4ff2e9ad96b5d51bbd25c8454 to your computer and use it in GitHub Desktop.
Save motooka/02a4f2d4ff2e9ad96b5d51bbd25c8454 to your computer and use it in GitHub Desktop.
PHPで時間のかかる処理を再現する。signalとか受け取ったときの挙動の確認。
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$start = microtime(true);
$a = 0;
while(true) {
if(microtime(true)-$start > 10) {
break;
}
$a++;
}
echo 'OK';
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$phpCommand = '$start=microtime(true);$a=0;while(microtime(true)-$start<10){$a++;}';
$osCommand = "php -r '$phpCommand' 1>/dev/null 2>&1 &";
exec($osCommand);
echo 'ok';
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$sleepResult = sleep(10);
if($sleepResult === false) {
echo 'sleep error';
}
else if($sleepResult === 0) {
echo 'OK';
}
else {
echo 'sleep interrupted';
}

検証の目的など

ブログ記事へ。 http://tmotooka.hatenablog.jp/entry/2016/11/06/121819

検証環境

  • AWS EC2 t2.nano (512MB RAM) 2台(Apache検証用 と NGINX検証用)
  • Amazon Linux 2016.09
  • Apache の場合
    • sudo yum install php70 で PHP 7.0.11 と Apache 2.4.23 をインストール
    • sudo service httpd start で Apache 起動
  • NGINX の場合
    • sudo yum install php70 で PHP 7.0.11 と Apache 2.4.23 をインストール
    • sudo yum install php70-fpm で PHP-FPM をインストール
    • sudo yum install nginx で NGINX 1.10.1 をインストール
    • sudo service php-fpm start で PHP-FPM 起動
    • sudo service nginx start で NGINX 起動

各再起動方法とPHPの挙動

リクエストは curl で投げた。

再起動方法 wait_with_sleeping.php wait_consuming_cpu_resource.php wait_on_background.php
sudo service httpd restart 死ぬ 死ぬ 死ぬ
sudo service httpd reload 死ぬ 死ぬ 死ぬ
sudo service httpd graceful sleepが中断される きちんと待ってくれる 死なない
sudo service nginx restart 死ぬ 死ぬ 死なない
sudo service nginx reload きちんと待ってくれる きちんと待ってくれる 死なない
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment