<div class="gmail_quote">On Wed, Jan 12, 2011 at 5:32 PM, Robert McKay <span dir="ltr"><<a href="mailto:robert@mckay.com">robert@mckay.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="gmail_quote"><div class="im">On Wed, Jan 12, 2011 at 4:33 PM, - Tethys <span dir="ltr"><<a href="mailto:tethys@gmail.com" target="_blank">tethys@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Does anyone know of a simple delay utility that consumes data from<br>
stdin and outputs it on stdout a fixed time later? What I want to be<br>
able to do is:<br>
<br>
mkdata | delay 30 | send_data_to_customer<br>
<br>
That will send the (time sensitive) output of mkdata to the customer<br>
with a 30 second delay. The amount of data I'm talking about isn't so<br>
large that I need to worry about memory usage, and buffering it in RAM<br>
will work just fine. I can write it myself, but I'd rather not<br>
reinvent the wheel if someone's already done it.<br></blockquote></div><div><br>You could pipe it through a series of netcats such that the latency along the path is 30 seconds. 50 roundtrips to japan should do it. ;-)<br>
<br>It's sortof an interesting problem as to do it properly you really need to timestamp every read().<br><br>You could perhaps create artificial latency using netem perhaps making the nc idea almost practical.<br><br>
Here's some crappy perl I made that may work. I think.<br><br></div></div></blockquote></div><br>A better version that exits on EOF properly.<br><pre>#!/usr/bin/perl -w<br>#<br># A simple latency pipe. Configure $delay to suit your needs.<br>
# reads from STDIN, adds latency, writes to STDOUT<br>#<br># Usage: something | <a href="http://this.pl">this.pl</a><br>#<br># Test it:<br>#<br># while true; do <br># sleep $(( $RANDOM%5 )); <br># date +%s; <br># done | ./<a href="http://this.pl">this.pl</a> | perl -ane ' $now = `date +%s`;\<br>
# chomp($now); print "now ($now) - then ($F[0]) = ", $now - $F[0], "\n"; '<br><br>use strict;<br>use IO::Select;<br>use IO::Handle;<br><br># how long to delay reads<br>my $delay = 30;<br>my $bufsize = 1024;<br>
<br><br>my @chunks = ();<br>my $reading = 1;<br>my $chunk;<br><br>my $out = IO::Handle->new();<br>$out->fdopen(fileno(\*STDOUT),"w");<br><br>my $s = IO::Select->new();<br>$s->add(\*STDIN);<br><br>while( ($reading)||(scalar(@chunks)>0) )<br>
{<br><br> if ($s->can_read(0.01) )<br> {<br> if (sysread(STDIN, $chunk, $bufsize)!=0)<br> {<br> push @chunks, { time=>time(), chunk=>$chunk };<br> }<br> else <br> {<br># a read event happened, but read returned 0 bytes. this means EOF<br>
$reading = 0;<br> }<br> }<br><br> if (my $test = shift(@chunks))<br> {<br><br> if ($test->{time} <= (time()-$delay) )<br> {<br> print $out $test->{chunk};<br> $out->flush();<br> } else { unshift(@chunks, $test); }<br>
<br> }<br><br>}<br></pre><br>rm@rat:~$ dd if=/dev/urandom of=test.file count=10000 bs=1024<br>10000+0 records in<br>10000+0 records out<br>rm@rat:~$ time perl <a href="http://latency.pl">latency.pl</a> < test.file > test.file2<br>
<br>real 0m30.266s<br>user 0m12.490s<br>sys 0m2.510s<br>rm@rat:~$ md5sum test.file test.file2<br>43a8d122da31ed1c852bd3e7bd50cc32 test.file<br>43a8d122da31ed1c852bd3e7bd50cc32 test.file2<br><br>rm@rat:~$ while true; do sleep $(( $RANDOM%5 )); date +%s; done | ./<a href="http://latency.pl">latency.pl</a> | perl -ane ' $now = `date +%s`; chomp($now); print "now ($now) - then ($F[0]) = ", $now - $F[0], "\n"; '<br>
now (1294885976) - then (1294885946) = 30<br>now (1294885979) - then (1294885949) = 30<br>now (1294885981) - then (1294885951) = 30<br>now (1294885982) - then (1294885952) = 30<br>now (1294885982) - then (1294885952) = 30<br>
now (1294885986) - then (1294885956) = 30<br>^C<br>rm@rat:~$<br><br>there's also Kosta's python solution from the last time;<br><br><a href="http://lists.gllug.org.uk/pipermail/gllug/2010-April/083052.html">http://lists.gllug.org.uk/pipermail/gllug/2010-April/083052.html</a><br>
<br><br>Rob<br>