2015/11/27


ちょっとしたツールを作る際にシングルヘッダのライブラリは非常に役立ちます。json を STL ライクに扱える picojson 等が有名ですね。

今日は GitHub で mmx というシングルヘッダライブラリを見つけました。

vurtun/mmx - GitHub

single header libraries for C/C++

https://github.com/vurtun/mmx

同梱されているライブラリは、JSON パーサ、C言語っぽい字句解析器、マルチスレッドスケジューラ、行列演算、ライトウェイトなウェブサーバです。

今日はこれを使ってファイルをサーブする簡単なウェブサーバを書いてみました。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#define MMW_STATIC
#define MMW_IMPLEMENTATION
#define MMW_USE_FIXED_TYPES
#define MMW_USE_ASSERT
#include "mm_web.h"

#ifdef _WIN32
#include <winsock2.h>
#define SERVER_INIT { \
  WORD wsa_version = MAKEWORD(2,2); \
  WSADATA wsa_data; \
  if (WSAStartup(wsa_version, &wsa_data)) { \
    fprintf(stderr"WSAStartup failed\n"); \
    return 1; \
  } \
}
#define SERVER_FINISH \
  WSACleanup();
#else
#define SERVER_INIT
#define SERVER_FINISH
#endif

#ifdef __APPLE__
#include <unistd.h>
#endif

static void
logger(const char* text) {
  printf("[debug] %s\n", text);
}

static int
dispatch(struct mmw_con *conn, void *userdata) {
  FILE* fp;
  char buf[4096];
  struct stat fi;

  strcpy(buf, "./public");
  strcat(buf, conn->request.uri);
  if (buf[strlen(buf)-1] == '/') strcat(buf, "index.html");
  if (stat(buf, &fi)) return 1;
  fp = fopen(buf, "r");
  if (!fp) return 1;
  mmw_response_begin(conn, 200, (int) fi.st_size, NULL0);
  while (!feof(fp)) {
    int n = fread(buf, 1sizeof buf, fp);
    mmw_write(conn, buf, n);
  }
  mmw_response_end(conn);
  fclose(fp);
  return 0;
}

int
main(void) {
  void *memory = NULL;
  mmw_size needed_memory = 0;
  struct mmw_server server;

  struct mmw_config config;
  memset(&config, 0sizeof config);
  config.address = "127.0.0.1";
  config.port = 8888;
  config.connection_max = 4;
  config.request_buffer_size = 2048;
  config.io_buffer_size = 8192;
  config.log = logger;
  config.dispatch = dispatch;

  SERVER_INIT;

  mmw_server_init(&server, &config, &needed_memory);
  memory = calloc(needed_memory, 1);
  mmw_server_start(&server, memory);
  while (1) mmw_server_update(&server);
  mmw_server_stop(&server);
  free(memory);

  SERVER_FINISH;

  return 0;
}

お決まりの手順がならびますが、ライトウェイトとは言えどある程度カスタマイズが可能なウェブサーバが作れそうです。パフォーマンスを計測してみようと思ったのですが、どこかで刺さって完走できませんでした。ただ落ちるといった事はありませんし、ブラウザで閲覧する限りにはちゃんと動作している様です。

ちょろっとファイルサーバを書きたい、といったニーズには便利そうです。

ライセンスは mm_json.h、mm_lexer.h、mm_sched.h、mm_vec.h が zlib ライセンス、mm_web.h が BSD ライセンスになります。

C++11/14 コア言語 C++11/14 コア言語
江添 亮
KADOKAWA 単行本 / ¥4,180 (2015年09月25日)
 
発送可能時間:

Posted at by



2015/11/22


先日、Visual Studio Code がオープンソースになりました。

Microsoft/vscode - GitHub
https://github.com/Microsoft/vscode

OSS になる少し前から拡張を作る為の仕組み等が github 上で展開されていたので、もしかするととは思っていましたが予想通りです。

Atom の時にも OpenVim を作ったので

Big Sky :: 21 世紀のエディタである Atom を最強にする

Atom 最高ですね!! でも、Atom は 21 世紀のエディタです。まだ 21 世紀になって 14 年しか経っていないので、 20 世紀最強のエディタ に比べてまだまだ足りない機能があるのはしょう...

http://mattn.kaoriya.net/software/atom/20140922210610.htm

これはやるしかないと決めて作ってみました。(Twitter で告知したので知っている人もいるかと思いますが)

OpenVim

Visual Studio Marketplace - OpenVim

Open a current file in Vim

https://marketplace.visualstudio.com/items/mattn.OpenVim
Open Vim というコマンドで、現在 Visual Studio Code で開いているファイルを Vim (gvim) で開けます。gvim じゃない人は openvim.executablePath という設定で起動コマンドを変更できます。 {
    "openvim.executablePath": "/usr/bin/emacs"
}

リポジトリは以下です。pull-request よろしくお願いします。

mattn/vscode-openvim - GitHub
https://github.com/mattn/vscode-openvim

もう一つ作りました。

Runner

Visual Studio Code はプロジェクトでファイルを実行するには launch.json という設定に書くしかなく、実質一つしか実行出来ない為、非常に面倒だった。そこで、ファイルタイプや拡張子に従って実行出来る物を書きました。

Visual Studio Marketplace - Runner

Run various scripts.

https://marketplace.visualstudio.com/items/mattn.Runner
Runner

CTRL+SHIFT+R をタイプすると主要なスクリプト言語は実行できます。登録されていない物や、ファイルタイプが決まらない物については、以下の様に自分のコマンドに置き換えたり拡張子で設定したりも出来ます。

