1 | NAME |
1 | NAME |
2 | AnyEvent - provide framework for multiple event loops |
2 | AnyEvent - provide framework for multiple event loops |
3 | |
3 | |
4 | EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl - various supported |
4 | EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl, Event::Lib, Qt, POE - |
5 | event loops |
5 | various supported event loops |
6 | |
6 | |
7 | SYNOPSIS |
7 | SYNOPSIS |
8 | use AnyEvent; |
8 | use AnyEvent; |
9 | |
9 | |
10 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { |
10 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { |
… | |
… | |
76 | module. |
76 | module. |
77 | |
77 | |
78 | During the first call of any watcher-creation method, the module tries |
78 | During the first call of any watcher-creation method, the module tries |
79 | to detect the currently loaded event loop by probing whether one of the |
79 | to detect the currently loaded event loop by probing whether one of the |
80 | following modules is already loaded: Coro::EV, Coro::Event, EV, Event, |
80 | following modules is already loaded: Coro::EV, Coro::Event, EV, Event, |
81 | Glib, Tk. The first one found is used. If none are found, the module |
81 | Glib, AnyEvent::Impl::Perl, Tk, Event::Lib, Qt, POE. The first one found |
82 | tries to load these modules in the stated order. The first one that can |
82 | is used. If none are found, the module tries to load these modules |
|
|
83 | (excluding Tk, Event::Lib, Qt and POE as the pure perl adaptor should |
|
|
84 | always succeed) in the order given. The first one that can be |
83 | be successfully loaded will be used. If, after this, still none could be |
85 | successfully loaded will be used. If, after this, still none could be |
84 | found, AnyEvent will fall back to a pure-perl event loop, which is not |
86 | found, AnyEvent will fall back to a pure-perl event loop, which is not |
85 | very efficient, but should work everywhere. |
87 | very efficient, but should work everywhere. |
86 | |
88 | |
87 | Because AnyEvent first checks for modules that are already loaded, |
89 | Because AnyEvent first checks for modules that are already loaded, |
88 | loading an event model explicitly before first using AnyEvent will |
90 | loading an event model explicitly before first using AnyEvent will |
… | |
… | |
129 | |
131 | |
130 | Note that "my $w; $w =" combination. This is necessary because in Perl, |
132 | Note that "my $w; $w =" combination. This is necessary because in Perl, |
131 | my variables are only visible after the statement in which they are |
133 | my variables are only visible after the statement in which they are |
132 | declared. |
134 | declared. |
133 | |
135 | |
134 | IO WATCHERS |
136 | I/O WATCHERS |
135 | You can create an I/O watcher by calling the "AnyEvent->io" method with |
137 | You can create an I/O watcher by calling the "AnyEvent->io" method with |
136 | the following mandatory key-value pairs as arguments: |
138 | the following mandatory key-value pairs as arguments: |
137 | |
139 | |
138 | "fh" the Perl *file handle* (*not* file descriptor) to watch for events. |
140 | "fh" the Perl *file handle* (*not* file descriptor) to watch for events. |
139 | "poll" must be a string that is either "r" or "w", which creates a |
141 | "poll" must be a string that is either "r" or "w", which creates a |
140 | watcher waiting for "r"eadable or "w"ritable events, respectively. "cb" |
142 | watcher waiting for "r"eadable or "w"ritable events, respectively. "cb" |
141 | is the callback to invoke each time the file handle becomes ready. |
143 | is the callback to invoke each time the file handle becomes ready. |
142 | |
144 | |
143 | File handles will be kept alive, so as long as the watcher exists, the |
145 | Although the callback might get passed parameters, their value and |
144 | file handle exists, too. |
146 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
147 | callbacks cannot use arguments passed to I/O watcher callbacks. |
145 | |
148 | |
|
|
149 | The I/O watcher might use the underlying file descriptor or a copy of |
146 | It is not allowed to close a file handle as long as any watcher is |
150 | it. You must not close a file handle as long as any watcher is active on |
147 | active on the underlying file descriptor. |
151 | the underlying file descriptor. |
148 | |
152 | |
149 | Some event loops issue spurious readyness notifications, so you should |
153 | Some event loops issue spurious readyness notifications, so you should |
150 | always use non-blocking calls when reading/writing from/to your file |
154 | always use non-blocking calls when reading/writing from/to your file |
151 | handles. |
155 | handles. |
152 | |
156 | |
… | |
… | |
162 | TIME WATCHERS |
166 | TIME WATCHERS |
163 | You can create a time watcher by calling the "AnyEvent->timer" method |
167 | You can create a time watcher by calling the "AnyEvent->timer" method |
164 | with the following mandatory arguments: |
168 | with the following mandatory arguments: |
165 | |
169 | |
166 | "after" specifies after how many seconds (fractional values are |
170 | "after" specifies after how many seconds (fractional values are |
167 | supported) should the timer activate. "cb" the callback to invoke in |
171 | supported) the callback should be invoked. "cb" is the callback to |
168 | that case. |
172 | invoke in that case. |
|
|
173 | |
|
|
174 | Although the callback might get passed parameters, their value and |
|
|
175 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
176 | callbacks cannot use arguments passed to time watcher callbacks. |
169 | |
177 | |
170 | The timer callback will be invoked at most once: if you want a repeating |
178 | The timer callback will be invoked at most once: if you want a repeating |
171 | timer you have to create a new watcher (this is a limitation by both Tk |
179 | timer you have to create a new watcher (this is a limitation by both Tk |
172 | and Glib). |
180 | and Glib). |
173 | |
181 | |
… | |
… | |
200 | o'clock"). |
208 | o'clock"). |
201 | |
209 | |
202 | While most event loops expect timers to specified in a relative way, |
210 | While most event loops expect timers to specified in a relative way, |
203 | they use absolute time internally. This makes a difference when your |
211 | they use absolute time internally. This makes a difference when your |
204 | clock "jumps", for example, when ntp decides to set your clock backwards |
212 | clock "jumps", for example, when ntp decides to set your clock backwards |
205 | from the wrong 2014-01-01 to 2008-01-01, a watcher that you created to |
213 | from the wrong date of 2014-01-01 to 2008-01-01, a watcher that is |
206 | fire "after" a second might actually take six years to finally fire. |
214 | supposed to fire "after" a second might actually take six years to |
|
|
215 | finally fire. |
207 | |
216 | |
208 | AnyEvent cannot compensate for this. The only event loop that is |
217 | AnyEvent cannot compensate for this. The only event loop that is |
209 | conscious about these issues is EV, which offers both relative |
218 | conscious about these issues is EV, which offers both relative |
210 | (ev_timer) and absolute (ev_periodic) timers. |
219 | (ev_timer, based on true relative time) and absolute (ev_periodic, based |
|
|
220 | on wallclock time) timers. |
211 | |
221 | |
212 | AnyEvent always prefers relative timers, if available, matching the |
222 | AnyEvent always prefers relative timers, if available, matching the |
213 | AnyEvent API. |
223 | AnyEvent API. |
214 | |
224 | |
215 | SIGNAL WATCHERS |
225 | SIGNAL WATCHERS |
216 | You can watch for signals using a signal watcher, "signal" is the signal |
226 | You can watch for signals using a signal watcher, "signal" is the signal |
217 | *name* without any "SIG" prefix, "cb" is the Perl callback to be invoked |
227 | *name* without any "SIG" prefix, "cb" is the Perl callback to be invoked |
218 | whenever a signal occurs. |
228 | whenever a signal occurs. |
219 | |
229 | |
|
|
230 | Although the callback might get passed parameters, their value and |
|
|
231 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
232 | callbacks cannot use arguments passed to signal watcher callbacks. |
|
|
233 | |
220 | Multiple signals occurances can be clumped together into one callback |
234 | Multiple signal occurances can be clumped together into one callback |
221 | invocation, and callback invocation will be synchronous. synchronous |
235 | invocation, and callback invocation will be synchronous. synchronous |
222 | means that it might take a while until the signal gets handled by the |
236 | means that it might take a while until the signal gets handled by the |
223 | process, but it is guarenteed not to interrupt any other callbacks. |
237 | process, but it is guarenteed not to interrupt any other callbacks. |
224 | |
238 | |
225 | The main advantage of using these watchers is that you can share a |
239 | The main advantage of using these watchers is that you can share a |
… | |
… | |
237 | |
251 | |
238 | The child process is specified by the "pid" argument (if set to 0, it |
252 | The child process is specified by the "pid" argument (if set to 0, it |
239 | watches for any child process exit). The watcher will trigger as often |
253 | watches for any child process exit). The watcher will trigger as often |
240 | as status change for the child are received. This works by installing a |
254 | as status change for the child are received. This works by installing a |
241 | signal handler for "SIGCHLD". The callback will be called with the pid |
255 | signal handler for "SIGCHLD". The callback will be called with the pid |
242 | and exit status (as returned by waitpid). |
256 | and exit status (as returned by waitpid), so unlike other watcher types, |
|
|
257 | you *can* rely on child watcher callback arguments. |
243 | |
258 | |
244 | Example: wait for pid 1333 |
259 | There is a slight catch to child watchers, however: you usually start |
|
|
260 | them *after* the child process was created, and this means the process |
|
|
261 | could have exited already (and no SIGCHLD will be sent anymore). |
|
|
262 | |
|
|
263 | Not all event models handle this correctly (POE doesn't), but even for |
|
|
264 | event models that *do* handle this correctly, they usually need to be |
|
|
265 | loaded before the process exits (i.e. before you fork in the first |
|
|
266 | place). |
|
|
267 | |
|
|
268 | This means you cannot create a child watcher as the very first thing in |
|
|
269 | an AnyEvent program, you *have* to create at least one watcher before |
|
|
270 | you "fork" the child (alternatively, you can call "AnyEvent::detect"). |
|
|
271 | |
|
|
272 | Example: fork a process and wait for it |
|
|
273 | |
|
|
274 | my $done = AnyEvent->condvar; |
|
|
275 | |
|
|
276 | AnyEvent::detect; # force event module to be initialised |
|
|
277 | |
|
|
278 | my $pid = fork or exit 5; |
245 | |
279 | |
246 | my $w = AnyEvent->child ( |
280 | my $w = AnyEvent->child ( |
247 | pid => 1333, |
281 | pid => $pid, |
248 | cb => sub { |
282 | cb => sub { |
249 | my ($pid, $status) = @_; |
283 | my ($pid, $status) = @_; |
250 | warn "pid $pid exited with status $status"; |
284 | warn "pid $pid exited with status $status"; |
|
|
285 | $done->broadcast; |
251 | }, |
286 | }, |
252 | ); |
287 | ); |
|
|
288 | |
|
|
289 | # do something else, then wait for process exit |
|
|
290 | $done->wait; |
253 | |
291 | |
254 | CONDITION VARIABLES |
292 | CONDITION VARIABLES |
255 | Condition variables can be created by calling the "AnyEvent->condvar" |
293 | Condition variables can be created by calling the "AnyEvent->condvar" |
256 | method without any arguments. |
294 | method without any arguments. |
257 | |
295 | |
… | |
… | |
332 | |
370 | |
333 | The known classes so far are: |
371 | The known classes so far are: |
334 | |
372 | |
335 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
373 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
336 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
374 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
337 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
375 | AnyEvent::Impl::EV based on EV (an interface to libev, best choice). |
338 | AnyEvent::Impl::Event based on Event, also second best choice :) |
376 | AnyEvent::Impl::Event based on Event, second best choice. |
339 | AnyEvent::Impl::Glib based on Glib, third-best choice. |
377 | AnyEvent::Impl::Glib based on Glib, third-best choice. |
|
|
378 | AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable. |
340 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
379 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
341 | AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable. |
380 | AnyEvent::Impl::Qt based on Qt, cannot be autoprobed (see its docs). |
|
|
381 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
|
|
382 | AnyEvent::Impl::POE based on POE, not generic enough for full support. |
|
|
383 | |
|
|
384 | There is no support for WxWidgets, as WxWidgets has no support for |
|
|
385 | watching file handles. However, you can use WxWidgets through the |
|
|
386 | POE Adaptor, as POE has a Wx backend that simply polls 20 times per |
|
|
387 | second, which was considered to be too horrible to even consider for |
|
|
388 | AnyEvent. Likewise, other POE backends can be used by AnyEvent by |
|
|
389 | using it's adaptor. |
|
|
390 | |
|
|
391 | AnyEvent knows about Prima and Wx and will try to use POE when |
|
|
392 | autodetecting them. |
342 | |
393 | |
343 | AnyEvent::detect |
394 | AnyEvent::detect |
344 | Returns $AnyEvent::MODEL, forcing autodetection of the event model |
395 | Returns $AnyEvent::MODEL, forcing autodetection of the event model |
345 | if necessary. You should only call this function right before you |
396 | if necessary. You should only call this function right before you |
346 | would have created an AnyEvent watcher anyway, that is, as late as |
397 | would have created an AnyEvent watcher anyway, that is, as late as |
… | |
… | |
385 | |
436 | |
386 | You can chose to use a rather inefficient pure-perl implementation by |
437 | You can chose to use a rather inefficient pure-perl implementation by |
387 | loading the "AnyEvent::Impl::Perl" module, which gives you similar |
438 | loading the "AnyEvent::Impl::Perl" module, which gives you similar |
388 | behaviour everywhere, but letting AnyEvent chose is generally better. |
439 | behaviour everywhere, but letting AnyEvent chose is generally better. |
389 | |
440 | |
|
|
441 | OTHER MODULES |
|
|
442 | The following is a non-exhaustive list of additional modules that use |
|
|
443 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
|
|
444 | in the same program. Some of the modules come with AnyEvent, some are |
|
|
445 | available via CPAN. |
|
|
446 | |
|
|
447 | AnyEvent::Util |
|
|
448 | Contains various utility functions that replace often-used but |
|
|
449 | blocking functions such as "inet_aton" by event-/callback-based |
|
|
450 | versions. |
|
|
451 | |
|
|
452 | AnyEvent::Handle |
|
|
453 | Provide read and write buffers and manages watchers for reads and |
|
|
454 | writes. |
|
|
455 | |
|
|
456 | AnyEvent::Socket |
|
|
457 | Provides a means to do non-blocking connects, accepts etc. |
|
|
458 | |
|
|
459 | AnyEvent::HTTPD |
|
|
460 | Provides a simple web application server framework. |
|
|
461 | |
|
|
462 | AnyEvent::DNS |
|
|
463 | Provides asynchronous DNS resolver capabilities, beyond what |
|
|
464 | AnyEvent::Util offers. |
|
|
465 | |
|
|
466 | AnyEvent::FastPing |
|
|
467 | The fastest ping in the west. |
|
|
468 | |
|
|
469 | Net::IRC3 |
|
|
470 | AnyEvent based IRC client module family. |
|
|
471 | |
|
|
472 | Net::XMPP2 |
|
|
473 | AnyEvent based XMPP (Jabber protocol) module family. |
|
|
474 | |
|
|
475 | Net::FCP |
|
|
476 | AnyEvent-based implementation of the Freenet Client Protocol, |
|
|
477 | birthplace of AnyEvent. |
|
|
478 | |
|
|
479 | Event::ExecFlow |
|
|
480 | High level API for event-based execution flow control. |
|
|
481 | |
|
|
482 | Coro |
|
|
483 | Has special support for AnyEvent. |
|
|
484 | |
|
|
485 | IO::Lambda |
|
|
486 | The lambda approach to I/O - don't ask, look there. Can use |
|
|
487 | AnyEvent. |
|
|
488 | |
|
|
489 | IO::AIO |
|
|
490 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
491 | programmer. Can be trivially made to use AnyEvent. |
|
|
492 | |
|
|
493 | BDB Truly asynchronous Berkeley DB access. Can be trivially made to use |
|
|
494 | AnyEvent. |
|
|
495 | |
390 | SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
496 | SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
391 | This is an advanced topic that you do not normally need to use AnyEvent |
497 | This is an advanced topic that you do not normally need to use AnyEvent |
392 | in a module. This section is only of use to event loop authors who want |
498 | in a module. This section is only of use to event loop authors who want |
393 | to provide AnyEvent compatibility. |
499 | to provide AnyEvent compatibility. |
394 | |
500 | |
… | |
… | |
431 | must not be done in an interactive application, so it makes sense. |
537 | must not be done in an interactive application, so it makes sense. |
432 | |
538 | |
433 | ENVIRONMENT VARIABLES |
539 | ENVIRONMENT VARIABLES |
434 | The following environment variables are used by this module: |
540 | The following environment variables are used by this module: |
435 | |
541 | |
436 | "PERL_ANYEVENT_VERBOSE" when set to 2 or higher, cause AnyEvent to |
542 | "PERL_ANYEVENT_VERBOSE" |
437 | report to STDERR which event model it chooses. |
543 | By default, AnyEvent will be completely silent except in fatal |
|
|
544 | conditions. You can set this environment variable to make AnyEvent |
|
|
545 | more talkative. |
|
|
546 | |
|
|
547 | When set to 1 or higher, causes AnyEvent to warn about unexpected |
|
|
548 | conditions, such as not being able to load the event model specified |
|
|
549 | by "PERL_ANYEVENT_MODEL". |
|
|
550 | |
|
|
551 | When set to 2 or higher, cause AnyEvent to report to STDERR which |
|
|
552 | event model it chooses. |
|
|
553 | |
|
|
554 | "PERL_ANYEVENT_MODEL" |
|
|
555 | This can be used to specify the event model to be used by AnyEvent, |
|
|
556 | before autodetection and -probing kicks in. It must be a string |
|
|
557 | consisting entirely of ASCII letters. The string "AnyEvent::Impl::" |
|
|
558 | gets prepended and the resulting module name is loaded and if the |
|
|
559 | load was successful, used as event model. If it fails to load |
|
|
560 | AnyEvent will proceed with autodetection and -probing. |
|
|
561 | |
|
|
562 | This functionality might change in future versions. |
|
|
563 | |
|
|
564 | For example, to force the pure perl model (AnyEvent::Impl::Perl) you |
|
|
565 | could start your program like this: |
|
|
566 | |
|
|
567 | PERL_ANYEVENT_MODEL=Perl perl ... |
438 | |
568 | |
439 | EXAMPLE PROGRAM |
569 | EXAMPLE PROGRAM |
440 | The following program uses an IO watcher to read data from STDIN, a |
570 | The following program uses an I/O watcher to read data from STDIN, a |
441 | timer to display a message once per second, and a condition variable to |
571 | timer to display a message once per second, and a condition variable to |
442 | quit the program when the user enters quit: |
572 | quit the program when the user enters quit: |
443 | |
573 | |
444 | use AnyEvent; |
574 | use AnyEvent; |
445 | |
575 | |
… | |
… | |
588 | $quit->broadcast; |
718 | $quit->broadcast; |
589 | }); |
719 | }); |
590 | |
720 | |
591 | $quit->wait; |
721 | $quit->wait; |
592 | |
722 | |
|
|
723 | BENCHMARKS |
|
|
724 | To give you an idea of the performance and overheads that AnyEvent adds |
|
|
725 | over the event loops themselves and to give you an impression of the |
|
|
726 | speed of various event loops I prepared some benchmarks. |
|
|
727 | |
|
|
728 | BENCHMARKING ANYEVENT OVERHEAD |
|
|
729 | Here is a benchmark of various supported event models used natively and |
|
|
730 | through anyevent. The benchmark creates a lot of timers (with a zero |
|
|
731 | timeout) and I/O watchers (watching STDOUT, a pty, to become writable, |
|
|
732 | which it is), lets them fire exactly once and destroys them again. |
|
|
733 | |
|
|
734 | Source code for this benchmark is found as eg/bench in the AnyEvent |
|
|
735 | distribution. |
|
|
736 | |
|
|
737 | Explanation of the columns |
|
|
738 | *watcher* is the number of event watchers created/destroyed. Since |
|
|
739 | different event models feature vastly different performances, each event |
|
|
740 | loop was given a number of watchers so that overall runtime is |
|
|
741 | acceptable and similar between tested event loop (and keep them from |
|
|
742 | crashing): Glib would probably take thousands of years if asked to |
|
|
743 | process the same number of watchers as EV in this benchmark. |
|
|
744 | |
|
|
745 | *bytes* is the number of bytes (as measured by the resident set size, |
|
|
746 | RSS) consumed by each watcher. This method of measuring captures both C |
|
|
747 | and Perl-based overheads. |
|
|
748 | |
|
|
749 | *create* is the time, in microseconds (millionths of seconds), that it |
|
|
750 | takes to create a single watcher. The callback is a closure shared |
|
|
751 | between all watchers, to avoid adding memory overhead. That means |
|
|
752 | closure creation and memory usage is not included in the figures. |
|
|
753 | |
|
|
754 | *invoke* is the time, in microseconds, used to invoke a simple callback. |
|
|
755 | The callback simply counts down a Perl variable and after it was invoked |
|
|
756 | "watcher" times, it would "->broadcast" a condvar once to signal the end |
|
|
757 | of this phase. |
|
|
758 | |
|
|
759 | *destroy* is the time, in microseconds, that it takes to destroy a |
|
|
760 | single watcher. |
|
|
761 | |
|
|
762 | Results |
|
|
763 | name watchers bytes create invoke destroy comment |
|
|
764 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
|
|
765 | EV/Any 100000 244 2.50 0.46 0.29 EV + AnyEvent watchers |
|
|
766 | CoroEV/Any 100000 244 2.49 0.44 0.29 coroutines + Coro::Signal |
|
|
767 | Perl/Any 100000 513 4.92 0.87 1.12 pure perl implementation |
|
|
768 | Event/Event 16000 516 31.88 31.30 0.85 Event native interface |
|
|
769 | Event/Any 16000 590 35.75 31.42 1.08 Event + AnyEvent watchers |
|
|
770 | Glib/Any 16000 1357 98.22 12.41 54.00 quadratic behaviour |
|
|
771 | Tk/Any 2000 1860 26.97 67.98 14.00 SEGV with >> 2000 watchers |
|
|
772 | POE/Event 2000 6644 108.64 736.02 14.73 via POE::Loop::Event |
|
|
773 | POE/Select 2000 6343 94.13 809.12 565.96 via POE::Loop::Select |
|
|
774 | |
|
|
775 | Discussion |
|
|
776 | The benchmark does *not* measure scalability of the event loop very |
|
|
777 | well. For example, a select-based event loop (such as the pure perl one) |
|
|
778 | can never compete with an event loop that uses epoll when the number of |
|
|
779 | file descriptors grows high. In this benchmark, all events become ready |
|
|
780 | at the same time, so select/poll-based implementations get an unnatural |
|
|
781 | speed boost. |
|
|
782 | |
|
|
783 | Also, note that the number of watchers usually has a nonlinear effect on |
|
|
784 | overall speed, that is, creating twice as many watchers doesn't take |
|
|
785 | twice the time - usually it takes longer. This puts event loops tested |
|
|
786 | with a higher number of watchers at a disadvantage. |
|
|
787 | |
|
|
788 | To put the range of results into perspective, consider that on the |
|
|
789 | benchmark machine, handling an event takes roughly 1600 CPU cycles with |
|
|
790 | EV, 3100 CPU cycles with AnyEvent's pure perl loop and almost 3000000 |
|
|
791 | CPU cycles with POE. |
|
|
792 | |
|
|
793 | "EV" is the sole leader regarding speed and memory use, which are both |
|
|
794 | maximal/minimal, respectively. Even when going through AnyEvent, it uses |
|
|
795 | far less memory than any other event loop and is still faster than Event |
|
|
796 | natively. |
|
|
797 | |
|
|
798 | The pure perl implementation is hit in a few sweet spots (both the |
|
|
799 | constant timeout and the use of a single fd hit optimisations in the |
|
|
800 | perl interpreter and the backend itself). Nevertheless this shows that |
|
|
801 | it adds very little overhead in itself. Like any select-based backend |
|
|
802 | its performance becomes really bad with lots of file descriptors (and |
|
|
803 | few of them active), of course, but this was not subject of this |
|
|
804 | benchmark. |
|
|
805 | |
|
|
806 | The "Event" module has a relatively high setup and callback invocation |
|
|
807 | cost, but overall scores in on the third place. |
|
|
808 | |
|
|
809 | "Glib"'s memory usage is quite a bit higher, but it features a faster |
|
|
810 | callback invocation and overall ends up in the same class as "Event". |
|
|
811 | However, Glib scales extremely badly, doubling the number of watchers |
|
|
812 | increases the processing time by more than a factor of four, making it |
|
|
813 | completely unusable when using larger numbers of watchers (note that |
|
|
814 | only a single file descriptor was used in the benchmark, so |
|
|
815 | inefficiencies of "poll" do not account for this). |
|
|
816 | |
|
|
817 | The "Tk" adaptor works relatively well. The fact that it crashes with |
|
|
818 | more than 2000 watchers is a big setback, however, as correctness takes |
|
|
819 | precedence over speed. Nevertheless, its performance is surprising, as |
|
|
820 | the file descriptor is dup()ed for each watcher. This shows that the |
|
|
821 | dup() employed by some adaptors is not a big performance issue (it does |
|
|
822 | incur a hidden memory cost inside the kernel which is not reflected in |
|
|
823 | the figures above). |
|
|
824 | |
|
|
825 | "POE", regardless of underlying event loop (whether using its pure perl |
|
|
826 | select-based backend or the Event module, the POE-EV backend couldn't be |
|
|
827 | tested because it wasn't working) shows abysmal performance and memory |
|
|
828 | usage: Watchers use almost 30 times as much memory as EV watchers, and |
|
|
829 | 10 times as much memory as Event (the high memory requirements are |
|
|
830 | caused by requiring a session for each watcher). Watcher invocation |
|
|
831 | speed is almost 900 times slower than with AnyEvent's pure perl |
|
|
832 | implementation. The design of the POE adaptor class in AnyEvent can not |
|
|
833 | really account for this, as session creation overhead is small compared |
|
|
834 | to execution of the state machine, which is coded pretty optimally |
|
|
835 | within AnyEvent::Impl::POE. POE simply seems to be abysmally slow. |
|
|
836 | |
|
|
837 | Summary |
|
|
838 | * Using EV through AnyEvent is faster than any other event loop (even |
|
|
839 | when used without AnyEvent), but most event loops have acceptable |
|
|
840 | performance with or without AnyEvent. |
|
|
841 | |
|
|
842 | * The overhead AnyEvent adds is usually much smaller than the overhead |
|
|
843 | of the actual event loop, only with extremely fast event loops such |
|
|
844 | as EV adds AnyEvent significant overhead. |
|
|
845 | |
|
|
846 | * You should avoid POE like the plague if you want performance or |
|
|
847 | reasonable memory usage. |
|
|
848 | |
|
|
849 | BENCHMARKING THE LARGE SERVER CASE |
|
|
850 | This benchmark atcually benchmarks the event loop itself. It works by |
|
|
851 | creating a number of "servers": each server consists of a socketpair, a |
|
|
852 | timeout watcher that gets reset on activity (but never fires), and an |
|
|
853 | I/O watcher waiting for input on one side of the socket. Each time the |
|
|
854 | socket watcher reads a byte it will write that byte to a random other |
|
|
855 | "server". |
|
|
856 | |
|
|
857 | The effect is that there will be a lot of I/O watchers, only part of |
|
|
858 | which are active at any one point (so there is a constant number of |
|
|
859 | active fds for each loop iterstaion, but which fds these are is random). |
|
|
860 | The timeout is reset each time something is read because that reflects |
|
|
861 | how most timeouts work (and puts extra pressure on the event loops). |
|
|
862 | |
|
|
863 | In this benchmark, we use 10000 socketpairs (20000 sockets), of which |
|
|
864 | 100 (1%) are active. This mirrors the activity of large servers with |
|
|
865 | many connections, most of which are idle at any one point in time. |
|
|
866 | |
|
|
867 | Source code for this benchmark is found as eg/bench2 in the AnyEvent |
|
|
868 | distribution. |
|
|
869 | |
|
|
870 | Explanation of the columns |
|
|
871 | *sockets* is the number of sockets, and twice the number of "servers" |
|
|
872 | (as each server has a read and write socket end). |
|
|
873 | |
|
|
874 | *create* is the time it takes to create a socketpair (which is |
|
|
875 | nontrivial) and two watchers: an I/O watcher and a timeout watcher. |
|
|
876 | |
|
|
877 | *request*, the most important value, is the time it takes to handle a |
|
|
878 | single "request", that is, reading the token from the pipe and |
|
|
879 | forwarding it to another server. This includes deleting the old timeout |
|
|
880 | and creating a new one that moves the timeout into the future. |
|
|
881 | |
|
|
882 | Results |
|
|
883 | name sockets create request |
|
|
884 | EV 20000 69.01 11.16 |
|
|
885 | Perl 20000 73.32 35.87 |
|
|
886 | Event 20000 212.62 257.32 |
|
|
887 | Glib 20000 651.16 1896.30 |
|
|
888 | POE 20000 349.67 12317.24 uses POE::Loop::Event |
|
|
889 | |
|
|
890 | Discussion |
|
|
891 | This benchmark *does* measure scalability and overall performance of the |
|
|
892 | particular event loop. |
|
|
893 | |
|
|
894 | EV is again fastest. Since it is using epoll on my system, the setup |
|
|
895 | time is relatively high, though. |
|
|
896 | |
|
|
897 | Perl surprisingly comes second. It is much faster than the C-based event |
|
|
898 | loops Event and Glib. |
|
|
899 | |
|
|
900 | Event suffers from high setup time as well (look at its code and you |
|
|
901 | will understand why). Callback invocation also has a high overhead |
|
|
902 | compared to the "$_->() for .."-style loop that the Perl event loop |
|
|
903 | uses. Event uses select or poll in basically all documented |
|
|
904 | configurations. |
|
|
905 | |
|
|
906 | Glib is hit hard by its quadratic behaviour w.r.t. many watchers. It |
|
|
907 | clearly fails to perform with many filehandles or in busy servers. |
|
|
908 | |
|
|
909 | POE is still completely out of the picture, taking over 1000 times as |
|
|
910 | long as EV, and over 100 times as long as the Perl implementation, even |
|
|
911 | though it uses a C-based event loop in this case. |
|
|
912 | |
|
|
913 | Summary |
|
|
914 | * The pure perl implementation performs extremely well, considering |
|
|
915 | that it uses select. |
|
|
916 | |
|
|
917 | * Avoid Glib or POE in large projects where performance matters. |
|
|
918 | |
|
|
919 | BENCHMARKING SMALL SERVERS |
|
|
920 | While event loops should scale (and select-based ones do not...) even to |
|
|
921 | large servers, most programs we (or I :) actually write have only a few |
|
|
922 | I/O watchers. |
|
|
923 | |
|
|
924 | In this benchmark, I use the same benchmark program as in the large |
|
|
925 | server case, but it uses only eight "servers", of which three are active |
|
|
926 | at any one time. This should reflect performance for a small server |
|
|
927 | relatively well. |
|
|
928 | |
|
|
929 | The columns are identical to the previous table. |
|
|
930 | |
|
|
931 | Results |
|
|
932 | name sockets create request |
|
|
933 | EV 16 20.00 6.54 |
|
|
934 | Perl 16 25.75 12.62 |
|
|
935 | Event 16 81.27 35.86 |
|
|
936 | Glib 16 32.63 15.48 |
|
|
937 | POE 16 261.87 276.28 uses POE::Loop::Event |
|
|
938 | |
|
|
939 | Discussion |
|
|
940 | The benchmark tries to test the performance of a typical small server. |
|
|
941 | While knowing how various event loops perform is interesting, keep in |
|
|
942 | mind that their overhead in this case is usually not as important, due |
|
|
943 | to the small absolute number of watchers (that is, you need efficiency |
|
|
944 | and speed most when you have lots of watchers, not when you only have a |
|
|
945 | few of them). |
|
|
946 | |
|
|
947 | EV is again fastest. |
|
|
948 | |
|
|
949 | Perl again comes second. It is noticably faster than the C-based event |
|
|
950 | loops Event and Glib, although the difference is too small to really |
|
|
951 | matter. |
|
|
952 | |
|
|
953 | POE also performs much better in this case, but is is still far behind |
|
|
954 | the others. |
|
|
955 | |
|
|
956 | Summary |
|
|
957 | * C-based event loops perform very well with small number of watchers, |
|
|
958 | as the management overhead dominates. |
|
|
959 | |
|
|
960 | FORK |
|
|
961 | Most event libraries are not fork-safe. The ones who are usually are |
|
|
962 | because they are so inefficient. Only EV is fully fork-aware. |
|
|
963 | |
|
|
964 | If you have to fork, you must either do so *before* creating your first |
|
|
965 | watcher OR you must not use AnyEvent at all in the child. |
|
|
966 | |
|
|
967 | SECURITY CONSIDERATIONS |
|
|
968 | AnyEvent can be forced to load any event model via |
|
|
969 | $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used |
|
|
970 | to execute arbitrary code or directly gain access, it can easily be used |
|
|
971 | to make the program hang or malfunction in subtle ways, as AnyEvent |
|
|
972 | watchers will not be active when the program uses a different event |
|
|
973 | model than specified in the variable. |
|
|
974 | |
|
|
975 | You can make AnyEvent completely ignore this variable by deleting it |
|
|
976 | before the first watcher gets created, e.g. with a "BEGIN" block: |
|
|
977 | |
|
|
978 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
|
|
979 | |
|
|
980 | use AnyEvent; |
|
|
981 | |
593 | SEE ALSO |
982 | SEE ALSO |
594 | Event modules: Coro::EV, EV, EV::Glib, Glib::EV, Coro::Event, Event, |
983 | Event modules: Coro::EV, EV, EV::Glib, Glib::EV, Coro::Event, Event, |
595 | Glib::Event, Glib, Coro, Tk. |
984 | Glib::Event, Glib, Coro, Tk, Event::Lib, Qt, POE. |
596 | |
985 | |
597 | Implementations: AnyEvent::Impl::CoroEV, AnyEvent::Impl::EV, |
986 | Implementations: AnyEvent::Impl::CoroEV, AnyEvent::Impl::EV, |
598 | AnyEvent::Impl::CoroEvent, AnyEvent::Impl::Event, AnyEvent::Impl::Glib, |
987 | AnyEvent::Impl::CoroEvent, AnyEvent::Impl::Event, AnyEvent::Impl::Glib, |
|
|
988 | AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, AnyEvent::Impl::EventLib, |
599 | AnyEvent::Impl::Tk, AnyEvent::Impl::Perl. |
989 | AnyEvent::Impl::Qt, AnyEvent::Impl::POE. |
600 | |
990 | |
601 | Nontrivial usage examples: Net::FCP, Net::XMPP2. |
991 | Nontrivial usage examples: Net::FCP, Net::XMPP2. |
602 | |
992 | |
603 | AUTHOR |
993 | AUTHOR |
604 | Marc Lehmann <schmorp@schmorp.de> |
994 | Marc Lehmann <schmorp@schmorp.de> |