#lang racket (provide bytes/sec->data-rate-string bits/sec->data-rate-string bytes/msec->binary-rate-string bytes/msec->binary-rate-string* bytes/sec->binary-rate-string bytes/sec->binary-rate-string* bytes/msec->data-rate-string) (define (convert b factor decimals) (real->decimal-string (/ b factor) decimals)) (define (bytes/sec->binary-rate-string* byte sec [decimals 1]) (define b (if (> byte 0) (/ byte sec) byte)) (cond ((< b 1024) (string-append (number->string b) "B/s")) ((and (>= b 1024) (< b 1048576)) (string-append (convert b 1024 decimals) " KB/s")) ((and (>= b 1048576) (< b 1073741824)) (string-append (convert b 1048576 decimals) " MB/s")) ((and (>= b 1073741824) (< b 1099511627776)) (string-append (convert b 1073741824 decimals) " GB/s")) ((and (>= b 1099511627776) (< b 1125899906842624) (string-append (convert b 1099511627776 decimals) " TB/s"))) ((and (>= b 1125899906842624) (< b 1152921504606846976)) (string-append (convert b 1125899906842624 decimals) " PB/s")) ((and (>= b 1152921504606846976) (< b 1180591620717411303424)) (string-append (convert b 1152921504606846976 decimals) " EB/s")) ((and (>= b 1180591620717411303424) (< b 1208925819614629174706176)) (string-append (convert b 1180591620717411303424 decimals) " ZB/s")) (else (string-append (convert b 1208925819614629174706176 decimals) " YB/s")))) (define (bytes/sec->binary-rate-string byte sec [decimals 1]) (define b (if (> byte 0) (/ byte sec) byte)) (cond ((< b 1024) (values b "B/s")) ((and (>= b 1024) (< b 1048576)) (string-append (convert b 1024 decimals) " KiB/s")) ((and (>= b 1048576) (< b 1073741824)) (string-append (convert b 1048576 decimals) " MiB/s")) ((and (>= b 1073741824) (< b 1099511627776)) (string-append (convert b 1073741824 decimals) " GiB/s")) ((and (>= b 1099511627776) (< b 1125899906842624) (string-append (convert b 1099511627776 decimals) " TiB/s"))) ((and (>= b 1125899906842624) (< b 1152921504606846976)) (string-append (convert b 1125899906842624 decimals) " PiB/s")) ((and (>= b 1152921504606846976) (< b 1180591620717411303424)) (string-append (convert b 1152921504606846976 decimals) " EiB/s")) ((and (>= b 1180591620717411303424) (< b 1208925819614629174706176)) (string-append (convert b 1180591620717411303424 decimals) " ZiB/s")) (else (string-append (convert b 1208925819614629174706176 decimals) " YiB/s")))) (define (bytes/msec->binary-rate-string* b msec [decimals 1]) (bytes/sec->binary-rate-string* b (/ msec 1000) decimals)) (define (bytes/msec->binary-rate-string b msec [decimals 1]) (bytes/sec->binary-rate-string b (/ msec 1000) decimals)) (define (bits/sec->data-rate-string bit sec [decimals 1]) (define b (if (> 0 sec) (/ bit sec) bit)) (cond ((< b 1000) (string-append (number->string b) " bit/s")) ((and (>= b 1000) (< b 1000000)) (string-append (convert b 1000 decimals) " kbit/s")) ((and (>= b 1000000) (< b 1000000000)) (string-append (convert b 1000000 decimals) " Mbit/s")) ((and (>= b 1000000000) (< b 1000000000000)) (string-append (convert b 1000000000 decimals) " Gbit/s")) (else (string-append (convert b 1000000000000 decimals) " Tbit/s")))) (define (bytes/sec->data-rate-string b sec [decimals 1]) (bits/sec->data-rate-string (* b 8) sec decimals)) (define (bytes/msec->data-rate-string b msec [decimals 1]) (bytes/sec->data-rate-string b (/ msec 1000) decimals))