1 ! Copyright (C) 2011 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays io io.encodings.binary io.servers
4 io.sockets kernel math memoize namespaces sequences fry literals
6 IN: benchmark.tcp-echo0
8 ! Max size here is 26 2^ 1 - because array-capacity limits on 32bit platforms
9 CONSTANT: test-size0 $[ 23 2^ 1 - ]
11 MEMO: test-bytes ( n -- byte-array ) iota >byte-array ;
13 TUPLE: tcp-echo < threaded-server #times #bytes ;
15 : <tcp-echo> ( #times #bytes -- obj )
16 binary \ tcp-echo new-threaded-server
19 <any-port-local-inet4> >>insecure ;
21 ERROR: incorrect-#bytes ;
23 : check-bytes ( bytes n -- bytes )
24 over length = [ incorrect-#bytes ] unless ;
26 : read-n ( n -- bytes )
27 [ read ] [ check-bytes ] bi ;
29 : read-write ( n -- ) read-n write flush ;
31 : write-read ( bytes -- )
32 [ write flush ] [ length read-n drop ] bi ;
34 M: tcp-echo handle-client*
35 [ #times>> ] [ #bytes>> ] bi
36 '[ _ [ _ test-bytes write-read ] times ] call ;
38 : server>address ( server -- port )
39 servers>> first addr>> port>> local-server ;
41 : tcp-echo-banner ( #times #bytes -- )
42 "Network testing: times: %d, length: %d\n" printf ;
44 :: tcp-echo-benchmark ( #times #bytes -- )
45 #times #bytes [ tcp-echo-banner ] 2keep
47 \ threaded-server get server>address binary [
48 #times [ #bytes read-write ] times
49 contents empty? [ incorrect-#bytes ] unless
51 ] with-threaded-server ;
53 : tcp-echo0-benchmark ( -- )
54 4 test-size0 tcp-echo-benchmark ;
56 MAIN: tcp-echo0-benchmark