Commit b166aaf8 authored by Jason Madden's avatar Jason Madden

Add benchmark for using subprocess.Popen to create /usr/bin/true [skip ci]

In response to #1172

The following numbers are for my machine on macOS 10.13.3 with MAXFD
of 50000.

Python 2.7:

.....................
spawn native no close_fds: Mean +- std dev: 1.81 ms +- 0.04 ms
.....................
spawn gevent no close_fds: Mean +- std dev: 2.11 ms +- 0.08 ms
.....................
spawn native close_fds: Mean +- std dev: 31.0 ms +- 0.7 ms
.....................
spawn gevent close_fds: Mean +- std dev: 31.6 ms +- 0.6 ms

Notice that the times when close_fd=True (not the default on 2.7) are
about the same. 2.7 uses the same Python loop we do to close all the
fds.

Now 3.7:

.....................
spawn native no close_fds: Mean +- std dev: 1.34 ms +- 0.04 ms
.....................
spawn gevent no close_fds: Mean +- std dev: 117 ms +- 2 ms
.....................
spawn native close_fds: Mean +- std dev: 1.36 ms +- 0.03 ms
.....................
spawn gevent close_fds: Mean +- std dev: 32.5 ms +- 0.4 ms

Notice that gevent is *much* slower when we *don't* close the fds.
This is because, starting in Python 3.4, close_fds defaults to true,
and when it's false we have to check os.get_inheritable() for each fd
before we close it. gevent performs the same as it did on Python 2.7
when closing fds, but the native implementation is much faster due to
the C optimizations outlined in #1172---it turns out they apply to BSD
and Apple platforms in addition to Linux, although they're not async
safe.

Now, the C code does the opposite for inheritable handles: it
explicitly calls make_inheritable() for the ones it wants to keep and
lets the OS close the others with CLOEXEC. We could probably do that
too; the slow down for this case counts as a regression, I think.
parent b7c9a1df
# -*- coding: utf-8 -*-
"""
Benchmarks for thread locals.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import perf
from gevent import subprocess as gsubprocess
import subprocess as nsubprocess
N = 10
def _bench_spawn(module, loops, close_fds=True):
total = 0
for _ in range(loops):
t0 = perf.perf_counter()
procs = [module.Popen('/usr/bin/true', close_fds=close_fds)
for _ in range(N)]
t1 = perf.perf_counter()
for p in procs:
p.communicate()
p.poll()
total += (t1 - t0)
return total
def bench_spawn_native(loops, close_fds=True):
return _bench_spawn(nsubprocess, loops, close_fds)
def bench_spawn_gevent(loops, close_fds=True):
return _bench_spawn(gsubprocess, loops, close_fds)
def main():
runner = perf.Runner()
runner.bench_time_func('spawn native no close_fds',
bench_spawn_native,
False,
inner_loops=N)
runner.bench_time_func('spawn gevent no close_fds',
bench_spawn_gevent,
False,
inner_loops=N)
runner.bench_time_func('spawn native close_fds',
bench_spawn_native,
inner_loops=N)
runner.bench_time_func('spawn gevent close_fds',
bench_spawn_gevent,
inner_loops=N)
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment