<commit_before>USING: io.sockets io.server io kernel math threads debugger
concurrency tools.time prettyprint ;
IN: benchmark.sockets

: simple-server ( -- )
    7777 local-server "simple-server" [
        10 [ read1 write1 flush ] times
    ] with-server ;

: simple-client ( -- )
    "localhost" 7777 <inet> <client> [
        10 [ CHAR: a dup write1 flush read1 assert= ] times
    ] with-stream ;

: socket-benchmark ( n -- )
    dup pprint " clients: " write
    [ simple-server ] in-thread
    yield yield
    [ drop simple-client ] parallel-each ;

: socket-benchmarks
    [ 10 socket-benchmark ] time
    [ 20 socket-benchmark ] time
    [ 40 socket-benchmark ] time
    [ 80 socket-benchmark ] time
    [ 160 socket-benchmark ] time
    [ 320 socket-benchmark ] time ;

MAIN: socket-benchmarks<commit_msg>Improved sockets benchmark<commit_after>USING: io.sockets io.server io kernel math threads debugger
concurrency tools.time prettyprint ;
IN: benchmark.sockets

: simple-server ( -- )
    7777 local-server "benchmark.sockets" [
        read1 CHAR: x = [
            stop-server
        ] [
            20 [ read1 write1 flush ] times
        ] if
    ] with-server ;

: simple-client ( -- )
    "localhost" 7777 <inet> <client> [
        CHAR: b write1 flush
        20 [ CHAR: a dup write1 flush read1 assert= ] times
    ] with-stream ;

: stop-server ( -- )
    "localhost" 7777 <inet> <client> [
        CHAR: x write1
    ] with-stream ;

: socket-benchmark ( n -- )
    dup pprint " clients: " write
        [
        [ simple-server ] in-thread
        100 sleep
        [ drop simple-client ] parallel-each
        stop-server
        yield yield
    ] time ;

: socket-benchmarks
    10 socket-benchmark
    20 socket-benchmark
    40 socket-benchmark
    80 socket-benchmark
    160 socket-benchmark
    320 socket-benchmark ;

MAIN: socket-benchmarks
{'fraction_non_alphanumeric': 0.10138740661686232, 'fraction_numerical': 0.03735325506937033, 'mean_word_length': 2.2665505226480835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1673667', 'n_tokens_mistral': 622, 'n_tokens_neox': 570, 'n_words': 161}