I wrote a simple connection stress test program to echo_server the other day,
Code
- -Module (stress_test ).
- -Export ([start/0, tests/1]).
- Start ()->
- Tests (12345 ).
- Tests (port)->
- IO: Format ("Starting ~ N "),
- Spawn (fun ()-> test (port) end ),
- Spawn (fun ()-> test (port) end ),
- Spawn (fun ()-> test (port) end ),
- Spawn (fun ()-> test (port) end ).
- Test (port)->
- Case gen_tcp: connect ("192.168.0.217", port, [binary, {packet, 0}])
- {OK, _}->
- Test (port );
- _->
- Test (port)
- End.
At first, my stress_test client runs on Windows and the echo_server server runs on Linux. After accepting 1016 connections, I stopped. So I used ulimit-N to change the number of file descriptors on the server to 10240. Then, after a few days, I still didn't understand it.
So I turned to the company's Linux programming cool. The result showed me that the number of file descriptors was not modified on the client. On Windows, I changed the number in the registry.
The cool started to be interested in the performance of this item. I just touched the Erlang document for a while, so we went on the Performance Tuning journey of the Erlang network connection ~~ The process is really exciting. We soon passed the 1024 mark ~~ We were very excited when we reached 4999 connections.
But why are there 4999 connections? Check the code and find that echo_server.erl defines a macro with the maximum number of connections being 5000 ~~
After the compilation is modified, the number of connections reaches more than 101xx, which is too big!
When we test 102400 connections again, ERL will be suspended for 32767 connections ~ It is said that the process is too open. Fortunately, remember this ERL parameter + P, you can define the number of processes that Erlang can generate. The default value is 32768. Changed!
I don't know what to do later. The first connection has stopped. the new performance bottleneck has slowed us down again. fortunately, cool people are familiar with Linux, and use strace (which will exit inexplicably), STAP to find some signs. I also thought that I mentioned another limit in the OTP document, that is, the number of ports... at the same time, we found that Erlang uses the traditional poll model on Linux. however, the source code of Erlang is found to support epoll. after searching the Internet for half a day, I finally found a maillist post.
Code
- $./Configure -- enable-kernel-poll
Because our test server is dual-core, we also opened the SMP support during configuration. After the cheerful make & make install ....
Change the content of/proc/sys/NET/IPv4/ip_local_port_range to 1024 to 65535. The maximum value can also be changed to 65535.
Code
- $ Echo 1024 65535> ip_local_port_range
Add an ERL environment variable code.
- $ Export erl_max_ports = 102400
So I started to run, but this time it was different from echo_server.
- $ Erl-noshell + P 102400 + k true
+S 2 -smp -S echo_server start
Stress_test
- $ Erl-noshell + P 102400 + k true
+S 2 -smp -S stress_test start
+ K true here,Indicates using the kernel poll,+ S 2 indicates two cores.This is so cheerful ~~~ Over 10 million! It is faster than epoll ~~
So we started another 204800 connection tests ~~~
Use top 1 to check that the CPU usage is extremely low, and the server is only around 5%. The memory is not very large ~~