{
    "runner.extensionMap": {
        "ujm": "ujm",
        "ank": "c:/dev/godev/bin/anko",
        "lisp": "sbcl"
    },
    "runner.languageMap": {
        "c": "crun.bat",
        "csharp": "cscrun.bat",
        "java": "javarun.bat"   
    }
}

個人的には Runner の方は便利だなーと思っています。

mattn/vscode-runner - GitHub
https://github.com/mattn/vscode-runner

こちらも pull-request お待ちしております。

ちなみに crun.bat は以下の様にしています。

@echo off

if "%1" equ "" goto :eof

setlocal
set tempfile=%date:~4%%time::=%
set tempfile=%tempfile:/=%
set tempfile=%tempfile:.=%
gcc -o %tempfile%.exe "%1"
%tempfile%.exe
del %tempfile%.exe

cscrun.bat も同様。

@echo off

if "%1" equ "" goto :eof

setlocal
set tempfile=%date:~4%%time::=%
set tempfile=%tempfile:/=%
set tempfile=%tempfile:.=%
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /out:%tempfile%.exe "%1"
%tempfile%.exe
del %tempfile%.exe

javarun.bat は以下の通り。

@echo off
setlocal enabledelayedexpansion
set CP=.;
for %%i in (lib\*) do set CP=!CP!%%i;
if exist "%JAVA_HOME%\bin\javac.exe" goto run
for /F %%i in ('dir /b "%JAVA_HOME%\..\jdk*"') do (
  set PATH=!JAVA_HOME!\..\%%i\bin;!PATH!
  break
)
:run
javac -encoding utf-8 -cp "%CP%" %1
if %ERRORLEVEL% neq 0 goto :eof
java -cp "%CP%";%~dp1%~n1 %2 %3 %4 %5 %6 %7 %8 %9
:eof
endlocal
exit /b %ERRORLEVEL%
Posted at by



2015/11/20


ミドルウェアとして Use するだけでパフォーマンスが向上するなんて夢の様な話はないと思っていたけど、HTTP Coala はそれをやってのけている様です。

goware/httpcoala · GitHub

Go http middleware handler for request coalescing

https://github.com/goware/httpcoala

ちょっと信じがたかったので、まずはベンチマークを取ってみた。

Coala 未使用

package main

import (
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

func main() {
    goji.Get("/"func(c web.C, w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello World"))
    })
    goji.Serve()
}
$ ab -n 10 -n 10000 http://localhost:8000/ Server Software:
Server Hostname:        localhost
Server Port:            8000

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   4.185837 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1280000 bytes
HTML transferred:       110000 bytes
Requests per second:    2389.01 [#/sec] (mean)
Time per request:       4.186 [ms] (mean)
Time per request:       0.419 [ms] (mean, across all concurrent requests)
Transfer rate:          298.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       7
Processing:     0    3   1.5      4      31
Waiting:        0    2   1.5      2      30
Total:          0    3   1.5      4      31

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      4
  80%      4
  90%      5
  95%      6
  98%      7
  99%      8
 100%     31 (longest request)

Coala 使用

package main

import (
    "net/http"

    "github.com/goware/httpcoala"
    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

func main() {
    goji.Get("/"func(c web.C, w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello World"))
    })
    goji.Use(httpcoala.Route("GET")) // ココ
    goji.Serve()
}
Server Software:
Server Hostname:        localhost
Server Port:            8000

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   3.860772 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1280000 bytes
HTML transferred:       110000 bytes
Requests per second:    2590.16 [#/sec] (mean)
Time per request:       3.861 [ms] (mean)
Time per request:       0.386 [ms] (mean, across all concurrent requests)
Transfer rate:          323.77 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     0    3   2.2      3      74
Waiting:        0    2   2.3      2      71
Total:          0    3   2.2      3      74

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      3
  75%      4
  80%      4
  90%      4
  95%      5
  98%      5
  99%      6
 100%     74 (longest request)

確かに若干ですがパフォーマンスが良くなってる。README.md によるとスレッド数を上げると

Without httpcoala middleware, Requests/sec:   7081.09
   With httpcoala middleware, Requests/sec:  18373.87

2倍以上の差が出る場合もある様です。何がそうさせるのだろうとコードを読んでみた。

httpcoala/httpcoala.go at master · goware/httpcoala · GitHub
https://github.com/goware/httpcoala/blob/master/httpcoala.go

複数のリクエストを channel を使って結合し、同じレスポンスを返すという仕組みで毎回 ResponseWriter の生成や切断時処理を行うのを回避してる。ハンドラ内が遅くなればなるほど効果が出ます。

httpcoala/httpcoala.go at master · goware/httpcoala · GitHub
https://github.com/goware/httpcoala/blob/master/httpcoala.go#L71-L78

今回の様に同じ URL に対して同じメソッドのリクエストが頻繁に起こる場合には非常に有効な手段だと分かる。キャッシュではない為、リアルタイム性も担保される。なかなかいい所に目を付けてるなーと思った。

効果には個人差がある様です。

Go Programming Language, The (Addison-Wesley Professional Computing Series) (English Edition) Go Programming Language, The (Addison-Wesley Professional Computing Series) (English Edition)
Donovan, Alan A. A., Kernighan, Brian W.
Addison-Wesley Professional Kindle版 / ¥4,551 (2015年11月16日)
 
発送可能時間:

Posted at by