WEBアプリケーション速さ比較

C言語にはClearSilverというテンプレートエンジンがあるらしい。
WEBアプリはApacheモジュールで作れば一番速そうだけど、テンプレートエンジン使うとどうなのかなと思いテストしてみた。

  • 結論

Resinが断トツで速かった。Apacheモジュール+ClearSilverよりもかなり速いという結果になった。(本当か?)
でもVMwareの動作速度が安定しないので、VMwareのせいかも知れない。テンプレートエンジンを使うとファイルの読み込みの分遅くなるのかな?

  • テスト環境

マシン: Dell Inspiron5100
メモリ: 1024MB
OS: WindowsXP上のVMware上のFedora Core 6(メモリ512MB)
Apache: バージョン2.2.3


以下のようなabコマンドで測定。

ab -n 1000 -c 100 http://localhost/hello
ab -n 1000 -c 100 http://localhost/hello.php
ab -n 1000 -c 100 http://localhost/hello.jsp
  • 結果

結果が不安定なので、各テスト10から15回くらい実行して上位5回をリストした。

Resin3.1 (JSP) Apacheモジュール+ClearSilver PHP 5.1.6 Jetty6.1.1 (JSP) CGI+ClearSilver (C言語)
1回目 1412.74 890.48 646.12 438.89 46.78
2回目 1498.16 909.76 656.96 553.08 39.87
3回目 1476.11 898.77 628.91 420.45 26.25
4回目 1450.28 882.66 656.96 410.76 21.90
5回目 1409.04 929.11 658.63 456.15 24.08

(「Requests per second:」の結果のリスト)

  • 以下はテストで使ったソース

JSPのソース

<html>
<head>
<title>hello world!</title>
</head>
<body>
<h1><% out.println("hello world!"); %></h1>
</body>
</html>

Apacheモジュール mod_hello.c のソース

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "http_log.h"
#include "ClearSilver.h"

static NEOERR *render_cb(void *obj, char *s)
{
    request_rec *r = (request_rec *)obj;
    ap_rputs(s, r);
    return STATUS_OK;
}

static void output_body(request_rec *r)
{
    HDF *hdf;
    CSPARSE *cs;
    hdf_init(&hdf);
    hdf_set_value(hdf, "message", "hello world!");
    cs_init(&cs, hdf);
    cs_parse_file(cs, "/hello/hello.cs");
    cs_render(cs, r, render_cb);

    cs_destroy(&cs);
    hdf_destroy(&hdf);
}

static int hello_handler(request_rec *r)
{
    if (strcmp(r->handler, "hello")) {
        return DECLINED;
    }
    if (!r->header_only) {
        output_body(r);
    }
    return OK;
}

static void hello_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(hello_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA hello_module = {
    STANDARD20_MODULE_STUFF,
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    NULL,                  /* table of config file commands       */
    hello_register_hooks   /* register hooks                      */
};

CGIのソース

#include <stdio.h>
#include "ClearSilver.h"

NEOERR *render_cb(void *data, char *s)
{
    printf("%s", s);
    return STATUS_OK;
}

int main()
{
    printf("content-type: text/html\n\n");
    HDF *hdf;
    CSPARSE *cs;
    hdf_init(&hdf);
    hdf_set_value(hdf, "message", "hello world!");
    cs_init(&cs, hdf);
    cs_parse_file(cs, "/hello/hello.cs");
    cs_render(cs, NULL, render_cb);
    cs_destroy(&cs);
    hdf_destroy(&hdf);
    return 0;
}

ClearSilverのテンプレート hello.cs のソース

<html>
<head>
<title>hello world!</title>
</head>
<body>
<h1><?cs var:message ?></h1>
</body>
</html>

hello.phpのソース

<html>
<head>
<title>hello world!</title>
</head>
<body>
<h1><?php echo "hello world!"; ?></h1>
</body>
</html>