最近一直在讀《java并發(fā)編程實(shí)踐》,書(shū)是絕對(duì)的好書(shū),翻譯不能說(shuō)差,也談不上好,特別是第一部分的前面幾章,有的地方翻譯的南轅北轍了,還是要對(duì)照著英文版來(lái)看。我關(guān)注并發(fā)編程是從學(xué)習(xí)Erlang開(kāi)始的,在多核來(lái)臨的時(shí)代,有人說(shuō)并發(fā)將是下一個(gè)10年的關(guān)鍵技術(shù)。java5之前的多線程編程很復(fù)雜,況且我也沒(méi)有從事此類應(yīng)用的開(kāi)發(fā),了解不多,而從jdk5引入了讓人流口水的concurrent包之后,java的并發(fā)編程開(kāi)始變的有趣起來(lái)。
書(shū)中第6章以編寫(xiě)一個(gè)web server為例子,引出了幾種不同版本的寫(xiě)法:?jiǎn)尉€程、多線程以及采用jdk5提供的線程池實(shí)現(xiàn)。我就用apache自帶的ab工具測(cè)試了下各個(gè)版本的性能,在redhat9 p4 2g內(nèi)存的機(jī)器上進(jìn)行了測(cè)試。
ab -n 50000 -c 1000 http://localhost/index.html >benchmark
單線程模式,順序性地處理每一個(gè)請(qǐng)求,50000并發(fā)很快就沒(méi)有響應(yīng)了,不參與比較了。再來(lái)看看我們自己寫(xiě)的多線程方式處理每個(gè)請(qǐng)求:
package net.rubyeye.concurrency.chapter6;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ThreadPerTaskWebServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(80);
while (true) {
final Socket connection = server.accept();
Runnable task = new Runnable() {
public void run() {
try {
handleRequest(connection);
} catch (IOException e) {
e.printStackTrace();
}
}
};
new Thread(task).start();
}
}
public static void handleRequest(Socket socket) throws IOException {
try {
InetAddress client = socket.getInetAddress();
// and print it to gui
s(client.getHostName() + " connected to server.\n");
// Read the http request from the client from the socket interface
// into a buffer.
BufferedReader input = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// Prepare a outputstream from us to the client,
// this will be used sending back our response
// (header + requested file) to the client.
DataOutputStream output = new DataOutputStream(socket
.getOutputStream());
// as the name suggest this method handles the http request, see
// further down.
// abstraction rules
http_handler(input, output);
socket.close();
} catch (Exception e) { // catch any errors, and print them
s("\nError:" + e.getMessage());
}
} // go back in loop, wait for next request
// our implementation of the hypertext transfer protocol
// its very basic and stripped down
private static void http_handler(BufferedReader input,
DataOutputStream output) {
int method = 0; // 1 get, 2 head, 0 not supported
String http = new String(); // a bunch of strings to hold
String path = new String(); // the various things, what http v, what
// path,
String file = new String(); // what file
String user_agent = new String(); // what user_agent
try {
// This is the two types of request we can handle
// GET /index.html HTTP/1.0
// HEAD /index.html HTTP/1.0
String tmp = input.readLine(); // read from the stream
String tmp2 = new String(tmp);
tmp.toUpperCase(); // convert it to uppercase
if (tmp.startsWith("GET")) { // compare it is it GET
method = 1;
} // if we set it to method 1
if (tmp.startsWith("HEAD")) { // same here is it HEAD
method = 2;
} // set method to 2
if (method == 0) { // not supported
try {
output.writeBytes(construct_http_header(501, 0));
output.close();
return;
} catch (Exception e3) { // if some error happened catch it
s("error:" + e3.getMessage());
} // and display error
}
// }
// tmp contains "GET /index.html HTTP/1.0 
."
// find first space
// find next space
// copy whats between minus slash, then you get "index.html"
// it's a bit of dirty code, but bear with me
int start = 0;
int end = 0;
for (int a = 0; a < tmp2.length(); a++) {
if (tmp2.charAt(a) == ' ' && start != 0) {
end = a;
break;
}
if (tmp2.charAt(a) == ' ' && start == 0) {
start = a;
}
}
path = tmp2.substring(start + 2, end); // fill in the path
} catch (Exception e) {
s("errorr" + e.getMessage());
} // catch any exception
// path do now have the filename to what to the file it wants to open
s("\nClient requested:" + new File(path).getAbsolutePath() + "\n");
FileInputStream requestedfile = null;
try {
// NOTE that there are several security consideration when passing
// the untrusted string "path" to FileInputStream.
// You can access all files the current user has read access to!!!
// current user is the user running the javaprogram.
// you can do this by passing "../" in the url or specify absoulute
// path
// or change drive (win)
// try to open the file,
requestedfile = new FileInputStream(path);
} catch (Exception e) {
try {
// if you could not open the file send a 404
output.writeBytes(construct_http_header(404, 0));
// close the stream
output.close();
} catch (Exception e2) {
}
;
s("error" + e.getMessage());
} // print error to gui
// happy day scenario
try {
int type_is = 0;
// find out what the filename ends with,
// so you can construct a the right content type
if (path.endsWith(".zip") || path.endsWith(".exe")
|| path.endsWith(".tar")) {
type_is = 3;
}
if (path.endsWith(".jpg") || path.endsWith(".jpeg")) {
type_is = 1;
}
if (path.endsWith(".gif")) {
type_is = 2;
// write out the header, 200 ->everything is ok we are all
// happy.
}
output.writeBytes(construct_http_header(200, 5));
// if it was a HEAD request, we don't print any BODY
if (method == 1) { // 1 is GET 2 is head and skips the body
while (true) {
// read the file from filestream, and print out through the
// client-outputstream on a byte per byte base.
int b = requestedfile.read();
if (b == -1) {
break; // end of file
}
output.write(b);
}
}
// clean up the files, close open handles
output.close();
requestedfile.close();
}
catch (Exception e) {
}
}
private static void s(String s) {
// System.out.println(s);
}
// this method makes the HTTP header for the response
// the headers job is to tell the browser the result of the request
// among if it was successful or not.
private static String construct_http_header(int return_code, int file_type) {
String s = "HTTP/1.0 ";
// you probably have seen these if you have been surfing the web a while
switch (return_code) {
case 200:
s = s + "200 OK";
break;
case 400:
s = s + "400 Bad Request";
break;
case 403:
s = s + "403 Forbidden";
break;
case 404:
s = s + "404 Not Found";
break;
case 500:
s = s + "500 Internal Server Error";
break;
case 501:
s = s + "501 Not Implemented";
break;
}
s = s + "\r\n"; // other header fields,
s = s + "Connection: close\r\n"; // we can't handle persistent
// connections
s = s + "Server: SimpleHTTPtutorial v0\r\n"; // server name
// Construct the right Content-Type for the header.
// This is so the browser knows what to do with the
// file, you may know the browser dosen't look on the file
// extension, it is the servers job to let the browser know
// what kind of file is being transmitted. You may have experienced
// if the server is miss configured it may result in
// pictures displayed as text!
switch (file_type) {
// plenty of types for you to fill in
case 0:
break;
case 1:
s = s + "Content-Type: image/jpeg\r\n";
break;
case 2:
s = s + "Content-Type: image/gif\r\n";
case 3:
s = s + "Content-Type: application/x-zip-compressed\r\n";
default:
s = s + "Content-Type: text/html\r\n";
break;
}
// //so on and so on

s = s + "\r\n"; // this marks the end of the httpheader
// and the start of the body
// ok return our newly created header!
return s;
}
}
測(cè)試結(jié)果如下:
Concurrency Level: 1000
Time taken for tests: 111.869356 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 4950000 bytes
HTML transferred: 250000 bytes
Requests per second: 446.95 [#/sec] (mean)
Time per request: 2237.387 [ms] (mean)
Time per request: 2.237 [ms] (mean, across all concurrent requests)
Transfer rate: 43.20 [Kbytes/sec] received
修改下上面的程序,采用jdk5提供的線程池:
private static final int NTHREADS = 5;
private static Executor exec;
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(80);
if (args.length == 0)
exec = Executors.newFixedThreadPool(NTHREADS);
else
exec = Executors.newFixedThreadPool(Integer.parseInt(args[0]));
while (true) {
final Socket connection = server.accept();
Runnable task = new Runnable() {
public void run() {
try {
handleRequest(connection);
} catch (IOException e) {
e.printStackTrace();
}
}
};
exec.execute(task);
}
}
默認(rèn)線程池大小取5,后經(jīng)過(guò)反復(fù)測(cè)試,線程池大小在5左右,測(cè)試結(jié)果達(dá)到最佳。測(cè)試采用線程池的結(jié)果如下:
Concurrency Level: 1000
Time taken for tests: 51.648142 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 4978908 bytes
HTML transferred: 251460 bytes
Requests per second: 968.09 [#/sec] (mean)
Time per request: 1032.963 [ms] (mean)
Time per request: 1.033 [ms] (mean, across all concurrent requests)
Transfer rate: 94.14 [Kbytes/sec] received
與上面結(jié)果一比較,牛人寫(xiě)的線程池終究是大大不一樣。當(dāng)連接數(shù)增加到10W以上,兩個(gè)版本之間的性能差異就更明顯了。這里采用的是固定線程池,如果采用緩沖線程池會(huì)怎么樣呢?newFixedThreadPool改為newCachedThreadPool方法,測(cè)試可以發(fā)現(xiàn)結(jié)果與固定線程池的最佳結(jié)果相似。CachedThreadPool更適合此處短連接、高并發(fā)的場(chǎng)景。后來(lái),我想Erlang寫(xiě)一個(gè)簡(jiǎn)單的web server,性能上會(huì)不會(huì)超過(guò)采用線程池的這個(gè)版本呢?試試:
%% httpd.erl - MicroHttpd
-module(httpd).
-export([start/0,start/1,start/2,process/2]).
-import(regexp,[split/2]).
-define(defPort,80).
-define(docRoot,".").
start() -> start(?defPort,?docRoot).
start(Port) -> start(Port,?docRoot).
start(Port,DocRoot) ->
case gen_tcp:listen(Port, [binary,{packet, 0},{active, false}]) of
{ok, LSock} ->
server_loop(LSock,DocRoot);
{error, Reason} ->
exit({Port,Reason})
end.
%% main server loop - wait for next connection, spawn child to process it
server_loop(LSock,DocRoot) ->
case gen_tcp:accept(LSock) of
{ok, Sock} ->
spawn(?MODULE,process,[Sock,DocRoot]),
server_loop(LSock,DocRoot);
{error, Reason} ->
exit({accept,Reason})
end.
%% process current connection
process(Sock,DocRoot) ->
Req = do_recv(Sock),
{ok,[Cmd|[Name|[Vers|_]]]} = split(Req,"[ \r\n]"),
FileName = DocRoot ++ Name,
LogReq = Cmd ++ " " ++ Name ++ " " ++ Vers,
Resp = case file:read_file(FileName) of
{ok, Data} ->
io:format("~p ~p ok~n",[LogReq,FileName]),
Data;
{error, Reason} ->
io:format("~p ~p failed ~p~n",[LogReq,FileName,Reason]),
error_response(LogReq,file:format_error(Reason))
end,
do_send(Sock,Resp),
gen_tcp:close(Sock).
%% construct HTML for failure message
error_response(LogReq,Reason) ->
"<html><head><title>Request Failed</title></head><body>\n" ++
"<h1>Request Failed</h1>\n" ++
"Your request to " ++ LogReq ++
" failed due to: " ++ Reason ++ "\n</body></html>\n"
.
%% send a line of text to the
do_send(Sock,Msg) ->
case gen_tcp:send(Sock, Msg) of
ok ->
ok;
{error, Reason} ->
exit(Reason)
end.
%% receive data from the socket
do_recv(Sock) ->
case gen_tcp:recv(Sock, 0) of
{ok, Bin} ->
binary_to_list(Bin);
{error, closed} ->
exit(closed);
{error, Reason} ->
exit(Reason)
end.
執(zhí)行:
erl -noshell +P 5000 -s httpd start
+P參數(shù)是將系統(tǒng)允許創(chuàng)建的process數(shù)目增加到50000,默認(rèn)是3萬(wàn)多。測(cè)試結(jié)果:
Concurrency Level: 1000
Time taken for tests: 106.35735 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 250000 bytes
HTML transferred: 0 bytes
Requests per second: 471.54 [#/sec] (mean)
Time per request: 2120.715 [ms] (mean)
Time per request: 2.121 [ms] (mean, across all concurrent requests)
Transfer rate: 2.30 [Kbytes/sec] received
結(jié)果讓人大失所望,這個(gè)結(jié)果與我們自己寫(xiě)的多線程java版本差不多,與采用線程池的版本就差多了,減少并發(fā)的話,倒是比java版本的快點(diǎn)。側(cè)面驗(yàn)證了
這個(gè)討論的結(jié)論:
erlang的優(yōu)勢(shì)就是高并發(fā)而非高性能。當(dāng)然,這三者都比不上C語(yǔ)言寫(xiě)的多線程web server。測(cè)試了unix/linux編程實(shí)踐中的例子,速度是遠(yuǎn)遠(yuǎn)超過(guò)前三者,不過(guò)支持的并發(fā)有限,因?yàn)橄到y(tǒng)創(chuàng)建的線程在超過(guò)5000時(shí)就崩潰了。如果采用jdk5進(jìn)行開(kāi)發(fā),應(yīng)當(dāng)充分利用新的并發(fā)包,可惜我們公司還停留在1.4。