in reply to Re^2: sleep and timing?
in thread sleep and timing?
Then rather than trying to send for an exact time period and count how many you succeeded in transmitting, do it the other way around. Transmit a set number of traps and time how long it took. Set the number to send relatively high and divide that number by the number of seconds taken to arrive at an average throughput per second.
This is a simulation.
#! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; use Time::HiRes qw[ time sleep ]; use constant { THREADS => 10, TRAPS => 1000, }; my $Q = new Thread::Queue; my $start : shared = 0; my $running : shared = 0; sub thread { $running++; sleep 0.01 until $start; while( my $trap = $Q->dequeue ) { ## Simulate sending trap. sleep $trap; } $running--; } my @threads = map{ threads->create( \&thread ) } 1 .. THREADS; ## Random "transmit" times 10 to 40 milliseconds $Q->enqueue ( (10 + rand 40) / 1000 ) for 1 .. TRAPS; ## one undef per thread to terminate their while loops $Q->enqueue( (undef) x THREADS ); ## until they are all running sleep .1 until $running == THREADS; ## Get the start time and set them going my $startTime = time; $start = 1; ## Wait for the queue to empty sleep .01 while $Q->pending; ## Wait till the've all stopped sleep .01 while $running; ## Grab the end time my $endTime = time; print "1000 traps took and average of ", ( $endTime - $startTime ) / 1 +000, " seconds per trap"; __END__ P:\test>junk3 1000 traps took and average of 0.00372980403900147 seconds per trap
Although each "trap" delays at least 10 ms and an average of 30 millseconds, threading allows those delays to overlap, so the ultimate cost per trap is under 4 ms.
HTH
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: sleep and timing?
by Anonymous Monk on Nov 23, 2005 at 17:45 UTC |