… | |
… | |
369 | |
369 | |
370 | There is no support for WxWidgets, as WxWidgets has no support for |
370 | There is no support for WxWidgets, as WxWidgets has no support for |
371 | watching file handles. However, you can use WxWidgets through the |
371 | watching file handles. However, you can use WxWidgets through the |
372 | POE Adaptor, as POE has a Wx backend that simply polls 20 times per |
372 | POE Adaptor, as POE has a Wx backend that simply polls 20 times per |
373 | second, which was considered to be too horrible to even consider for |
373 | second, which was considered to be too horrible to even consider for |
374 | AnyEvent. Likewise, other POE backends can be used by Anyevent by using |
374 | AnyEvent. Likewise, other POE backends can be used by AnyEvent by using |
375 | it's adaptor. |
375 | it's adaptor. |
|
|
376 | |
|
|
377 | AnyEvent knows about L<Prima> and L<Wx> and will try to use L<POE> when |
|
|
378 | autodetecting them. |
376 | |
379 | |
377 | =item AnyEvent::detect |
380 | =item AnyEvent::detect |
378 | |
381 | |
379 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
382 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
380 | if necessary. You should only call this function right before you would |
383 | if necessary. You should only call this function right before you would |
… | |
… | |
431 | no warnings; |
434 | no warnings; |
432 | use strict; |
435 | use strict; |
433 | |
436 | |
434 | use Carp; |
437 | use Carp; |
435 | |
438 | |
436 | our $VERSION = '3.2'; |
439 | our $VERSION = '3.3'; |
437 | our $MODEL; |
440 | our $MODEL; |
438 | |
441 | |
439 | our $AUTOLOAD; |
442 | our $AUTOLOAD; |
440 | our @ISA; |
443 | our @ISA; |
441 | |
444 | |
… | |
… | |
448 | [Coro::Event:: => AnyEvent::Impl::CoroEvent::], |
451 | [Coro::Event:: => AnyEvent::Impl::CoroEvent::], |
449 | [EV:: => AnyEvent::Impl::EV::], |
452 | [EV:: => AnyEvent::Impl::EV::], |
450 | [Event:: => AnyEvent::Impl::Event::], |
453 | [Event:: => AnyEvent::Impl::Event::], |
451 | [Glib:: => AnyEvent::Impl::Glib::], |
454 | [Glib:: => AnyEvent::Impl::Glib::], |
452 | [Tk:: => AnyEvent::Impl::Tk::], |
455 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
456 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
457 | [Prima:: => AnyEvent::Impl::POE::], |
453 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
458 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
454 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
459 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
455 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
460 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
456 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
461 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
457 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
462 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
… | |
… | |
855 | $quit->broadcast; |
860 | $quit->broadcast; |
856 | }); |
861 | }); |
857 | |
862 | |
858 | $quit->wait; |
863 | $quit->wait; |
859 | |
864 | |
|
|
865 | |
|
|
866 | =head1 BENCHMARK |
|
|
867 | |
|
|
868 | To give you an idea of the performance and overheads that AnyEvent adds |
|
|
869 | over the event loops themselves (and to give you an impression of the |
|
|
870 | speed of various event loops), here is a benchmark of various supported |
|
|
871 | event models natively and with anyevent. The benchmark creates a lot of |
|
|
872 | timers (with a zero timeout) and io watchers (watching STDOUT, a pty, to |
|
|
873 | become writable, which it is), lets them fire exactly once and destroys |
|
|
874 | them again. |
|
|
875 | |
|
|
876 | =head2 Explanation of the columns |
|
|
877 | |
|
|
878 | I<watcher> is the number of event watchers created/destroyed. Since |
|
|
879 | different event models feature vastly different performances, each event |
|
|
880 | loop was given a number of watchers so that overall runtime is acceptable |
|
|
881 | and similar between tested event loop (and keep them from crashing): Glib |
|
|
882 | would probably take thousands of years if asked to process the same number |
|
|
883 | of watchers as EV in this benchmark. |
|
|
884 | |
|
|
885 | I<bytes> is the number of bytes (as measured by the resident set size, |
|
|
886 | RSS) consumed by each watcher. This method of measuring captures both C |
|
|
887 | and Perl-based overheads. |
|
|
888 | |
|
|
889 | I<create> is the time, in microseconds (millionths of seconds), that it |
|
|
890 | takes to create a single watcher. The callback is a closure shared between |
|
|
891 | all watchers, to avoid adding memory overhead. That means closure creation |
|
|
892 | and memory usage is not included in the figures. |
|
|
893 | |
|
|
894 | I<invoke> is the time, in microseconds, used to invoke a simple |
|
|
895 | callback. The callback simply counts down a Perl variable and after it was |
|
|
896 | invoked "watcher" times, it would C<< ->broadcast >> a condvar once to |
|
|
897 | signal the end of this phase. |
|
|
898 | |
|
|
899 | I<destroy> is the time, in microseconds, that it takes to destroy a single |
|
|
900 | watcher. |
|
|
901 | |
|
|
902 | =head2 Results |
|
|
903 | |
|
|
904 | name watcher bytes create invoke destroy comment |
|
|
905 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
|
|
906 | EV/Any 100000 610 3.52 0.91 0.75 EV + AnyEvent watchers |
|
|
907 | CoroEV/Any 100000 610 3.49 0.92 0.75 coroutines + Coro::Signal |
|
|
908 | Perl/Any 16000 654 4.64 1.22 0.77 pure perl implementation |
|
|
909 | Event/Event 16000 523 28.05 21.38 0.86 Event native interface |
|
|
910 | Event/Any 16000 943 34.43 20.48 1.39 Event + AnyEvent watchers |
|
|
911 | Glib/Any 16000 1357 96.99 12.55 55.51 quadratic behaviour |
|
|
912 | Tk/Any 2000 1855 27.01 66.61 14.03 SEGV with >> 2000 watchers |
|
|
913 | POE/Event 2000 6644 108.15 768.19 14.33 via POE::Loop::Event |
|
|
914 | POE/Select 2000 6343 94.69 807.65 562.69 via POE::Loop::Select |
|
|
915 | |
|
|
916 | =head2 Discussion |
|
|
917 | |
|
|
918 | The benchmark does I<not> measure scalability of the event loop very |
|
|
919 | well. For example, a select-based event loop (such as the pure perl one) |
|
|
920 | can never compete with an event loop that uses epoll when the number of |
|
|
921 | file descriptors grows high. In this benchmark, only a single filehandle |
|
|
922 | is used (although some of the AnyEvent adaptors dup() its file descriptor |
|
|
923 | to worka round bugs). |
|
|
924 | |
|
|
925 | C<EV> is the sole leader regarding speed and memory use, which are both |
|
|
926 | maximal/minimal, respectively. Even when going through AnyEvent, there is |
|
|
927 | only one event loop that uses less memory (the C<Event> module natively), and |
|
|
928 | no faster event model, not event C<Event> natively. |
|
|
929 | |
|
|
930 | The pure perl implementation is hit in a few sweet spots (both the |
|
|
931 | zero timeout and the use of a single fd hit optimisations in the perl |
|
|
932 | interpreter and the backend itself). Nevertheless tis shows that it |
|
|
933 | adds very little overhead in itself. Like any select-based backend its |
|
|
934 | performance becomes really bad with lots of file descriptors, of course, |
|
|
935 | but this was not subjetc of this benchmark. |
|
|
936 | |
|
|
937 | The C<Event> module has a relatively high setup and callback invocation cost, |
|
|
938 | but overall scores on the third place. |
|
|
939 | |
|
|
940 | C<Glib>'s memory usage is quite a bit bit higher, features a faster |
|
|
941 | callback invocation and overall lands in the same class as C<Event>. |
|
|
942 | |
|
|
943 | The C<Tk> adaptor works relatively well, the fact that it crashes with |
|
|
944 | more than 2000 watchers is a big setback, however, as correctness takes |
|
|
945 | precedence over speed. Nevertheless, its performance is surprising, as the |
|
|
946 | file descriptor is dup()ed for each watcher. This shows that the dup() |
|
|
947 | employed by some adaptors is not a big performance issue (it does incur a |
|
|
948 | hidden memory cost inside the kernel, though). |
|
|
949 | |
|
|
950 | C<POE>, regardless of backend (wether using its pure perl select-based |
|
|
951 | backend or the Event backend) shows abysmal performance and memory |
|
|
952 | usage: Watchers use almost 30 times as much memory as EV watchers, and 10 |
|
|
953 | times as much memory as both Event or EV via AnyEvent. Watcher invocation |
|
|
954 | is almost 700 times slower as with AnyEvent's pure perl implementation. |
|
|
955 | |
|
|
956 | Summary: using EV through AnyEvent is faster than any other event |
|
|
957 | loop. The overhead AnyEvent adds can be very small, and you should avoid |
|
|
958 | POE like the plague if you want performance or reasonable memory usage. |
|
|
959 | |
|
|
960 | |
860 | =head1 FORK |
961 | =head1 FORK |
861 | |
962 | |
862 | Most event libraries are not fork-safe. The ones who are usually are |
963 | Most event libraries are not fork-safe. The ones who are usually are |
863 | because they are so inefficient. Only L<EV> is fully fork-aware. |
964 | because they are so inefficient. Only L<EV> is fully fork-aware. |
864 | |
965 | |
865 | If you have to fork, you must either do so I<before> creating your first |
966 | If you have to fork, you must either do so I<before> creating your first |
866 | watcher OR you must not use AnyEvent at all in the child. |
967 | watcher OR you must not use AnyEvent at all in the child. |
|
|
968 | |
867 | |
969 | |
868 | =head1 SECURITY CONSIDERATIONS |
970 | =head1 SECURITY CONSIDERATIONS |
869 | |
971 | |
870 | AnyEvent can be forced to load any event model via |
972 | AnyEvent can be forced to load any event model via |
871 | $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used to |
973 | $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used to |
… | |
… | |
879 | |
981 | |
880 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
982 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
881 | |
983 | |
882 | use AnyEvent; |
984 | use AnyEvent; |
883 | |
985 | |
|
|
986 | |
884 | =head1 SEE ALSO |
987 | =head1 SEE ALSO |
885 | |
988 | |
886 | Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
989 | Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
887 | L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>, |
990 | L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>, |
888 | L<Event::Lib>, L<Qt>, L<POE>. |
991 | L<Event::Lib>, L<Qt>, L<POE>. |
… | |
… | |
892 | L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, L<AnyEvent::Impl::EventLib>, |
995 | L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, L<AnyEvent::Impl::EventLib>, |
893 | L<AnyEvent::Impl::Qt>, L<AnyEvent::Impl::POE>. |
996 | L<AnyEvent::Impl::Qt>, L<AnyEvent::Impl::POE>. |
894 | |
997 | |
895 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
998 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
896 | |
999 | |
|
|
1000 | |
897 | =head1 AUTHOR |
1001 | =head1 AUTHOR |
898 | |
1002 | |
899 | Marc Lehmann <schmorp@schmorp.de> |
1003 | Marc Lehmann <schmorp@schmorp.de> |
900 | http://home.schmorp.de/ |
1004 | http://home.schmorp.de/ |
901 | |
1005 | |