Skip to content

Commit

Permalink
#799 add a second script which uses perf module, which is supposed to…
Browse files Browse the repository at this point in the history
… be more reliable
  • Loading branch information
giampaolo committed Aug 23, 2016
1 parent 8a2505e commit 16e11c0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
56 changes: 56 additions & 0 deletions scripts/internal/bench_oneshot_2.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 16e11c0

Please sign in to comment.