]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/sockets/sockets.factor
Fixing everything for mandatory stack effects
[factor.git] / extra / benchmark / sockets / sockets.factor
1 USING: io.sockets io kernel math threads io.encodings.ascii
2 io.streams.duplex debugger tools.time prettyprint
3 concurrency.count-downs namespaces arrays continuations
4 destructors ;
5 IN: benchmark.sockets
6
7 SYMBOL: counter
8
9 : number-of-requests 1 ;
10
11 : server-addr ( -- addr ) "127.0.0.1" 7777 <inet4> ;
12
13 : server-loop ( server -- )
14     dup accept drop [
15         [
16             read1 CHAR: x = [
17                 "server" get dispose
18             ] [
19                 number-of-requests
20                 [ read1 write1 flush ] times
21                 counter get count-down
22             ] if
23         ] with-stream
24     ] curry "Client handler" spawn drop server-loop ;
25
26 : simple-server ( -- )
27     [
28         server-addr ascii <server> dup "server" set [
29             server-loop
30         ] with-disposal
31     ] ignore-errors ;
32
33 : simple-client ( -- )
34     server-addr ascii [
35         CHAR: b write1 flush
36         number-of-requests
37         [ CHAR: a dup write1 flush read1 assert= ] times
38         counter get count-down
39     ] with-client ;
40
41 : stop-server ( -- )
42     server-addr ascii [
43         CHAR: x write1
44     ] with-client ;
45
46 : clients ( n -- )
47     dup pprint " clients: " write [
48         dup 2 * <count-down> counter set
49         [ simple-server ] "Simple server" spawn drop
50         yield yield
51         [ [ simple-client ] "Simple client" spawn drop ] times
52         counter get await
53         stop-server
54         yield yield
55     ] time ;
56
57 : socket-benchmarks ;
58
59 MAIN: socket-benchmarks