Fork me on GitHub

2009/10/02

はてな
tthttpd(tinytinyhttpd)にiratqqさんがIPv6対応をコードを入れてくれました。通常起動でIPv4、IPv6どちらも扱える様になっていて、引数で
# tthttpd -4
とすればIPv4だけで
# tthttpd -6
とすればIPv6だけで起動する様になります。iratqq++
また、静的なファイルをサーブする場合にはこれまでreadしながらsendしていたのですが、sendfile(2)を使う様にしました。Windowsの場合は昨日Plackの高速化対応した時の様にTransmitFileを使うようにしました。
小さいサイズのファイルだと差は出ませんが、数メガくらいのファイルから格段に差が出て速くなりました。
mattn's tinytinyhttpd at master - GitHub

tiny tiny httpd

http://github.com/mattn/tinytinyhttpd

LinuxサーバHacks―プロが使うテクニック&ツール100選 LinuxサーバHacks―プロが使うテクニック&ツール100選
ロブ フリッケンガー
オライリージャパン / ¥ 2,310 (2003-11)
 
発送可能時間:在庫あり。


2009/09/29

はてな
久々tthttpd(tinytinyhttpd)を触っていて、動いてるだろうと思ってたkeep-aliveのコードが動いていなかった事に愕然としながら修正してたら、以前smegheadさんが報告してくれていたmingw32で落ちる問題の原因を発見。
記憶の底で、_beginthread()したらCloseHandle()しなきゃいねないと思い込んでいたんですが、どうやら_beginthread()じゃなくて_beginthreadex()の場合だけだった(参照)。_beginthread()の場合はスレッド終了時にリソース回収されるらしい。
他いろいろと修正してkeep-aliveが動くようになり、ベンチマーク取ってみた。
テストは「helloworld」と書かれたhello.txtをabでGETする物。

まずはconnection closeの場合
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            8080

