# simple preforking echo server in Ruby
require 'socket'
# Create a socket, bind it to localhost:4242, and start listening.
# Runs once in the parent; all forked children inherit the socket's
# file descriptor.
acceptor = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
address = Socket.pack_sockaddr_in(3838, 'localhost')
acceptor.bind(address)
acceptor.listen(10)
# Close the socket when we exit the parent or any child process. This
# only closes the file descriptor in the calling process, it does not
# take the socket out of the listening state (until the last fd is
# closed).
#
# The trap is guaranteed to happen, and guaranteed to happen only
# once, right before the process exits for any reason (unless
# it's terminated with a SIGKILL).
trap('EXIT') { acceptor.close }
# Fork you some child processes. In the parent, the call to fork
# returns immediately with the pid of the child process; fork never
# returns in the child because we exit at the end of the block.
3.times do
fork do
# now we're in the child process; trap (Ctrl-C) interrupts and
# exit immediately instead of dumping stack to stderr.
trap('INT') { exit }
puts "child #$$ accepting on shared socket (localhost:4242)"
loop {
# This is where the magic happens. accept(2) blocks until a
# new connection is ready to be dequeued.
socket, addr = acceptor.accept
socket.write "child #$$ echo> "
socket.flush
message = socket.gets
socket.write message
socket.close
puts "child #$$ echo'd: '#{message.strip}'"
}
exit
end
end
# Trap (Ctrl-C) interrupts, write a note, and exit immediately
# in parent. This trap is not inherited by the forks because it
# runs after forking has commenced.
trap('INT') { puts "\nbailing" ; exit }
# Sit back and wait for all child processes to exit.
Process.waitall
=begin
$ ab -n 100000 -c 5 http://localhost:3838/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: localhost
Server Port: 3838
Document Path: /
Document Length: 0 bytes
Concurrency Level: 5
Time taken for tests: 3.682 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 3200000 bytes
HTML transferred: 0 bytes
Requests per second: 27159.44 [#/sec] (mean)
Time per request: 0.184 [ms] (mean)
Time per request: 0.037 [ms] (mean, across all concurrent requests)
Transfer rate: 848.73 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 0 0 0.3 0 9
Waiting: 0 0 0.1 0 3
Total: 0 0 0.3 0 9
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 0
99% 1
100% 9 (longest request)
=end
from gevent import wsgi, monkey
class WebServer(object):
def application(self, environ, start_response):
start_response("200 OK", [])
return ["hello world"]
if __name__ == "__main__":
monkey.patch_all()
app = WebServer()
wsgi.WSGIServer(('', 3838), app.application, log=None).serve_forever()
"""
ab -n 100000 -c 5 http://localhost:3838/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: gevent/0.13
Server Hostname: localhost
Server Port: 3838
Document Path: /
Document Length: 12 bytes
Concurrency Level: 5
Time taken for tests: 8.183 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 14700000 bytes
HTML transferred: 1200000 bytes
Requests per second: 12220.75 [#/sec] (mean)
Time per request: 0.409 [ms] (mean)
Time per request: 0.082 [ms] (mean, across all concurrent requests)
Transfer rate: 1754.35 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 0 0.1 0 4
Waiting: 0 0 0.1 0 4
Total: 0 0 0.1 0 4
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 0
99% 1
100% 4 (longest request)
"""
package main
import "net/http"
import "runtime"
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
handler := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world\n"))
}
http.ListenAndServe(":3838", http.HandlerFunc(handler))
}
/*
$ ab -n 100000 -c 5 http://localhost:3838/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: localhost
Server Port: 3838
Document Path: /
Document Length: 12 bytes
Concurrency Level: 5
Time taken for tests: 6.985 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 10900000 bytes
HTML transferred: 1200000 bytes
Requests per second: 14316.85 [#/sec] (mean)
Time per request: 0.349 [ms] (mean)
Time per request: 0.070 [ms] (mean, across all concurrent requests)
Transfer rate: 1523.96 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 0 0 0.3 0 11
Waiting: 0 0 0.3 0 11
Total: 0 0 0.3 0 11
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 1
95% 1
98% 1
99% 2
100% 11 (longest request)
*/
cluster = require "cluster"
http = require "http"
numCPUs = require("os").cpus().length
if cluster.isMaster
i = 0
while i < numCPUs
cluster.fork()
i++
cluster.on 'exit', (worker, code, signal) ->
console.log "worker" + worker.pid + "died"
else
http.createServer((req, res) ->
res.writeHead 200
res.end "hello world"
).listen(3838)
###
$ ab -n 100000 -c 5 http://localhost:3838/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
^[[A^[[A^[[ACompleted 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: localhost
Server Port: 3838
Document Path: /
Document Length: 11 bytes
Concurrency Level: 5
Time taken for tests: 5.381 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 8600000 bytes
HTML transferred: 1100000 bytes
Requests per second: 18584.68 [#/sec] (mean)
Time per request: 0.269 [ms] (mean)
Time per request: 0.054 [ms] (mean, across all concurrent requests)
Transfer rate: 1560.82 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 3
Processing: 0 0 0.3 0 22
Waiting: 0 0 0.2 0 17
Total: 0 0 0.3 0 22
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 1
99% 1
100% 22 (longest request)
###
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types (status200)
import Network.Wai.Handler.Warp (run)
application _ = return $
responseLBS status200 [] "hello world"
main = run 3838 application
{-
$ ghc --make -threaded -O2 ParallelHello.hs && ./ParallelHello +RTS -N3
$ ab -n 100000 -c 5 http://localhost:3838/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: Warp/1.3.9
Server Hostname: localhost
Server Port: 3838
Document Path: /
Document Length: 11 bytes
Concurrency Level: 5
Time taken for tests: 8.865 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 5000000 bytes
HTML transferred: 1100000 bytes
Requests per second: 11280.55 [#/sec] (mean)
Time per request: 0.443 [ms] (mean)
Time per request: 0.089 [ms] (mean, across all concurrent requests)
Transfer rate: 550.81 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 0 0 0.6 0 33
Waiting: 0 0 0.6 0 32
Total: 0 0 0.6 0 33
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 1
98% 3
99% 4
100% 33 (longest request)
-}