すがブロ

sugamasaoのhatenablogだよ

rack-lineprofをSinatraアプリに使う

ISUCONでたぶん使うであろう自分の作業メモです。

サンプルアプリとして sugamasao/Shiori · GitHub を使ってみるよ。

気になった人はこの本を買ってね!!!!1

Webアプリエンジニア養成読本[しくみ、開発、環境構築・運用…全体像を最新知識で最初から! ] (Software Design plus)

Webアプリエンジニア養成読本[しくみ、開発、環境構築・運用…全体像を最新知識で最初から! ] (Software Design plus)

それでは実際の作業をしていくよ。

% git clone git@github.com:sugamasao/Shiori.git

ディレクトリを移動する

% cd Shiori

Gemfileに rack-lineprof を入れる。

 group :development do
+  gem 'rack-lineprof'
   gem 'pry'
 end

bundle installする。

% bundle install --without production

SQLite3にスキーマを作る

% cd db
% sqlite3 sqlite.db < schema.sqlite3.sql

上位ディレクトリに移動してbundle exec rackupする。 これで通常の動きを確認できる。

次に、rack-lineprofを導入する。

+require 'rack-lineprof'
 require 'rack/protection'
 require './app/shiori'
 
+use Rack::Lineprof

これで http://localhost:9292/?lineprof=shiori.rb にアクセスすると、ターミナルに以下のように出力される。

app/shiori.rb
               |  29  
               |  30    get '/' do
   0.4ms     3 |  31      @bookmarks = Bookmark.order('id DESC').page(params[:page])
   5.4ms     1 |  32      erb :index
               |  33    end
               |  34  

127.0.0.1 - - [28/Sep/2014 00:21:17] "GET /?lineprof=shiori.rb HTTP/1.1" 200 1079 0.0089

なお、パラメータで shiori.rb と指定しているが、app/shiori.rb としても大丈夫。なので、複数で同じ名前のファイル名があったりする場合はディレクトリ名を含めたパスを指定すれば良さそう。 同じように、パラメータ部分をindex.erbにすると以下のように、viewファイルのプロファイルも取れる

[Rack::Lineprof] ===============================================================

app/views/index.erb
               |   1  <h1>登録したURL一覧</h1>
   1.0ms     3 |   2  <%= will_paginate @bookmarks %>
               |   3  <div>
               |   4    <a href='/new'>新規登録</a>
              .......
               |  13    </thead>
               |  14    <tbody>
   1.1ms     4 |  15      <% @bookmarks.each do |bookmark| %>
               |  16        <tr>
               |  17          <td><%= h bookmark.id %></td>

127.0.0.1 - - [28/Sep/2014 00:21:26] "GET /?lineprof=index.erb HTTP/1.1" 200 1079 0.0084

ちなみに、これはrack-lineprofの問題ではないけれど、unicornをインストールする際に利用される wsylvest/kgio · GitHub が2.2.0-preview1でビルドでエラーになってしまう……。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/sugamasao/.rbenv/versions/2.2.0-preview1/bin/ruby -r ./siteconf20140928-35901-blyemq.rb extconf.rb 
checking for CLOCK_MONOTONIC in time.h... no
checking for CLOCK_MONOTONIC() in time.h... no
checking for clockid_t in time.h... no
checking for clock_gettime() in -lrt... no
checking for t_open() in -lnsl... no
checking for socket() in -lsocket... no
checking for poll() in poll.h... yes
checking for getaddrinfo() in sys/types.h,sys/socket.h,netdb.h... yes
checking for getnameinfo() in sys/types.h,sys/socket.h,netdb.h... yes
checking for struct sockaddr_storage in sys/types.h,sys/socket.h... yes
checking for accept4() in sys/socket.h... no
checking for sys/select.h... yes
checking for writev() in sys/uio.h... yes
checking for ruby/io.h... yes
checking for rb_io_t.fd in ruby.h,ruby/io.h... yes
checking for rb_io_t.mode in ruby.h,ruby/io.h... yes
checking for rb_io_t.pathv in ruby.h,ruby/io.h... yes
checking for struct RFile in ruby.h,ruby/io.h... yes
checking size of struct RFile in ruby.h,ruby/io.h... 24
checking for struct RObject... yes
checking size of struct RObject... 40
checking size of int... 4
checking for rb_io_ascii8bit_binmode()... yes
checking for rb_update_max_fd()... yes
checking for rb_fd_fix_cloexec()... yes
checking for rb_cloexec_open()... yes
checking for rb_thread_blocking_region()... no
checking for rb_thread_io_blocking_region()... yes
checking for rb_str_set_len()... yes
checking for rb_time_interval()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_str_subseq()... yes
checking for rb_ary_subseq()... yes
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling accept.c
In file included from accept.c:90:
/Users/sugamasao/.rbenv/versions/2.2.0-preview1/include/ruby-2.2.0/ruby/backward/rubysig.h:14:2: warning: rubysig.h is obsolete [-W#warnings]
#warning rubysig.h is obsolete
 ^
accept.c:101:2: error: use of undeclared identifier 'TRAP_BEG'
        TRAP_BEG;
        ^
accept.c:103:2: error: use of undeclared identifier 'TRAP_END'
        TRAP_END;
        ^
1 warning and 2 errors generated.
make: *** [accept.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/sugamasao/.rbenv/versions/2.2.0-preview1/lib/ruby/gems/2.2.0/gems/kgio-2.8.1 for inspection.
Results logged to /Users/sugamasao/.rbenv/versions/2.2.0-preview1/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-13/2.2.0-static/kgio-2.8.1/gem_make.out
An error occurred while installing kgio (2.8.1), and Bundler cannot continue.
Make sure that `gem install kgio -v '2.8.1'` succeeds before bundling.

基本的には以下のブログをなぞるだけで大丈夫でした。ありがとうございます〜。