factor/extra/benchmark/sockets/sockets.factor

66 lines
1.6 KiB
Factor
Raw Normal View History

USING: io.sockets io kernel math threads io.encodings.ascii
2008-03-01 15:23:41 -05:00
debugger tools.time prettyprint concurrency.count-downs
namespaces arrays continuations ;
2008-02-11 16:53:42 -05:00
IN: benchmark.sockets
2008-03-01 15:23:41 -05:00
SYMBOL: counter
: number-of-requests 1 ;
: server-addr "127.0.0.1" 7777 <inet4> ;
: server-loop ( server -- )
dup accept [
[
read1 CHAR: x = [
"server" get dispose
] [
number-of-requests
[ read1 write1 flush ] times
counter get count-down
] if
] with-stream
] curry "Client handler" spawn drop server-loop ;
2008-02-11 16:53:42 -05:00
: simple-server ( -- )
2008-03-01 15:23:41 -05:00
[
2008-03-04 18:52:32 -05:00
server-addr ascii <server> dup "server" set [
2008-03-01 15:23:41 -05:00
server-loop
] with-disposal
] ignore-errors ;
2008-02-11 16:53:42 -05:00
: simple-client ( -- )
2008-03-01 15:23:41 -05:00
server-addr <client> [
2008-02-11 17:10:11 -05:00
CHAR: b write1 flush
2008-03-01 15:23:41 -05:00
number-of-requests
[ CHAR: a dup write1 flush read1 assert= ] times
counter get count-down
2008-02-11 17:10:11 -05:00
] with-stream ;
: stop-server ( -- )
2008-03-01 15:23:41 -05:00
server-addr <client> [
2008-02-11 17:10:11 -05:00
CHAR: x write1
2008-02-11 16:53:42 -05:00
] with-stream ;
2008-02-14 06:21:02 -05:00
: clients ( n -- )
2008-02-16 19:48:06 -05:00
dup pprint " clients: " write [
2008-03-01 15:23:41 -05:00
dup 2 * <count-down> counter set
[ simple-server ] "Simple server" spawn drop
2008-02-16 19:48:06 -05:00
yield yield
2008-03-01 15:23:41 -05:00
[ [ simple-client ] "Simple client" spawn drop ] times
counter get await
2008-02-11 17:10:11 -05:00
stop-server
yield yield
] time ;
2008-02-11 16:53:42 -05:00
: socket-benchmarks
2008-02-14 06:21:02 -05:00
10 clients
20 clients
2008-02-23 23:48:45 -05:00
40 clients ;
! 80 clients
! 160 clients
! 320 clients
! 640 clients ;
2008-02-11 16:53:42 -05:00
MAIN: socket-benchmarks