How to rate-limit a pipe under linux?

63

16

Is there a filter which I could use to rate-limit a pipe on linux? If this exists, let call it rate-limit, I want to be able to type in a terminal something like

cat /dev/urandom | rate-limit 3 -k | foo

in order to send a a stream of random bytes to foo's standard input at a rate (lower than) 3 kbytes/s.

Frédéric Grosshans

Posted 2010-03-17T17:37:19.017

Reputation: 1 035

1I asked here because I want to use it in a programme, not for troubleshooting. But it's my very first question here so I apologize if I made an error. – None – 2010-03-17T17:54:05.530

1BTW, the above is an unnecessary use of cat, you could do rate-limit 3k < /dev/urandom | foo. – dmckee --- ex-moderator kitten – 2010-03-17T18:15:25.820

Answers

92

Pipe Viewer has this feature.

cat /dev/urandom | pv -L 3k | foo

Juliano

Posted 2010-03-17T17:37:19.017

Reputation: 2 492

1Also handy to use while watching output of a job, with -q... e.g: command 2>&1 | pv -q -L 3k – Attie – 2017-10-19T12:00:39.140

17

I'd say that Juliano has got the right answer if you have that tool, but I'd also suggest that this is a neat little K&R style exercise: just write a specialized version of cat that reads one character at a time from stdin, outputs each to stdout and then usleeps before moving on. Be sure to unbuffer the standard output, or this will run rather jerkily.

I called this slowcat.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char**argv){
  int c;
  useconds_t stime=10000; // defaults to 100 Hz

  if (argc>1) { // Argument is interperted as Hz
    stime=1000000/atoi(argv[1]);
  }

  setvbuf(stdout,NULL,_IONBF,0);

  while ((c=fgetc(stdin)) != EOF){
    fputc(c,stdout);
    usleep(stime);
  }

  return 0;
}

Compile it and try with

$ ./slowcat 10 < slowcat.c

dmckee --- ex-moderator kitten

Posted 2010-03-17T17:37:19.017

Reputation: 7 311

I'd use that with one of those retro terminal screensavers for a giggle if I had speakers attached to my linux boxes. – Benny Mackney – 2016-10-04T23:07:48.537

18Now I'm feeling the horrible temptation to add a "clack" noise to each character an set the default speed to 40 CPS, with an extra delay for newlines. – dmckee --- ex-moderator kitten – 2010-03-17T19:19:07.600

5

throttle seems designed specifically for this. e.g.

cat /dev/urandom | throttle -k 3 | foo

Ponkadoodle

Posted 2010-03-17T17:37:19.017

Reputation: 319

1I would agree with this, but there doesn't seem to be a standard package for Ubuntu. I also am not seeing the source code for it either. Anyone know where you can get this utility? – Benjamin Leinweber – 2017-02-17T16:08:47.873

1

@BenjaminLeinweber It looks like the website that normally hosts the code is offline, but freebsd has the source for 1.2 available on their ftp server. You would have to download it there & ./configure && make.

– Ponkadoodle – 2017-02-17T21:34:16.423

I added the source to github in case someone want to change stuff to make it compile on newer machines.

– Marcelo Roberto Jimenez – 2020-02-12T20:12:05.563

3

Here's an all-shell solution that won't lose input (cf. the head -1 idea from Mike S):

hexdump -c /dev/urandom | (lines=0; while read line; do echo $line; if [ $((++lines % 10)) -eq 0 ]; then sleep 1; fi; done) | cat -n

BrianHoltz

Posted 2010-03-17T17:37:19.017

Reputation: 31

2

Quick and dirty, all shell all the time (I have CentOS and pv doesn't come with it by default):

hexdump -c /dev/urandom | while true; do head -1; sleep 1; done | \
your_program_that_reads_stdin  -your_argument_list

...dirty because although it's rate limited, it's bursty at a rate that I can't tell you :-) . But it's handy when you just need to send data from one place to the other (I was doing some socat tests) and you don't want to swamp your screen with garbage.

Mike S

Posted 2010-03-17T17:37:19.017

Reputation: 166