Tracパフォーマンス改善

omae です。

今回は Trac のパフォーマンスを上げるためにやっている設定を紹介したいと思います。

1. trac.ini の [trac] htdocs_location を設定し、Apache に静的ファイルを処理させる

この方法は TracCgi Mapping Static Resources に書かれていることでありよく知られているものです。 CGI ではなく mod_python などで実行している場合でも Apache に処理させるようにしたほうが速いです。

2. Trac plugin の静的ファイルを Apache に処理させる

Trac plugin は自身が使うための静的ファイルのディレクトリを ITemplateProvider.get_htdocs_dirs() というインターフェイスから返すことになっています。 このディレクトリへのアクセスも Apache に処理させるようにします。

ここでインストールしている plugin からこのディレクトリを一つ一つ調べてもよいのですが、めんどくさいので次のスクリプトを書きました。 引数に TRACENV のパスを指定すると ITemplateProvider.get_htdocs_dirs() の値を収集して出力します。

#! /usr/bin/python

import sys
try:
    set = set
except NameError:
    from sets import Set as set

from trac.env import open_environment
from trac.web.chrome import Chrome

def print_providers(path):
    try:
        env = open_environment(path)
    except Exception, e:
        print('%s: %s', path, e)

    chrome = Chrome(env)
    container = set()
    for provider in chrome.template_providers:
        dirs = provider.get_htdocs_dirs()
        for dir in dirs:
            container.add(dir)

    container = [v for v in container]
    container.sort()

    print('%s:' % path)
    for prefix, dir in container:
        print('\t%s\t%s' % (prefix, dir))

for arg in sys.argv[1:]:
    print_providers(arg)

これを実行すると以下のような出力が得られます。

/var/lib/trac/opengroove:
        bitten  /usr/share/trac/plugins/Bitten-0.6dev_r641-py2.3.egg/bitten/htdocs
        common  /usr/lib/python2.3/site-packages/trac/htdocs
        customfieldadmin        /usr/share/trac/plugins/TracCustomFieldAdmin-0.2-py2.3.egg/customfieldadmin/htdocs
        iniadmin        /usr/share/trac/plugins/IniAdmin-0.2-py2.3.egg/iniadmin/htdocs
        s5      /usr/share/trac/plugins/S5-0.1-py2.3.egg/s5/htdocs
        site    /var/lib/trac/opengroove/htdocs
        ticketdelete    /usr/share/trac/plugins/TracTicketDelete-2.0-py2.3.egg/ticketdelete/htdocs
        tracfreemind    /usr/share/trac/plugins/TracFreeMind-0.2-py2.3.egg/tracfreemind/htdocs
        tracnav /usr/share/trac/plugins/TracNav-4.0pre7-py2.3.egg/tracnav/htdocs
        tracwysiwyg     /usr/share/trac/plugins/TracWysiwyg-0.2_r4353-py2.3.egg/tracwysiwyg/htdocs
        workfloweditor  /usr/share/trac/plugins/WorkflowEditorPlugin-1.0.1_r5329-py2.3.egg/workfloweditor/htdocs

ここで最初の文字列は get_htdocs_dirs() が返す prefix であり、この値をキーにファイルがどのディレクトリにあるかを Trac は判断しています。 また common に関しては htdocs_location の設定値で URL が変更できるようになっています。 site に関しては $TRACENV/htdocs/ に置いたファイルへアクセスできるようするために Trac 自身が get_htdocs_dirs() で登録してくるようになっています。

この出力から http://localhost/trac/ で複数プロジェクトを処理している場合は以下のような設定を書きます。

AliasMatch ^/trac/[^/]+/chrome/bitten/(.+) /usr/share/trac/plugins/Bitten-0.6dev_r641-py2.3.egg/bitten/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/customfieldadmin/(.+) /usr/share/trac/plugins/TracCustomFieldAdmin-0.2-py2.3.egg/customfieldadmin/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/iniadmin/(.+) /usr/share/trac/plugins/IniAdmin-0.2-py2.3.egg/iniadmin/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/s5/(.+) /usr/share/trac/plugins/S5-0.1-py2.3.egg/s5/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/ticketdelete/(.+) /usr/share/trac/plugins/TracTicketDelete-2.0-py2.3.egg/ticketdelete/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/tracfreemind/(.+) /usr/share/trac/plugins/TracFreeMind-0.2-py2.3.egg/tracfreemind/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/tracnav/(.+) /usr/share/trac/plugins/TracNav-4.0pre7-py2.3.egg/tracnav/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/tracwysiwyg/(.+) /usr/share/trac/plugins/TracWysiwyg-0.2_r4353-py2.3.egg/tracwysiwyg/htdocs/$1
AliasMatch ^/trac/[^/]+/chrome/workfloweditor/(.+) /usr/share/trac/plugins/WorkflowEditorPlugin-1.0.1_r5329-py2.3.egg/workfloweditor/
AliasMatch ^/trac/([^/]+)/chrome/site/(.+) /var/lib/trac/$1/htdocs/$2

mod_python で運用している場合には上記だけでなく /trac/*/chrome/ へのアクセスが mod_python に処理させてしまわないように SetHandler None が必要になると思います(未確認。mod_rewrite でないとうまくいかないかも知れません)

<Location /trac/*/chrome/>
    SetHandler None
</Location>

これで体感的な速度が上がるかと思います。

更なる速度改善としては添付ファイルへのアクセスを Apache に処理させる…というのが考えられます。 画像を貼りすぎた Wiki ページでは、やはりその分の速度劣化があるので Apache で処理できれば早く表示できるようになります。 ただし Apache に処理させるということは Trac が行う権限チェック(WIKI_VIEW があるかなど)が効かなくなるので、そのあたりは注意が必要です。

Categorized: Trac

Comments are closed.