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


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: sleep and timing? by BrowserUk
in thread sleep and timing? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.