|
|
カテゴリ
最新の記事
以前の記事
検索
おすすめキーワード(PR)
ファン
|
今さらながら、サーバ屋なら一本ぐらい作っておかなあかんやろうと。
http://twitter.com/okamenosuke_bot 昭和初期の詩人、尾形亀之助の bot です。 つまづく石でもあれば私はそこでころびたい。 使ったのは PHP。文字コードは全て UTF-8。 ・手順 (1) OAuth のライブラリをダウンロード (2) Bot プログラムを動作させるレンサバを確保 (3) Bot プログラムを cron で呼び出すレンサバを確保 (4) Bot 用 Twitter アカウント取得 (5) Bot 用 Twitter アプリケーション登録 (6) プログラムを書く (7) cron を設定する では順を追って。 (1) OAuth のライブラリをダウンロード Twitter の認証方式は 2010年8月に BASIC 認証から OAuth 認証に切り替わりました。 OAuth 認証はやたらとややこしいので、頭のいい人が作ってくれたライブラリに頼ることにしました。 Thank you, abraham. 下記の URL から落とせます。(abraham-twitteroauth-0.2.0-beta3-0-g76446fa.tar.gz) 使用するのは tar ボールの中にある、OAuth.php と twitteroauth.php。 twitteroauth https://github.com/abraham/twitteroauth (2) Bot プログラムを動作させるレンサバを確保 今回は @pages を借りました。 http://atpages.jp/ (3) Bot プログラムを cron で呼び出すレンサバを確保 今回は 000webhost.com を借りました。 海外のサーバですが、登録も簡単で機能も恐ろしく充実しているのでお勧めです。 http://www.000webhost.com/ ※注 abraham の twitteroauth ライブラリを動作させるには、レンタルサーバ上の cURL が SSL に対応している必要があります。 対応していない場合、 Protocol https not supported or disabled in libcurl というようなエラーメッセージが吐かれるはず。 000webhost.com は残念ながら SSL 非対応の cURL だったので、今回は二箇所のサーバを借りました。cron が設定できる && cURL が SSL 対応している サーバが借りられるならば一箇所でOKです。 (4) Bot 用 Twitter アカウント取得 普通に取得。 (5) Bot 用 Twitter アプリケーション登録 (4) で取得したアカウントでログインした状態で https://dev.twitter.com/apps/new にアクセス。 下記の項目を設定します。 アプリケーション名 : (適当に) アプリケーションの説明 : (適当に) アプリケーションのウェブサイトURL : (適当に) アプリケーションの種類 : クライアントアプリケーション 標準のアクセスタイプ: Read & Write 登録が完了すると、下記のキーが発行されるので控えます。 Consumer key Consumer secret Access Token (oauth_token) Access Token Secret (oauth_token_secret) (4) で登録した bot のアカウントの「連携アプリ」に上記で登録したアプリケーションが表示されていれば OK です。 (6) プログラムを書く @pages 側に置いたソフトはこんなかんじです。超簡単。 # ディレクトリ構成 とりあえず、同ディレクトリに突っ込んでおけば大丈夫なはず。 bot.php # 本体 (自作) twitterauth.php # abraham のライブラリ OAuth.php # abraham のライブラリ # bot.php <?php include_once('./twitteroauth.php'); $c_key = (4) で取得した Consumer key; $c_secret = (4) で取得した Consumer secret; $a_token = (4) で取得した Access Token (oauth_token); $a_token_secret = (4) で取得した Access Token Secret (oauth_token_secret); $url = 'https://twitter.com/statuses/update.xml'; $message = (ポストしたいメッセージ); $params = array('status'=>$message); $obj = new TwitterOAuth($c_key, $c_secret, $a_token, $a_token_secret); $obj->oAuthRequest($url, 'POST', $params); (7) cron を設定する bot.php を 呼び出すプログラムを書いて cron 設定します。ソフトはこんなんじ。 # cron.php <?php $url = '(レンタルサーバのURL)/bot.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); $res = curl_exec($ch); curl_close ($ch); で、(4) で取得したアカウントにツイートされていたら OK です。
Smarty のコンパイルファイル、キャッシュファイルを tmpfs に置くと早くなるのか実験です。
先に結論。多少は早くなるようです。 大した手間ではないので、出力先を tmpfs にしておいても良いかもしれません。 同じスクリプトに10回アクセスし、平均実行時間を計測しました。 計測に使用したのは ここ で導入した Xdebug + Wingrind です。 ・環境 CentOS release 5.5 (Final) PHP 5.3.3 (cli) (built: Jul 22 2010 16:41:20) Smarty-3.0rc3 ※eAccelerator などのオプティマイザは無効 ・保存先ディレクトリ作成 (tmpfs) # cd / # mkdir php_tmp # mount tmpfs /php_tmp -t tmpfs # mount ... tmpfs on /php_tmp type tmpfs (rw) # cd php_tmp # mkdir temporary # cd temporary # mkdir cache # mkdir compile # cd ../ # chown -R apache:apache temporary ・実験(1) キャッシュしない場合 ここ で作ったスクリプトで実験します。キャッシュしないのでコンパイルファイルのみが保存されます。 A : ext3 が保存先の場合 48.1 msec B : tmpfsが保存先の場合 43.9 msec ・実験(2) キャッシュする場合 (1) と同じスクリプトで実験します。 A : ext3 が保存先の場合 55.6 msec B : tmpfsが保存先の場合 47.6 msec ・まとめ キャッシュしない場合、する場合のいずれも tmpfs を使用した方が (多少ですが) 早いことがわかりました。 キャッシュした方が時間がかかっていますが、これは単純に Smarty ライブラリ内部で呼び出される関数の数が多いからです。(今回のスクリプトの場合、キャッシュ時は 70個、非キャッシュ時は 61個) ん? ということは assign 時のコストが低いスクリプト (DBを見ないとか) の場合、キャッシュした方が遅い場合もあるのでしょうか・・・要検討です。
Xdebug 2.1 と Webgrind 1.0 で PHP をプロファイリングしてみます。
クライアント側で動かす WinCacheGrind に対し、サーバ側で動作するのが Webgrind です。 魅力的なところは、 ・各クライアントに WinCacheGrind をインストールしなくてもよい ・クライアント側に Xdebug のプロファイリングファイルを落とさなくてもよい ちょっとアレなのは、 ・プロファイリングファイルが大きい上にどんどん増える ・サーバの動作が重くなる 開発サーバでの検証時にのみ、使用するのが吉かもしれません。 (サーバが超高速でメモリも超潤沢ならば気にしなくていいかも) ・環境 CentOS release 5.5 (Final) PHP 5.3.3 (cli) (built: Jul 22 2010 16:41:20) ・Xdebug 導入 http://www.xdebug.org/ Xdebug 2.1 also provides PHP 5.3 support. と、あるように PHP 5.3 対応です。 pecl コマンドでインストール。xdebug.so がインストールされます。 # pecl install xdebug Build process completed successfully Installing '/usr/lib/php/modules/xdebug.so' install ok: channel://pecl.php.net/xdebug-2.1.0 configuration option "php_ini" is not set to php.ini location You should add "extension=xdebug.so" to php.ini # cd /usr/lib/php/modules/ # ls -la -rw-r--r-- 1 root root 656508 9月 27 22:18 xdebug.so 設定ファイルを作成。php.ini に追記しても勿論構いません。分離させるのは私の趣味。 # cd /etc/php.d # more xdebug.ini zend_extension="/usr/lib/php/modules/xdebug.so" 確認。 # php -m [Zend Modules] Xdebug # php -v PHP 5.3.3 (cli) (built: Jul 22 2010 16:41:20) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans ・Xdebug 設定 各設定項目の説明はこちらを。 http://xdebug.org/docs/all_settings プロファイルする為に最低限必要な設定を書いてみました。 # more /etc/php.d/xdebug.ini zend_extension="/usr/lib/php/modules/xdebug.so" xdebug.profiler_enable = 1 xdebug.profiler_enable_trigger = 1 xdebug.profiler_output_dir = "/var/tmp/xdebug" ファイルが出力されるディレクトリを作成。 # cd /var/tmp # mkdir xdebug # chown -R apache:apache xdebug/ Apache 再起動 # service httpd restart 適当なPHPスクリプトにブラウザからアクセスしてみると、こんなかんじでファイルが生成されます。 # pwd /var/tmp/xdebug # ls -la -rw-r--r-- 1 apache apache 20935 9月 27 22:37 cachegrind.out.7630 ・Webgrind 導入 公式から zip を落とします。こちらも PHP5 系対応。 webgrind-release-1.0.zip 64.8 KB http://code.google.com/p/webgrind/ Webgrind is an Xdebug profiling web frontend in PHP5. 解凍して適当な場所にコピー。 # unzip webgrind-release-1.0.zip # cd /var/www # mkdir webgrind # cp -rp /root/webgrind/* ./webgrind/ # chown -R apache:apache webgrind/ ファイルが出力されるディレクトリを作成 # cd /var/tmp # mkdir webgrind # chown -R apache:apache webgrind/ これは趣味。httpd.conf はなるべくさわりたくないんです。 # cd /etc/httpd/conf.d # more webgrind.conf Alias /webgrind /var/www/webgrind <Directory /var/www/webgrind> Order deny,allow Deny from all Allow from 192.168.0 </Directory> 設定ファイル (config.php) を編集します。$storageDir は Webgrind 用、$profileDir は Xdebug 用に作ったディレクトリのパスを指定。 # cd /var/www/webgrind # vi config.php 19 static $storageDir = '/var/tmp/webgrind'; 20 static $profilerDir = '/var/tmp/xdebug'; Apache 再起動 # service httpd restart ブラウザからアクセスしてみます。あら、動かない。 http://twitpic.com/2t226f Apache のエラーログを見ると php-json が要る模様。 [Mon Sep 27 22:51:35 2010] [error] [client 192.168.0.2] PHP Fatal error: Call to undefined function json_encode() in /var/www/webgrind/index.p hp on line 90, referer: http://192.168.0.100/webgrind/ /etc/php.d/json.ini の ここ でコメントアウトした部分を有効化して Apache 再起動。 # more /etc/php.d/json.ini ; Enable json extension module extension=json.so # service httpd restart 動いた!画面はこんなかんじです。 http://twitpic.com/2t23sx http://twitpic.com/2t25do http://twitpic.com/2t25jj
XCache 1.3.0 の導入と設定です。
業務では eAccelerator 一本槍なので、久しぶりに XCache をさわってみます。 一回安定動作しちゃうとなかなか他のものに手を出さないポッポな自分を変えるんだ!なんてね。 リリースによると XCache 1.3.0 は PHP 5.3 サポート版のようです。 http://xcache.lighttpd.net/wiki/Release-1.3.0 ・環境 CentOS release 5.5 (Final) PHP 5.3.3 (cli) (built: Jul 22 2010 16:41:20) ・導入 公式から tar.gz を落とします。 http://xcache.lighttpd.net/ xcache-1.3.0.tar.gz (104KB) あとはこんなかんじで。/usr/lib/php/modules に xcache.so が入ります。 # tar xvfz xcache-1.3.0.tar.gz # cd xcache-1.3.0 # phpize # ./configure --enable-xcache # make # make install Installing shared extensions: /usr/lib/php/modules/ # cd /usr/lib/php/modules/ # ls -la ... -rwxr-xr-x 1 root root 353829 9月 25 08:51 xcache.so tar.gz の解凍ディレクトリに設定ファイルのサンプル xcache.ini があります。このファイルの内容を php.ini に追記しても構わないんですが、切り離したくなったときに php.ini を書き換えるのが面倒なので /etc/php.d 直下にコピーします。 # ls -la | grep ini -rw-r--r-- 1 root root 75212 9月 25 08:50 tmp-php.ini -rw-r--r-- 1 1003 root 631 7月 6 2009 xcache-test.ini -rw-r--r-- 1 1003 root 2434 7月 6 2009 xcache-zh-gb2312.ini -rw-r--r-- 1 1003 root 2604 7月 6 2009 xcache.ini # cp xcache.ini /etc/php.d/ # cd /etc/php.d コピーした xcache.ini を編集して、php -m で確認。 # php -m PHP Warning: Cannot load module 'XCache' because conflicting module 'eAccelerator' is already loaded in Unknown on line 0 あ。eAccelerator と XCache は共存できないことを忘れてました。 /etc/php.d/eaccelerator.ini の eaccelerator.so を extension 指定している箇所をコメントアウトして再度確認します。 # php -m ... [Zend Modules] XCache ・設定 /etc/php.d にコピーした xcache.ini を編集します。今回はこんなかんじ。パラメータは適当です。設定項目の詳細はこちら。 http://xcache.lighttpd.net/wiki/XcacheIni # more /etc/php.d/xcache.ini [xcache-common] extension = xcache.so [xcache.admin] xcache.admin.enable_auth = On xcache.admin.user = "hoge001" xcache.admin.pass = "fadc66080badf32beac136095b231a0b" [xcache] xcache.shm_scheme = "mmap" xcache.size = 2M xcache.count = 1 xcache.slots = 8K xcache.ttl = 0 xcache.gc_interval = 0 xcache.var_size = 1M xcache.var_count = 1 xcache.var_slots = 8K xcache.var_ttl = 0 xcache.var_maxttl = 0 xcache.var_gc_interval = 300 xcache.test = Off xcache.readonly_protection = Off xcache.mmap_path ="/dev/zero" xcache.coredump_directory = "/var/tmp/xcache/coredump" xcache.cacher = On xcache.stat = On xcache.optimizer = On [xcache.coverager] xcache.coverager = On xcache.coveragedump_directory = "/var/tmp/xcache/coveragedump" xcache.admin.pass は管理画面のパスワードの md5 です。"hoge001pass" にしました。単純なスクリプトで出しました。 # more md5.php print(md5($argv[1])); # php md5.php hoge001pass fadc66080badf32beac136095b231a0b 管理画面は tar.gz を展開して出来たディレクトリの直下にある admin ディレクトリ内にスクリプトがあります。 今回は /var/www/xcache というディレクトリを作成して管理画面用スクリプトをコピーし、/etc/httpd/conf.d に xcache.conf を作成します。 # cd /var/www/ # mkdir xcache # cp /root/xcache-1.3.0/admin/* ./ # cd ../ # chown -R apache:apache xcache/ # cd /etc/httpd/conf.d/ # more xcache.conf Alias /xcache /var/www/xcache <Directory /var/www/xcache> Order deny,allow Deny from all Allow from 192.168.0 </Directory> 管理画面にアクセスするとこんなかんじ。3画面あります。意外と細かいです。 Statistics http://twitpic.com/2s7pse List PHP http://twitpic.com/2s7q84 List Var Data http://twitpic.com/2s7rjl 久々にさわってみましたが、管理系が充実していて驚きました。 XCache API についての記事をあまり見かけないけれど、実際どうなんでしょうね。見かけないって事は使いにくいのかしらん。
Smarty 3.0 RC3 を使ってみます。
サンプルプログラムを作ってみました。 ・ディレクトリ構成 # tree |-- index.php |-- index.tpl ...テンプレートファイル `-- temporary |-- cache ...キャッシュディレクトリ `-- compile ...コンパイルディレクトリ ・index.tpl UTF-8 で書いています。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head> <title>test01</title> </head> <body> これはテストテンプレートです<br /> <br /> 時刻 {$date}<br /> 全角 {$hoge}<br /> </body> </html> ・index.php こちらも UTF-8 で書いています。初期化、変数の割り当て、表示するだけの単純なスクリプトです。書き方は2系と同じで大丈夫みたいです。 <?php include_once '/usr/share/php/smarty/Smarty.class.php'; $cwd = null; $obj = null; $cache_id = 'cach01'; $compile_id = 'comp01'; // get current dir path $cwd = getcwd(); // init $obj = new Smarty(); $obj->template_dir = $cwd; $obj->compile_dir = $cwd.'/temporary/compile'; $obj->cache_dir = $cwd.'/temporary/cache'; $obj->caching = true; $obj->compile_check = false; // assign $obj->assign('date', date('Y-m-d H:i:s')); $obj->assign('hoge', 'ほげ'); // display $obj->display('index.tpl', $cache_id, $compile_id); unset($obj); ・実行イメージ こんなかんじになります。 スクリーンショット ・コンパイルファイルとキャッシュファイル ファイル名、なが! cach01 が display() 関数の引数で指定したキャッシュID, comp01 が同じく指定したコンパイルID です。ここは 2系と比べるとかなり変わったみたいすね (モヤさま風)。 # tree
... `-- temporary |-- cache | `-- cach01^comp01^f616f5373908b61c9b1370d36c09e96ae6d52748.index.tpl.php `-- compile `-- comp01^f616f5373908b61c9b1370d36c09e96ae6d52748.file.index.tpl.cache.php
|
||||||||||||||||||||||||||||||||||||||