… | |
… | |
141 | =head2 I/O WATCHERS |
141 | =head2 I/O WATCHERS |
142 | |
142 | |
143 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
143 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
144 | with the following mandatory key-value pairs as arguments: |
144 | with the following mandatory key-value pairs as arguments: |
145 | |
145 | |
146 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch for |
146 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch |
147 | events. C<poll> must be a string that is either C<r> or C<w>, which |
147 | for events. C<poll> must be a string that is either C<r> or C<w>, |
148 | creates a watcher waiting for "r"eadable or "w"ritable events, |
148 | which creates a watcher waiting for "r"eadable or "w"ritable events, |
149 | respectively. C<cb> is the callback to invoke each time the file handle |
149 | respectively. C<cb> is the callback to invoke each time the file handle |
150 | becomes ready. |
150 | becomes ready. |
151 | |
151 | |
|
|
152 | Although the callback might get passed parameters, their value and |
|
|
153 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
154 | callbacks cannot use arguments passed to I/O watcher callbacks. |
|
|
155 | |
152 | The I/O watcher might use the underlying file descriptor or a copy of it. |
156 | The I/O watcher might use the underlying file descriptor or a copy of it. |
153 | It is not allowed to close a file handle as long as any watcher is active |
157 | You must not close a file handle as long as any watcher is active on the |
154 | on the underlying file descriptor. |
158 | underlying file descriptor. |
155 | |
159 | |
156 | Some event loops issue spurious readyness notifications, so you should |
160 | Some event loops issue spurious readyness notifications, so you should |
157 | always use non-blocking calls when reading/writing from/to your file |
161 | always use non-blocking calls when reading/writing from/to your file |
158 | handles. |
162 | handles. |
159 | |
163 | |
… | |
… | |
170 | |
174 | |
171 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
175 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
172 | method with the following mandatory arguments: |
176 | method with the following mandatory arguments: |
173 | |
177 | |
174 | C<after> specifies after how many seconds (fractional values are |
178 | C<after> specifies after how many seconds (fractional values are |
175 | supported) should the timer activate. C<cb> the callback to invoke in that |
179 | supported) the callback should be invoked. C<cb> is the callback to invoke |
176 | case. |
180 | in that case. |
|
|
181 | |
|
|
182 | Although the callback might get passed parameters, their value and |
|
|
183 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
184 | callbacks cannot use arguments passed to time watcher callbacks. |
177 | |
185 | |
178 | The timer callback will be invoked at most once: if you want a repeating |
186 | The timer callback will be invoked at most once: if you want a repeating |
179 | timer you have to create a new watcher (this is a limitation by both Tk |
187 | timer you have to create a new watcher (this is a limitation by both Tk |
180 | and Glib). |
188 | and Glib). |
181 | |
189 | |
… | |
… | |
226 | |
234 | |
227 | You can watch for signals using a signal watcher, C<signal> is the signal |
235 | You can watch for signals using a signal watcher, C<signal> is the signal |
228 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
236 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
229 | be invoked whenever a signal occurs. |
237 | be invoked whenever a signal occurs. |
230 | |
238 | |
|
|
239 | Although the callback might get passed parameters, their value and |
|
|
240 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
241 | callbacks cannot use arguments passed to signal watcher callbacks. |
|
|
242 | |
231 | Multiple signal occurances can be clumped together into one callback |
243 | Multiple signal occurances can be clumped together into one callback |
232 | invocation, and callback invocation will be synchronous. synchronous means |
244 | invocation, and callback invocation will be synchronous. synchronous means |
233 | that it might take a while until the signal gets handled by the process, |
245 | that it might take a while until the signal gets handled by the process, |
234 | but it is guarenteed not to interrupt any other callbacks. |
246 | but it is guarenteed not to interrupt any other callbacks. |
235 | |
247 | |
… | |
… | |
249 | |
261 | |
250 | The child process is specified by the C<pid> argument (if set to C<0>, it |
262 | The child process is specified by the C<pid> argument (if set to C<0>, it |
251 | watches for any child process exit). The watcher will trigger as often |
263 | watches for any child process exit). The watcher will trigger as often |
252 | as status change for the child are received. This works by installing a |
264 | as status change for the child are received. This works by installing a |
253 | signal handler for C<SIGCHLD>. The callback will be called with the pid |
265 | signal handler for C<SIGCHLD>. The callback will be called with the pid |
254 | and exit status (as returned by waitpid). |
266 | and exit status (as returned by waitpid), so unlike other watcher types, |
|
|
267 | you I<can> rely on child watcher callback arguments. |
255 | |
268 | |
256 | There is a slight catch to child watchers, however: you usually start them |
269 | There is a slight catch to child watchers, however: you usually start them |
257 | I<after> the child process was created, and this means the process could |
270 | I<after> the child process was created, and this means the process could |
258 | have exited already (and no SIGCHLD will be sent anymore). |
271 | have exited already (and no SIGCHLD will be sent anymore). |
259 | |
272 | |
… | |
… | |
926 | |
939 | |
927 | =head2 Results |
940 | =head2 Results |
928 | |
941 | |
929 | name watchers bytes create invoke destroy comment |
942 | name watchers bytes create invoke destroy comment |
930 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
943 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
931 | EV/Any 100000 610 3.52 0.91 0.75 EV + AnyEvent watchers |
944 | EV/Any 100000 244 2.50 0.46 0.29 EV + AnyEvent watchers |
932 | CoroEV/Any 100000 610 3.49 0.92 0.75 coroutines + Coro::Signal |
945 | CoroEV/Any 100000 244 2.49 0.44 0.29 coroutines + Coro::Signal |
933 | Perl/Any 100000 513 4.91 0.92 1.15 pure perl implementation |
946 | Perl/Any 100000 513 4.92 0.87 1.12 pure perl implementation |
934 | Event/Event 16000 523 28.05 21.38 0.86 Event native interface |
947 | Event/Event 16000 516 31.88 31.30 0.85 Event native interface |
935 | Event/Any 16000 943 34.43 20.48 1.39 Event + AnyEvent watchers |
948 | Event/Any 16000 936 39.17 33.63 1.43 Event + AnyEvent watchers |
936 | Glib/Any 16000 1357 96.99 12.55 55.51 quadratic behaviour |
949 | Glib/Any 16000 1357 98.22 12.41 54.00 quadratic behaviour |
937 | Tk/Any 2000 1855 27.01 66.61 14.03 SEGV with >> 2000 watchers |
950 | Tk/Any 2000 1860 26.97 67.98 14.00 SEGV with >> 2000 watchers |
938 | POE/Event 2000 6644 108.15 768.19 14.33 via POE::Loop::Event |
951 | POE/Event 2000 6644 108.64 736.02 14.73 via POE::Loop::Event |
939 | POE/Select 2000 6343 94.69 807.65 562.69 via POE::Loop::Select |
952 | POE/Select 2000 6343 94.13 809.12 565.96 via POE::Loop::Select |
940 | |
953 | |
941 | =head2 Discussion |
954 | =head2 Discussion |
942 | |
955 | |
943 | The benchmark does I<not> measure scalability of the event loop very |
956 | The benchmark does I<not> measure scalability of the event loop very |
944 | well. For example, a select-based event loop (such as the pure perl one) |
957 | well. For example, a select-based event loop (such as the pure perl one) |
… | |
… | |
946 | file descriptors grows high. In this benchmark, all events become ready at |
959 | file descriptors grows high. In this benchmark, all events become ready at |
947 | the same time, so select/poll-based implementations get an unnatural speed |
960 | the same time, so select/poll-based implementations get an unnatural speed |
948 | boost. |
961 | boost. |
949 | |
962 | |
950 | C<EV> is the sole leader regarding speed and memory use, which are both |
963 | C<EV> is the sole leader regarding speed and memory use, which are both |
951 | maximal/minimal, respectively. Even when going through AnyEvent, there are |
964 | maximal/minimal, respectively. Even when going through AnyEvent, it uses |
952 | only two event loops that use slightly less memory (the C<Event> module |
965 | far less memory than any other event loop and is still faster than Event |
953 | natively and the pure perl backend), and no faster event models, not even |
966 | natively. |
954 | C<Event> natively. |
|
|
955 | |
967 | |
956 | The pure perl implementation is hit in a few sweet spots (both the |
968 | The pure perl implementation is hit in a few sweet spots (both the |
957 | zero timeout and the use of a single fd hit optimisations in the perl |
969 | constant timeout and the use of a single fd hit optimisations in the perl |
958 | interpreter and the backend itself, and all watchers become ready at the |
970 | interpreter and the backend itself). Nevertheless this shows that it |
959 | same time). Nevertheless this shows that it adds very little overhead in |
971 | adds very little overhead in itself. Like any select-based backend its |
960 | itself. Like any select-based backend its performance becomes really bad |
972 | performance becomes really bad with lots of file descriptors (and few of |
961 | with lots of file descriptors (and few of them active), of course, but |
973 | them active), of course, but this was not subject of this benchmark. |
962 | this was not subject of this benchmark. |
|
|
963 | |
974 | |
964 | The C<Event> module has a relatively high setup and callback invocation cost, |
975 | The C<Event> module has a relatively high setup and callback invocation cost, |
965 | but overall scores on the third place. |
976 | but overall scores on the third place. |
966 | |
977 | |
967 | C<Glib>'s memory usage is quite a bit bit higher, but it features a |
978 | C<Glib>'s memory usage is quite a bit bit higher, but it features a |
… | |
… | |
975 | The C<Tk> adaptor works relatively well. The fact that it crashes with |
986 | The C<Tk> adaptor works relatively well. The fact that it crashes with |
976 | more than 2000 watchers is a big setback, however, as correctness takes |
987 | more than 2000 watchers is a big setback, however, as correctness takes |
977 | precedence over speed. Nevertheless, its performance is surprising, as the |
988 | precedence over speed. Nevertheless, its performance is surprising, as the |
978 | file descriptor is dup()ed for each watcher. This shows that the dup() |
989 | file descriptor is dup()ed for each watcher. This shows that the dup() |
979 | employed by some adaptors is not a big performance issue (it does incur a |
990 | employed by some adaptors is not a big performance issue (it does incur a |
980 | hidden memory cost inside the kernel, though, that is not reflected in the |
991 | hidden memory cost inside the kernel which is not reflected in the figures |
981 | figures above). |
992 | above). |
982 | |
993 | |
983 | C<POE>, regardless of underlying event loop (wether using its pure perl |
994 | C<POE>, regardless of underlying event loop (whether using its pure |
984 | select-based backend or the Event module) shows abysmal performance and |
995 | perl select-based backend or the Event module, the POE-EV backend |
|
|
996 | couldn't be tested because it wasn't working) shows abysmal performance |
985 | memory usage: Watchers use almost 30 times as much memory as EV watchers, |
997 | and memory usage: Watchers use almost 30 times as much memory as |
986 | and 10 times as much memory as both Event or EV via AnyEvent. Watcher |
998 | EV watchers, and 10 times as much memory as Event (the high memory |
|
|
999 | requirements are caused by requiring a session for each watcher). Watcher |
987 | invocation is almost 900 times slower than with AnyEvent's pure perl |
1000 | invocation speed is almost 900 times slower than with AnyEvent's pure perl |
988 | implementation. The design of the POE adaptor class in AnyEvent can not |
1001 | implementation. The design of the POE adaptor class in AnyEvent can not |
989 | really account for this, as session creation overhead is small compared |
1002 | really account for this, as session creation overhead is small compared |
990 | to execution of the state machine, which is coded pretty optimally within |
1003 | to execution of the state machine, which is coded pretty optimally within |
991 | L<AnyEvent::Impl::POE>. POE simply seems to be abysmally slow. |
1004 | L<AnyEvent::Impl::POE>. POE simply seems to be abysmally slow. |
992 | |
1005 | |
993 | =head2 Summary |
1006 | =head2 Summary |
994 | |
1007 | |
|
|
1008 | =over 4 |
|
|
1009 | |
995 | Using EV through AnyEvent is faster than any other event loop, but most |
1010 | =item * Using EV through AnyEvent is faster than any other event loop |
996 | event loops have acceptable performance with or without AnyEvent. |
1011 | (even when used without AnyEvent), but most event loops have acceptable |
|
|
1012 | performance with or without AnyEvent. |
997 | |
1013 | |
998 | The overhead AnyEvent adds is usually much smaller than the overhead of |
1014 | =item * The overhead AnyEvent adds is usually much smaller than the overhead of |
999 | the actual event loop, only with extremely fast event loops such as the EV |
1015 | the actual event loop, only with extremely fast event loops such as EV |
1000 | adds AnyEvent significant overhead. |
1016 | adds AnyEvent significant overhead. |
1001 | |
1017 | |
1002 | And you should simply avoid POE like the plague if you want performance or |
1018 | =item * You should simply avoid POE like the plague if you want performance or |
1003 | reasonable memory usage. |
1019 | reasonable memory usage. |
|
|
1020 | |
|
|
1021 | =back |
1004 | |
1022 | |
1005 | |
1023 | |
1006 | =head1 FORK |
1024 | =head1 FORK |
1007 | |
1025 | |
1008 | Most event libraries are not fork-safe. The ones who are usually are |
1026 | Most event libraries are not fork-safe. The ones who are usually are |