factor/extra/benchmark/sockets/sockets.factor

74 lines
1.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel math threads io io.sockets
io.encodings.ascii io.streams.duplex debugger tools.time
prettyprint concurrency.count-downs concurrency.promises
namespaces arrays continuations destructors ;
2008-03-05 16:02:02 -05:00
IN: benchmark.sockets
SYMBOL: counter
SYMBOL: port-promise
SYMBOL: server
2008-03-05 16:02:02 -05:00
CONSTANT: number-of-requests 1000
2008-03-05 16:02:02 -05:00
: server-addr ( -- addr )
"127.0.0.1" port-promise get ?promise <inet4> ;
2008-03-05 16:02:02 -05:00
: server-loop ( server -- )
2008-05-07 06:14:25 -04:00
dup accept drop [
2008-03-05 16:02:02 -05:00
[
read1 CHAR: x = [
server get dispose
2008-03-05 16:02:02 -05:00
] [
number-of-requests
[ read1 write1 flush ] times
] if
] with-stream
] curry "Client handler" spawn drop server-loop ;
: simple-server ( -- )
[
"127.0.0.1" 0 <inet4> ascii <server>
[ server set ]
[ addr>> port>> port-promise get fulfill ]
[ [ server-loop ] with-disposal ]
tri
2008-03-05 16:02:02 -05:00
] ignore-errors ;
: simple-client ( -- )
2008-09-07 05:14:47 -04:00
[
server-addr ascii [
CHAR: b write1 flush
number-of-requests
[ CHAR: a dup write1 flush read1 assert= ] times
] with-client
] try
counter get count-down ;
2008-03-05 16:02:02 -05:00
: stop-server ( -- )
server-addr ascii [
2008-03-05 16:02:02 -05:00
CHAR: x write1
] with-client ;
2008-03-05 16:02:02 -05:00
: clients ( n -- )
dup pprint " clients: " write [
<promise> port-promise set
dup <count-down> counter set
2008-03-05 16:02:02 -05:00
[ simple-server ] "Simple server" spawn drop
yield yield
[ [ simple-client ] "Simple client" spawn drop ] times
counter get await
stop-server
yield yield
2008-09-07 05:14:47 -04:00
] benchmark . flush ;
2008-03-05 16:02:02 -05:00
2008-09-07 05:14:47 -04:00
: socket-benchmarks ( -- )
1 clients
10 clients
20 clients
40 clients
100 clients ;
2008-03-05 16:02:02 -05:00
MAIN: socket-benchmarks