IO::AsyncX::SharedTimer - provides IO::Async timers which sacrifice accuracy for performance
version 0.001
# Needs to be added to a loop before you can # call any other methods my $loop = IO::Async::Loop->new; $loop->add( my $timer = IO::AsyncX::SharedTimer->new( # Combine timers into 50ms buckets, and # use cached value for ->now with 50ms expiry resolution => 0.050, ) ); # Report current time, accurate to ~50ms (defined # by the resolution parameter, as above) use feature qw(say); say "Time is roughly " . $timer->now; # Set a timeout for ~30s on an I/O operation Future->wait_any( $client->read_until(qr/\n/), $timer->timeout_future(after => 30) )->get;
This module provides various time-related utility methods for use with larger IO::Async applications.
In situations where you have many related timers - connection expiry, for example - there may be some overhead in having each of these in the timer priority queue as a separate event. Sometimes the exact trigger time is not so important, which is where this class comes in. You get to specify an accuracy, and all timers which would occur within the same window of time will be grouped together as a single timer event.
This may reduce calls to poll/epoll and timer management overhead, but please benchmark the difference before assuming that this module will be worth using - for some applications the overhead this introduces will outweigh any potential benefits.
One benchmark gave the following results for 1ms resolution across 100 timers set to rand(1) seconds:
Rate normal shared normal 32.9/s -- -50% shared 65.7/s 100% --
See the examples/ directory for code.
Change the current resolution.
Takes one named parameter:
resolution - the resolution for timers and "now", in seconds
Returns the current resolution.
Returns an approximation of the current time.
On first call, it will return (and cache) the value provided by the Time::Hires time function.
time
Subsequent calls will return this same value. The cached value expires after "resolution" seconds - note that the expiry happens via the event loop so if your code does not cede control back to the main event loop in a timely fashion, the cached value will not expire. Put another way: the value will be cached for at least "resolution" seconds.
There's a good chance that the method call overhead will incur a heavier performance impact than just calling Time::HiRes time directly. As always, profile and benchmark first.
Example usage:
my $start = $timer->now; $loop->run; my $elapsed = 1000.0 * ($timer->now - $start); say "Operation took about ${elapsed}ms to complete.";
Returns a Future which will resolve after approximately "resolution" seconds.
See the delay_future documentation in IO::Async::Loop for more details.
delay_future
Returns a Future which will fail after approximately "resolution" seconds.
add_child, adopt_future, can_event, children, configure_unknown, debug_printf, get_loop, invoke_error, invoke_event, loop, make_event_cb, maybe_invoke_event, maybe_make_event_cb, new, notifier_name, parent, remove_child, remove_from_parent
Tom Molesworth <cpan@perlsite.co.uk>
Copyright Tom Molesworth 2014-2015. Licensed under the same terms as Perl itself.
To install IO::AsyncX::SharedTimer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::AsyncX::SharedTimer
CPAN shell
perl -MCPAN -e shell install IO::AsyncX::SharedTimer
For more information on module installation, please visit the detailed CPAN module installation guide.