C言語のWebFrameworkがどうしても欲しいので作ることにした。
- C言語だけ
- 標準ライブラリ以外は使わない
- この上なく高速で余計なことをFrameworkの中でしない
の3つをポイントに開発することにした。ということで自分で作らないといけないのは
- socketによるTCPの送受信
- HTTP Requestのパーサー
- hashやarrayの仕組み
- HTMLレンダリングエンジン
が特に作らないといけない重要ポイント。
ということで作ってみました。
[c]
#include "cweb/cweb.h"
int main(int argc, char *argv[])
{
CWebListen(8081);
return 1;
}
void didReceiveCWebRequest(CWebTCPConnection *connection, CWebHTTPRequest *request)
{
CWebObject *object = CWebObjectCreateDictionaryStringValueWithCopy("title", "taitoru");
char *html = CWebRenderHTML("./index.html", object);
CWebHTTPResponse *response = CWebResponseCreateWithHTMLBODY(html);
CWebResponse(connection, response);
}
<p class="p1">[/c]
こんな感じで利用できます。main関数内はこれだけです。
ルーティングもあるんですが、今日はつかれたのでここまでにしてます。
アクセスするとこんな感じ
一旦Apache Benchしてみました
Server Software: CWeb Server Hostname: 127.0.0.1 Server Port: 8081 Document Path: / Document Length: 89 bytes Concurrency Level: 10 Time taken for tests: 0.139 seconds Complete requests: 1000 Failed requests: 16 (Connect: 0, Receive: 0, Length: 16, Exceptions: 0) Write errors: 0 Total transferred: 180078 bytes HTML transferred: 89078 bytes Requests per second: 7210.85 [#/sec] (mean) Time per request: 1.387 [ms] (mean) Time per request: 0.139 [ms] (mean, across all concurrent requests) Transfer rate: 1268.08 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.2 0 1 Processing: 0 1 0.5 1 3 Waiting: 0 1 0.4 1 3 Total: 1 1 0.4 1 4 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 2 80% 2 90% 2 95% 2 98% 3 99% 3 100% 4 (longest request)
正直ApacheBenchなんて初めてやるからいいかどうかわからないんだけどw
ただ、revelのテンプレートアプリを同じようにやったらRequests per secondが2000ぐらいなので、まだ激速いとはいえないですね。
やっといまレンダリングエンジンとレスポンスまで出来たのでこれから色々詰めていきます。やりたいのは
- 1リクエストしか処理できないのをlibevent使ってどうにかする
- ちょっとでも malloc->freeを使う部分を減らす