From 16e11c0d70e21a10cb7058fa5b054b5e2d724447 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 24 Aug 2016 01:18:05 +0200 Subject: [PATCH] #799 add a second script which uses perf module, which is supposed to be more reliable --- Makefile | 7 ++++ scripts/internal/bench_oneshot_2.py | 56 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 scripts/internal/bench_oneshot_2.py diff --git a/Makefile b/Makefile index 8886731d9..0d037a206 100644 --- a/Makefile +++ b/Makefile @@ -188,3 +188,10 @@ win-upload-exes: # run script which benchmarks oneshot() ctx manager (see #799) bench-oneshot: install $(PYTHON) scripts/internal/bench_oneshot.py + +# same as above but using perf module (supposed to be more precise) +bench-oneshot-2: install + rm -f normal.json oneshot.json + $(PYTHON) scripts/internal/bench_oneshot_2.py normal -o normal.json + $(PYTHON) scripts/internal/bench_oneshot_2.py oneshot -o oneshot.json + $(PYTHON) -m perf compare_to normal.json oneshot.json diff --git a/scripts/internal/bench_oneshot_2.py b/scripts/internal/bench_oneshot_2.py new file mode 100644 index 000000000..b57581499 --- /dev/null +++ b/scripts/internal/bench_oneshot_2.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +Same as bench_oneshot.py but uses perf module instead, which is +supposed to be more precise. +""" + +import sys + +import perf.text_runner + +import psutil +from bench_oneshot import names + + +p = psutil.Process() +funs = [getattr(p, n) for n in names] + + +def call_normal(funs): + for fun in funs: + fun() + + +def call_oneshot(funs): + with p.oneshot(): + for fun in funs: + fun() + + +def prepare_cmd(runner, cmd): + cmd.append(runner.args.benchmark) + + +def main(): + runner = perf.text_runner.TextRunner(name='psutil') + runner.argparser.add_argument('benchmark', choices=('normal', 'oneshot')) + runner.prepare_subprocess_args = prepare_cmd + + args = runner.parse_args() + if not args.worker: + print("%s methods involved on platform %r:" % ( + len(names), sys.platform)) + for name in sorted(names): + print(" " + name) + + if args.benchmark == 'normal': + runner.bench_func(call_normal, funs) + else: + runner.bench_func(call_oneshot, funs) + +main()