Document Path:          /hello.txt
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   0.993 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      160000 bytes
HTML transferred:       11000 bytes
Requests per second:    1006.77 [#/sec] (mean)
Time per request:       9.933 [ms] (mean)
Time per request:       0.993 [ms] (mean, across all concurrent requests)
Transfer rate:          157.31 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     1   10  13.0      6     128
Waiting:        0   10  12.9      6     128
Total:          2   10  13.0      6     129

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      7
  75%      7
  80%      7
  90%     15
  95%     31
  98%     59
  99%     86
 100%    129 (longest request)
そしてconnection keep-aliveの場合
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            8080

Document Path:          /hello.txt
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   0.559 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    1000
Total transferred:      185472 bytes
HTML transferred:       11088 bytes
Requests per second:    1789.60 [#/sec] (mean)
Time per request:       5.588 [ms] (mean)
Time per request:       0.559 [ms] (mean, across all concurrent requests)
Transfer rate:          324.14 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     0    6  19.7      1     263
Waiting:        0    5  19.0      0     262
Total:          0    6  19.8      1     263

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      4
  75%      4
  80%      4
  90%     11
  95%     22
  98%     46
  99%     80
 100%    263 (longest request)
ちなみに同じマシン(Ubuntu9.10 on CeleronM 1.5GHz 500M)で取ったapache2のベンチ。
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache/2.2.11
Server Hostname:        localhost
Server Port:            80

Document Path:          /~mattn/hello.txt
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   0.771 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    1000
Total transferred:      464920 bytes
HTML transferred:       11000 bytes
Requests per second:    1297.49 [#/sec] (mean)
Time per request:       7.707 [ms] (mean)
Time per request:       0.771 [ms] (mean, across all concurrent requests)
Transfer rate:          589.09 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       7
Processing:     6    8   3.3      7      28
Waiting:        6    7   3.1      6      27
Total:          6    8   3.3      7      28

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      7
  75%      7
  80%      7
  90%     12
  95%     16
  98%     18
  99%     27
 100%     28 (longest request)
あまりパフォーマンスは意識してなかった割には、まぁまぁな速度(Request per secondでapache2の1.5倍速くらい?)が出たんじゃないかと思います。ただ機能も小さいですしapacheにも幾らかモジュールが入っちゃってますから参考値にしかすぎませんが...
とりあえずmingw32で不意に落ちる問題が解決したのでこれからパフォーマンスも気にしながらやって行こうかなーと思います。


Effective C++ 原著第3版 (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) Effective C++ 原著第3版 (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES)
スコット・メイヤーズ
ピアソン・エデュケーション / ¥ 3,990 (2006-04-29)
 
発送可能時間:在庫あり。


Effective STL―STLを効果的に使いこなす50の鉄則 Effective STL―STLを効果的に使いこなす50の鉄則
スコット メイヤーズ
ピアソンエデュケーション / ¥ 3,360 (2002-01)
 
発送可能時間:在庫あり。


2009/08/18

はてな
色々と修正を施して、sinatraのdispatch.cgiも動くようになっています。またBASIC認証をサポートしましたので、tDiaryのupdate.rbだけに認証をかけられる様になっています。実際には、ターゲットURL、メソッド(GET/POSTなど)、ユーザ・パスワード一覧という設定が可能になったので、複数ユーザを扱う事も出来ます。
ただ現状、プレインテキストのみ対応ですので、これについては今後なんとかして行きたいと思っています。

本当ならば.htaccessを読み込ませたいのですが、.htaccessはcrypt/md5を使って暗号化されており、libcryptを使うとなれば依存が増えると同時に、ライセンス的にもグレーになってきますので、少し悩んでいます。どこかにPublic Domainで書かれたlibcrypt知りませんか?

2009/08/04

はてな
smegheadさんがtthttpdのpatchを書いてくれました。感謝!

その後、デフォルトページがCGIの場合に上手く動かないバグを修正しました。
お礼とは言っては何ですが(Windowsなんかイラネかもしれませんが)、smegheadさんが作っておられるC言語で書かれたBTS「starbug1」をWindowsにポーティングしてみました。
以下patch
diff -u starbug1-1.3.01.orig/hook.c starbug1-1.3.01/hook.c
--- starbug1-1.3.01.orig/hook.c 2009-07-12 20:37:55.000000000 +0900
+++ starbug1-1.3.01/hook.c  2009-08-04 10:11:19.125000000 +0900
@@ -4,7 +4,11 @@
 #include <cgic.h>
 #include <dirent.h>
 #include <sys/stat.h>
+#ifndef _WIN32
 #include <dlfcn.h>
+#else
+#include <windows.h>
+#endif
 #include "data.h"
 #include "alloc.h"
 #include "util.h"
@@ -12,6 +16,24 @@
 #include "hook_data.h"
 #include "simple_string.h"
 
+#ifdef _WIN32
+#define dlopen(x,y) (void*)LoadLibrary(x)
+#define dlsym(x,y) (void*)GetProcAddress((HMODULE)x,y)
+#define dlclose(x) FreeLibrary((HMODULE)x)
+const char* dlerror() {
+    static char szMsgBuf[256];
+    FormatMessage(
+            FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+            NULL,
+            GetLastError(),
+            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+            szMsgBuf,
+            sizeof szMsgBuf,
+            NULL);
+    return szMsgBuf;
+}
+#endif
+
 static void put_env_a(char* name, char* value, char* buf)
 {
     sprintf(buf, "%s=%s", name, value);
diff -u starbug1-1.3.01.orig/util.c starbug1-1.3.01/util.c
--- starbug1-1.3.01.orig/util.c 2009-07-25 22:38:03.000000000 +0900
+++ starbug1-1.3.01/util.c  2009-08-04 11:39:35.078125000 +0900
@@ -698,9 +698,21 @@
     char locale_utf8[DEFAULT_LENGTH];
     sprintf(locale_utf8, "%s.UTF-8", locale);
     d("locale: %s\n", locale_utf8);
+#ifndef _WIN32
     setenv("LANG", locale_utf8, 1); /* FreeBSD(さくらインターネット)でsetlocaleが動作しない場合があったため、環境変数を書き換える。 */
+#else
+    {
+        char envstr[256];
+        strcpy(envstr, "LANG=");
+        strcat(envstr, locale_utf8);
+        putenv(envstr);
+    }
+#endif
     d("setlocale: %s\n", setlocale(LC_ALL, locale_utf8));
     d("bindtextdomain: %s\n", bindtextdomain("starbug1", "locale"));
     d("textdomain: %s\n", textdomain("starbug1"));
+#ifdef _WIN32
+    bind_textdomain_codeset("starbug1", "utf-8");
+#endif
 }
 /* vim: set ts=4 sw=4 sts=4 expandtab fenc=utf-8: */
ビルドに必要なツール類はgnuwin32なんかで取得して下さい。
見事起動出来ました。
starbug1-win32
ありがとうございました。