| 
									
										
										
										
											2011-10-15 13:52:07 -04:00
										 |  |  | ! Copyright (C) 2011 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: accessors byte-arrays io io.encodings.binary io.servers | 
					
						
							|  |  |  | io.sockets kernel math memoize namespaces sequences fry literals | 
					
						
							|  |  |  | locals formatting ;
 | 
					
						
							|  |  |  | IN: benchmark.tcp-echo0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Max size here is 26 2^ 1 - because array-capacity limits on 32bit platforms | 
					
						
							| 
									
										
										
										
											2011-10-19 04:17:08 -04:00
										 |  |  | CONSTANT: test-size0 $[ 23 2^ 1 - ] | 
					
						
							| 
									
										
										
										
											2011-10-15 13:52:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 17:59:35 -04:00
										 |  |  | MEMO: test-bytes ( n -- byte-array ) <iota> >byte-array ;
 | 
					
						
							| 
									
										
										
										
											2011-10-15 13:52:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: tcp-echo < threaded-server #times #bytes ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <tcp-echo> ( #times #bytes -- obj )
 | 
					
						
							|  |  |  |     binary \ tcp-echo new-threaded-server | 
					
						
							|  |  |  |         swap >>#bytes | 
					
						
							|  |  |  |         swap >>#times | 
					
						
							| 
									
										
										
										
											2016-03-08 05:24:09 -05:00
										 |  |  |         <any-port-local-inet4> >>insecure ;
 | 
					
						
							| 
									
										
										
										
											2012-07-19 20:35:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-15 13:52:07 -04:00
										 |  |  | ERROR: incorrect-#bytes ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : check-bytes ( bytes n -- bytes )
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     over length = [ incorrect-#bytes ] unless ;
 | 
					
						
							| 
									
										
										
										
											2011-10-15 13:52:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : read-n ( n -- bytes )
 | 
					
						
							|  |  |  |     [ read ] [ check-bytes ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : read-write ( n -- ) read-n write flush ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : write-read ( bytes -- )
 | 
					
						
							|  |  |  |     [ write flush ] [ length read-n drop ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: tcp-echo handle-client* | 
					
						
							|  |  |  |     [ #times>> ] [ #bytes>> ] bi
 | 
					
						
							|  |  |  |     '[ _ [ _ test-bytes write-read ] times ] call ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : server>address ( server -- port )
 | 
					
						
							|  |  |  |     servers>> first addr>> port>> local-server ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tcp-echo-banner ( #times #bytes -- )
 | 
					
						
							|  |  |  |     "Network testing: times: %d, length: %d\n" printf ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: tcp-echo-benchmark ( #times #bytes -- )
 | 
					
						
							|  |  |  |     #times #bytes [ tcp-echo-banner ] 2keep
 | 
					
						
							|  |  |  |     <tcp-echo> [ | 
					
						
							|  |  |  |         \ threaded-server get server>address binary [ | 
					
						
							|  |  |  |             #times [ #bytes read-write ] times
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |             contents empty? [ incorrect-#bytes ] unless
 | 
					
						
							| 
									
										
										
										
											2011-10-15 13:52:07 -04:00
										 |  |  |         ] with-client | 
					
						
							|  |  |  |     ] with-threaded-server ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-19 20:35:47 -04:00
										 |  |  | : tcp-echo0-benchmark ( -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-15 13:52:07 -04:00
										 |  |  |     4 test-size0 tcp-echo-benchmark ;
 | 
					
						
							| 
									
										
										
										
											2012-07-19 20:35:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MAIN: tcp-echo0-benchmark |