Rails, Ruby

Ruby Enterprise Edition + Passenger2.0 RC1 でRailsを高速化?

2008/06/15

mod_railsで有名なオランダのPhusion社から Ruby Enterprise Edition がリリースされたので、早速Passenger(mod_rails)と組み合わせて試してみました。

Ruby Enterprise Edition は、Rails に最適化されたRubyのようです(An enhanced garbage collector. An improved memory allocator. )。

インストールはいたって簡単で、指定したディレクトリに(gemも)すべて展開されるため、既存のRubyとの併用も可能です。またアンインストールしたければそのディレクトリを削除すればいいだけです。

Ruby Enterprise Edition のインストール

Ruby Enterprise EdtionはPassengerとの組み合わせでメモリ使用量を33%減にできるようですので、ちょっと期待して使ってみました。

1
2
3
4
5
6
7
8
9
10
# wget http://rubyforge.org/frs/download.php/38084/ruby-enterprise-1.8.6-20080507.tar.gz
# tar zxvf ruby-enterprise-1.8.6-20080507.tar.gz
# ./installer
 
......
 
Where would you like to install Ruby Enterprise Edition to?
(All Ruby Enterprise Edition files will be put inside that directory.)
 
[/usr/local/ruby-enterprise/] :インストールするディレクトリを指定

installerを起動すると、どこに設置するのか(line.10)聞かれてインストールが始まります。途中でRailsもインストールされます(すこし時間がかかりました)。

Passenger(mod_rails) 2.0 RC1 のインストール

ここを参考に。

gemでインストール

2.0RC1はpassenger-1.9.0となっているようです。まずはここからダウンロードしておきます。下記のようにさきほどインストールしたRuby Enterprise Editionを利用してgemでインストール。

1
2
# cd /usr/local/ruby-enterprise/
# ./bin/ruby ./bin/gem install passenger-1.9.0.gem

これでRuby Enterprise Editionディレクトリ内にpassengerが配置されます。

Apacheの設定

次にRuby Enterprise Editionディレクトリ内のbin配下にpassenger-install-apache2-moduleというスクリプトを叩きます。

1
2
# cd /usr/local/ruby-enterprise/
# ./bin/passenger-install-apache2-module

ずらずらとメッセージが出てきて、httpd.confへの編集を促されるので、下記をhttpd.confなどに追記します。Ruby Enterprise Editionをインストールしたディレクトリによってパスは変わるので注意してください。また、デフォルトでproductionとして起動しますが、もしdevelopmentで起動したいなら『 RailsEnv development』を追記してください(Configuring Passengerを参照)。

LoadModule passenger_module /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-1.9.0/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-1.9.0
PassengerRuby /usr/local/ruby-enterprise/bin/ruby

ヴァーチャルホストの設定は下記の通りです。DocumentRootはRailsアプリのpublicディレクトリを指定します。

<VirtualHost *:80>
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public
</VirtualHost>

起動

これで設定は完了なはずです。Apacheを再起動して設定ファイルを反映して、アクセスしてみます。productionだとエラーがわかりづらいので、最初はdevelopmentで起動した方がいいのかもしれません。

ぼくの環境の場合、普段使っているgemに入っているものが、Ruby Enterprise Editionのgemに入っていなかったため、エラーが起きてしまいました。その場合は、こっちのgemで再インストールを行います。

1
2
# cd /usr/local/ruby-enterpriise
# ./bin/ruby ./bin/gem install <パッケージ>

メモリ使用量の比較

scaffoldしてから、ちょっと手を加えた程度の小さいアプリを動かしてみたところ、140Mくらい利用しているみたいです。増井さんの記事 (素Ruby + Passenger 1.x系)とあまり変わらない。。。うーむ、設定を間違えたのでしょうか。。アプリも環境も異なるので、後ほどmongrelなどと比較してみたいと思います。

Tags: , , ,

Edge Rails, Rails

フレキシブルなto_xml

2008/06/10

Rails2.0でscaffoldを行うと、HTMLと同じURL+.xmlでXMLの取得も行えます。リソースひとつだけ取得するのであればこのままでも大丈夫なのですが、たとえばuserとblogが1:nの関係で、userのXMLを取得する時に同時に複数のblogも取得したい場合どのようにすればよいのかわかりませんでした。

to_xmlを使わずテンプレートでがんばろうかなと思っていたところ、1年前のRyan’s Scrapsでまさにそのものな:includeオプションを見つけました。

1
2
3
4
5
6
7
@user = User.find(1)
@out = @user.to_xml(:include => :blogs)
 
respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @out }
end

scaffold後に↑のようなソースになりますが、2行目でto_xmlのオプションに:includeを付与しておくと、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id type="integer">2</id>
  <login>ホゲ</login>
 
  ~ 中略 ~
 
  <blogs type="array">
    <blog>
      <created-at type="datetime">2008-06-10T15:06:14+09:00</created-at>
      <id type="integer">1</id>
      <user-id type="integer">2</user-id>
      <title>ホゲブログ1</title>
      <updated-at type="datetime">2008-06-10T15:06:14+09:00</updated-at>
    </blog>
    <blog>
      <created-at type="datetime">2008-06-10T15:36:23+09:00</created-at>
      <id type="integer">5</id>
      <user-id type="integer">2</user-id>
      <title>ホゲブログ2</title>
      <updated-at type="datetime">2008-06-10T15:36:23+09:00</updated-at>
    </blog>
  <blogs/>
</user>

のように、userの子要素として blogs > blog が追加されました。

そのほか to_xml には :include 以外にも便利なオプションがあります。

たとえば不必要な項目を除外する:except、指定項目だけを表示する:only。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
user = User.find(1)
 
user.to_xml(:except => [:id, :created_at])
#=>
# <user>
#   <name>Ryan</name>
#   <email>ryan@spamme.com</email>
# </user>
 
user.to_xml(:only => [:email])
#=>
# <user>
#   <email>ryan@spamme.com</email>
# </user>

また、下記のようにXMLにエレメントを追加することもできます。この例ではidとcreated_atを除外して、serialize_versionを1.1として追加しています。

1
2
3
4
5
6
7
8
9
user.to_xml(:except => [:id, :created_at]) do |xml|
  xml.serialize_version 1.1
end
#=>
# <user>
#   <name>Ryan</name>
#   <email>ryan@spamme.com</email>
#   <serialize_version>1.1</serialize_version>
# </user>

あー便利だなあ。

Tags: ,

Edge Rails

[Edge Rails] DBコンソールを楽に起動:DB Console

2008/05/28

Rails 2.0.91 が使えるようになり、2.1 リリースも間近なようです(2.0.91などマイナーアップデートのものを利用したい場合はこちらを参照してください)。

今回の Edge Rails は、2.0.91 より使えるようになった DB Console です。
これは Rails プロジェクト配下のscript/にあるスクリプトで、使っているDBのコンソールを起動することができます。IDやPWなどの設定情報は、Railsプロジェクトと同じconfig/database.ymlのものが使われます。

$ script/dbconsole
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.51a Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql>

これはMySQLでの例ですが、使っているDBがSQLiteならばSQLiteのコンソールが起動します。

複数のアプリでユーザなどを使い分けている人には特に便利な機能です。

# Ryan’s Scraps の更新を見逃していました。。。

Tags: ,

Rails

RESTfulなRailsをtelnetで試す

2008/05/15

いまさらですが必要になったので、Rails2.0 の scaffold で、RESTfulなアプリを試してみました。確認に手間取ったのでメモ。

RESTfulなアプリを作成

いつもどおり scaffold で、アプリを作成します。

1
2
3
4
5
$ rails rest
$ cd test
$ script/generate scaffold entry title:string description:text
$ rake db:migrate
$ script/server -p 3333

3行目では、titleとdescriptionを持ったentryモデルを作成しています。

確認

GET(データ取得)、DELETE(データ削除)は試せたのですが、POST(データ追加)とPUT(データ更新)がいまいち試せてなかったので、telnetで試してみました。

1
2
3
4
5
6
7
8
9
$ telnet localhost 3333
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
POST /entries.xml HTTP/1.1
Content-Type: application/xml
Content-Length: 83
 
<entry><title>this is the title</title><description>desc desc</description></entry>

5行目よりHTTPメソッドを発行していて、6行目ではContent-Typeをxmlとしています(Content-Typeは必須のようですね)。7行目のContent-Lengthは、9行目に送信しているデータ(XML)のバイト数です。

この結果、下記のようなレスポンスが返ってきます。16,18行目では、指定したtitle, descriptionが入力されているのを確認することができます(もちろん、http://localhost:3333/entries/2 でも確認できます)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
HTTP/1.1 201 Created
Connection: close
Date: Wed, 14 May 2008 14:25:22 GMT
Set-Cookie:<>
Status: 201 Created
Location: http://:/entries/2
X-Runtime: 0.03339
Cache-Control: no-cache
Server: Mongrel 1.1.4
Content-Type: application/xml; charset=utf-8
Content-Length: 286
 
<?xml version="1.0" encoding="UTF-8"?>
<entry>
  <created-at type="datetime">2008-05-14T14:25:22Z</created-at>
  <description>desc desc</description>
  <id type="integer">2</id>
  <title>this is the title</title>
  <updated-at type="datetime">2008-05-14T14:25:22Z</updated-at>
</entry>

ちなみにわざわざtelnetを使わなくても、Firefoxのプラグインである Poster を使うとGUIでHTTPメソッドを発行できるので便利です。

Tags: , ,

ウェブ, 音楽

Muxtape を使ってみた

2008/04/29

巷で流行っているらしい Muxtape をはじめて触ってみました。自分でも作ってみたので、よろしければ聴いてみてください(とくに何のテーマもありませんが、このエントリで触れた、ヤンさんの曲などを入れてみました)。

使っていると他の人のリストをすごく聞きたくなります。できれば、自分が投稿した曲と同じ曲を使っているリストとかを表示してくれると、違う文脈で聴けておもしろいかなと思いました。曲は同じでも扱い方が異なれば、そのおもしろさも変わるでしょうし。

サービス内容 / インターフェイスともにシンプルで、とても好感が持てるサービスです。でもなんとなく郷愁系サービスな気がして、いまの若い人たちはあまり面白く感じないかも。

Tags:


« Previous Entries