For training purposes, I wrote a simple TCP proxy in Erlang. It works, but I experience an odd performance drop when I use ab (Apache Bench) to make many simultaneous requests. This is not a decline in productivity in itself, which makes me wonder, but the extent of the decline. The backend is nginx as a web server. My proxy is between ab and nginx.
This is the code for my proxy.
-module(proxy). -export([start/3]). start(InPort, OutHost, OutPort) -> {ok, Listen} = gen_tcp:listen(InPort, [binary, {packet, 0}, {active, once}]), spawn(fun() -> connect(Listen, OutHost, OutPort) end). connect(Listen, OutHost, OutPort) -> {ok, Client} = gen_tcp:accept(Listen), spawn(fun() -> connect(Listen, OutHost, OutPort) end), {ok, Server} = gen_tcp:connect(OutHost, OutPort, [binary, {packet, 0}, {active, once}]), loop(Client, Server). loop(Client, Server) -> receive {tcp, Client, Data} -> gen_tcp:send(Server, Data), inet:setopts(Client, [{active, once}]), loop(Client, Server); {tcp, Server, Data} -> gen_tcp:send(Client, Data), inet:setopts(Server, [{active, once}]), loop(Client, Server); {tcp_closed, _} -> ok end.
Running 64 consecutive requests in my proxy server. I get a very good result.
ab -n 64 127.0.0.1:80/ Concurrency Level: 1 Time taken for tests: 0.097 seconds Complete requests: 64 Failed requests: 0 Write errors: 0 Total transferred: 23168 bytes HTML transferred: 9664 bytes Requests per second: 659.79 [
This is a bit slower than using Apache Bench directly against nginx.
But, firing 64 concurrent requests in the proxy, performance drops insanely
ab -n 64 -c 64 127.0.0.1:80/ Concurrency Level: 64 Time taken for tests: 2.011 seconds Complete requests: 64 Failed requests: 0 Write errors: 0 Total transferred: 23168 bytes HTML transferred: 9664 bytes Requests per second: 31.82 [
What / where is the problem? I expected lower performance, but why so much? Look at queries per second!
It does not seem to be that important. I give erl many threads using + A. I even tried SMP, but the results are almost the same.
My setup: Windows 7 64, Intel QuadCore, 8 GB RAM. I get similar results on Ubuntu using 128 concurrent requests.
EDIT: A new understanding is included. The total number of requests does not matter. This is just the number of simultaneous requests.