Skip to content

Instantly share code, notes, and snippets.

@azproduction
Created October 17, 2011 13:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azproduction/1292557 to your computer and use it in GitHub Desktop.
Save azproduction/1292557 to your computer and use it in GitHub Desktop.
Simple Google Dart HTTP Server
/**
* Simple HTTP server on Dart
* @see http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/runtime/bin/socket_impl.dart
* http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/runtime/bin/socket_stream.dart
*
* Based on http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/samples/socket/SocketExample.dart
* @author azproduction
*/
class HttpExample {
final String host = "127.0.0.1";
final int port = 50500;
ServerSocket serverSocket;
List receiveBuffer;
List<int> output;
HttpExample() {
output = "HTTP/1.1 200 OK\nServer:Dart\n\n<h1>Works!</h1>".charCodes();
}
void go() {
// initialize the server
serverSocket = new ServerSocket(host, port, 5);
if (serverSocket == null) {
throw "can't get server socket";
}
serverSocket.setConnectionHandler(onConnect);
print("accepting connections on ${host}:${port}");
}
void onSend() {
}
void onConnect() {
List<int> receiveBuffer = new List();
Socket receiveSocket = serverSocket.accept();
InputStream inputStream = receiveSocket.inputStream;
OutputStream outputStream = receiveSocket.outputStream;
inputStream.readUntil(receiveBuffer, bufferReady);
outputStream.write(output, 0, output.length, onSend);
receiveSocket.close();
}
// receive buffer has been filled
void bufferReady() {
}
}
main() {
new HttpExample().go();
}
@azproduction
Copy link
Author

ab test

Server Software:        Dart
Server Hostname:        127.0.0.1
Server Port:            50500

Document Path:          /
Document Length:        15 bytes

Concurrency Level:      4
Time taken for tests:   1.277 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      45000 bytes
HTML transferred:       15000 bytes
Requests per second:    782.88 [#/sec] (mean)
Time per request:       5.109 [ms] (mean)
Time per request:       1.277 [ms] (mean, across all concurrent requests)
Transfer rate:          34.40 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       2
Processing:     1    5   4.9      4      81
Waiting:        1    5   4.8      4      81
Total:          1    5   4.9      4      82

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

There is a huge chance to get an error "apr_socket_recv: Connection reset by peer (54)" I wonder, why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment