ユーザ用ツール

サイト用ツール


Sidebar

アプリケーション系:libmicrohttpd:サンプルとパフォーマンス

libmicrohttpdのサンプルとパフォーマンス

  • CにHTTPサーバ機能を組み込むためのライブラリ
  • 実際にサンプル作ってパフォーマンス計ってみた
  • サンプル
//
// gcc -lmicrohttpd mhd.c
//
 
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <microhttpd.h>
 
#define PORT 8888
 
int response_cb (void *cls
    , struct MHD_Connection *connection
    , const char *url
    , const char *method
    , const char *version
    , const char *upload_data
    , size_t *upload_data_size
    , void **con_cls)
{
    int ret;
    struct MHD_Response *res;
    const char *page  = "hello\n";
 
    res = MHD_create_response_from_buffer(strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT);
    ret = MHD_queue_response (connection, MHD_HTTP_OK, res);
    MHD_destroy_response (res);
 
    return ret;
}
 
 
int main ()
{
    struct MHD_Daemon *daemon;
 
    daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY
        , PORT
        , NULL
        , NULL
        , &response_cb
        , NULL
        , MHD_OPTION_END
    );
 
    if (daemon == NULL)
        return 1;
 
    getchar ();
 
    MHD_stop_daemon (daemon);
 
    return 0;
}
  • build
gcc -lmicrohttpd mhd.c
./a.out
  • abでベンチ
 ab -c 100 -n 100000 http://127.0.0.1:8888/
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/
 
Benchmarking 127.0.0.1 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Finished 100000 requests
 
 
Server Software:
Server Hostname:        127.0.0.1
Server Port:            8888
 
Document Path:          /
Document Length:        6 bytes
 
Concurrency Level:      100
Time taken for tests:   4.664327 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      8100891 bytes
HTML transferred:       600066 bytes
Requests per second:    21439.32 [#/sec] (mean)
Time per request:       4.664 [ms] (mean)
Time per request:       0.047 [ms] (mean, across all concurrent requests)
Transfer rate:          1696.06 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  62.2      0    3001
Processing:     1    2   1.4      2     208
Waiting:        0    0   1.5      0     206
Total:          1    3  62.2      2    3004
 
Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      3
  80%      3
  90%      3
  95%      4
  98%      4
  99%      4
 100%   3004 (longest request)

早い!! そして軽量。

root     31572  0.0  0.0  12208   496 pts/21   Sl+  22:52   0:00          \_ ./a.out

ちょっとしたHTTPサーバ機能を作りたいなら、これで十分かも。

アプリケーション系/libmicrohttpd/サンプルとパフォーマンス.txt · 最終更新: 2013/04/28 22:53 by matsumoto_r