Last active
December 15, 2017 14:39
-
-
Save tonyg/4520234 to your computer and use it in GitHub Desktop.
Soak-testing Racket's UDP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require racket/udp) | |
(define count 0) | |
(define s (udp-open-socket #f #f)) | |
(udp-bind! s #f 12346) | |
(define buffer (make-bytes 1024)) | |
(let loop ((start-time #f)) | |
(sync (udp-receive!-evt s buffer)) | |
(udp-send-to s "127.0.0.1" 12345 #"pong") | |
(set! count (+ count 1)) | |
(let ((start-time (or start-time (current-inexact-milliseconds)))) | |
(when (zero? (modulo count 100)) | |
(display "\r") | |
(let* ((now (current-inexact-milliseconds)) | |
(delta (- now start-time)) | |
(rate (if (zero? delta) 0 (/ count (/ delta 1000))))) | |
(display (list 'count count | |
'runtime (/ (truncate delta) 1000) | |
'rate (truncate rate) | |
'stamp now)))) | |
(loop start-time))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require racket/udp) | |
(define sent-count 0) | |
(define received-count 0) | |
(define s (udp-open-socket #f #f)) | |
(udp-bind! s #f 12345) | |
(define buffer (make-bytes 1024)) | |
(define start-time (current-inexact-milliseconds)) | |
(define last-recv-time #f) | |
(let loop () | |
(udp-send-to s "127.0.0.1" 12346 #"ping") | |
(set! sent-count (+ sent-count 1)) | |
(let inner-loop () | |
(sync (handle-evt (udp-receive!-evt s buffer) | |
(lambda (_) | |
(set! last-recv-time (current-inexact-milliseconds)) | |
(set! received-count (+ received-count 1)) | |
(inner-loop))) | |
always-evt)) | |
(when (zero? (modulo sent-count 100)) | |
(display "\r") | |
(let* ((delta (- (current-inexact-milliseconds) start-time)) | |
(send-rate (if (zero? delta) 0 (/ sent-count (/ delta 1000))))) | |
(display (list 'out sent-count | |
'in received-count | |
'runtime (/ (truncate delta) 1000) | |
'rate (truncate send-rate) | |
'stamp last-recv-time)))) | |
(loop)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment