• Stars
    star
    100
  • Rank 340,575 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A little benchmarking tool for Python

rich-bench

A little Python benchmarking tool.

Why do I need this?

The builtin timeit module for Python is great, but the typical usage for micro-benchmarks is to run a small script like this:

python -m timeit "a = 1; b = 2; a * b"

The problem with this approach is that the compiled code is a module, so any variables on the top-level are globals. The compiled bytecode is different to the same statements being inside a local scope (e.g. a method or function). They behave and perform very differently in CPython.

richbench encourages you to write benchmarks inside functions to properly simulate the closures and scope of production code.

Installation

Requires Python 3.6+ and can be installed using pip:

pip install richbench

Usage

Write your benchmark functions in a directory and run richbench with that target to get the results:

$ richbench my_benchmarks/

Results are displayed in a table like this:

Example result table

$ richbench --help
usage: richbench [-h] [--profile] [--percentage] [--markdown] [--benchmark [BENCHMARK]] [--repeat REPEAT] [--times TIMES] target [target ...]

positional arguments:
  target

options:
  -h, --help            show this help message and exit
  --profile             Profile the benchmarks and store in .profiles/
  --percentage          Show percentage of improvement instead of multiplier
  --markdown            Prints a markdown friendly table
  --benchmark [BENCHMARK]
                        Run specific benchmark
  --repeat REPEAT       Repeat benchmark this many times
  --times TIMES         Run benchmark this many times

Writing benchmarks

Benchmarks should be in a directory and must have the filename bench_{name}.py.

The last statement in the benchmark file should be a list, called __benchmarks__ with a list of tuples containing:

  1. function a
  2. function b
  3. the name of the benchmark
def sort_seven():
    """Sort a list of seven items"""
    for _ in range(10_000):
        sorted([3,2,4,5,1,5,3])

def sort_three():
    """Sort a list of three items"""
    for _ in range(10_000):
        sorted([3,2,4])

__benchmarks__ = [
    (sort_seven, sort_three, "Sorting 3 items instead of 7")
]

Tips for benchmarking

Inside your benchmark function try to:

  • Run setup commands once
  • Repeat the code you want to benchmark as many times as possible to create a stable benchmark

For micro-benchmarks, this can be achieved by calling 1000 - 100,000 times.

If your benchmark code completes within 0.001s it is likely you will get unstable benchmarks because of the CPU doing other activities. Increase the times the target code is run within the function.

Profiling

By adding the --profile flag to the command line, it will generate a subdirectory .profiles with HTML profile data of your target functions.

More Repositories

1

vscode-pets

Adds playful pets 🦀🐱🐶 in your VS Code window
TypeScript
1,553
star
2

wily

A Python application for tracking, reporting on timing and complexity in Python code
Python
1,205
star
3

mocker

A Docker-type runtime, written in 100% Python
Python
794
star
4

perflint

Python Linter for performance anti patterns
Python
582
star
5

pycharm-security

Finds security holes in your Python projects from PyCharm and GitHub
Kotlin
312
star
6

cpython-book-samples

Sample scripts and examples for my CPython Internals book
Python
180
star
7

anti-patterns

Python
137
star
8

instaviz

Instant visualization of Python AST and Code Objects
JavaScript
107
star
9

retox

For running a local continuous testing environment with tox
Python
76
star
10

ants-azure-demos

Collection of PoCs and Azure Demos
Python
49
star
11

python-assembly-poc

Python
46
star
12

requests-staticmock

A test utility for mocking out requests host from a fixtures directory
Python
42
star
13

django-xss-fuzzer

An XSS fuzzer for Django
Python
38
star
14

workday

A Python client for workday.com
Python
37
star
15

CSnakes

C#
37
star
16

azure-pipelines-python-examples

Example configurations for Azure Build Pipelines for Python
Python
36
star
17

hathi

A dictionary attack tool for PostgreSQL and MSSQL
Python
33
star
18

pep-explorer

An easy to use online explorer for Python Enhancement Proposals
HTML
33
star
19

notations

Estimating Big-O notations for a given function in Python
Python
24
star
20

django-on-azure

Resources for my Django on Azure workshop at PyCon US 2021
CSS
19
star
21

ServiceNowHackathon2016

Sydney Hackathon with ServiceNow March 2016- Slack and ServiceNow
JavaScript
19
star
22

tonybaloney.github.io

HTML
19
star
23

wntf

An anti-recommendation algorithm for twitter
Python
17
star
24

pyucwa

Python client for the Skype for Business (Lync) UCWA 2.0 API
Python
17
star
25

django-on-azure-demo

Tutorial for running Django on Azure
Python
16
star
26

dependabot-bot

Python
15
star
27

sciencelogic

A ScienceLogic EM7 API client for Python
Python
15
star
28

readysalted

An Internet of Things toolkit for SaltStack
C++
14
star
29

hubot-spark

A hubot integration for Cisco Spark
CoffeeScript
14
star
30

dissy

A TUI disassembler
Python
14
star
31

pyinline

Python
13
star
32

St2Client

A StackStorm API client for C#.NET including a PowerShell module
C#
13
star
33

python-railroads

A script to generate railroad diagrams for Python grammar
Python
13
star
34

tonybaloney

12
star
35

netimages

tool for sniffing images over HTTP traffic and showing them on the console. Designed for remote shells.
Python
12
star
36

bad-security-practices

Python
11
star
37

performance_testing

Results from a performance test for Python runtimes
HTML
9
star
38

try-pyjion

JavaScript
8
star
39

confluence-to-powerpoint

Python
8
star
40

generic_demos

Python
7
star
41

python-3.11-demos

Python
6
star
42

libcloud.extra

Some extra packs and examples for libcloud integration with StackStorm
Python
6
star
43

python-task-provider

TypeScript
5
star
44

pluralsight

Python
5
star
45

markdownlint-rule-titlecase

Custom title case rule for markdownlint headers
JavaScript
4
star
46

wily-pycharm

Code Complexity Plugin for PyCharm and IntelliJ-based IDEs
Kotlin
4
star
47

multicloud

Python
4
star
48

pytest-freethreaded

Python
4
star
49

pyrower

A Python Rowing Machine
Python
3
star
50

django-cosmos

A Cosmos DB driver for Django
Python
3
star
51

vs-test-detail

An NUnit extension for Azure DevOps
TypeScript
3
star
52

Cloud-auto-scaling

Provides auto-scaling capabilities to a group of Virtual Servers via SNMP, currently works with Abiquo
PHP
3
star
53

python-for-csharp-java-devs

2
star
54

pywinexe

Python bindings for winexe
Python
2
star
55

rightscale-agent-libcloud

Python
2
star
56

pathgather

Python client for Pathgather API
Python
2
star
57

pyjion-home

The website for www.trypyjion.com
CSS
2
star
58

tox-nuitka

A tox plugin for executing via nuitka
Python
2
star
59

cpython-clion-demo

Demo of debugging CPython from CLion
C
2
star
60

simple-flask-azd

A tiny template for Azure Developer CLI with Flask running in App Service
Bicep
1
star
61

email-toolbox

Python
1
star
62

panic_room

Python
1
star
63

pysaba

Python
1
star
64

cisco-spark-async-bot

A Python Asynchronous Bot for Cisco Spark API
Python
1
star
65

hubot-servicenow

JavaScript
1
star
66

ants-vscode-session

Jupyter Notebook
1
star
67

Abiquo-Backup

Backup Virtual Machines in an Abiquo Cloud
PHP
1
star
68

cognitive-kitchen-sink

SCSS
1
star
69

No-More-Spreadsheets

A web based service catalogue and pricing tool for products and services.
ASP
1
star
70

PHPVarnish

PHP Class for Varnish Management CLI
PHP
1
star
71

pycharm-webinar

Demo project for PyCharm webinar
Python
1
star
72

pyupgradesim

CSS
1
star