perf-baseline


Nameperf-baseline JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/rayluo/perf_baseline
SummaryThe perf_baseline is a performance regression detection tool for Python projects.
upload_time2023-09-06 05:37:24
maintainer
docs_urlNone
authorRay Luo
requires_python
licenseMIT
keywords performance timeit baseline regression detection
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Perf Baseline

The ``perf_baseline`` is a performance regression detection tool for Python projects.
It uses ``timeit`` to automatically time your function,
records the result as a baseline into a file,
and compares subsequent test results against the initial baseline
to detect performance regression based on your specified threshold.
(We do not compare against the second-last performance test result,
therefore your performance won't suffer from gradual decline.)


## Installation

`pip install perf_baseline`

## Usage

Let's say your project contains some important functions like this:

```python
def add(a, b):
    return a + b

def sub(a, b):
    return a - b
```

And you want to prevent your future refactoring
from accidentally introducing performance regression.
How? All you need to do is to create test cases like this:

```python
from perf_baseline import Baseline

baseline = Baseline(
    "my_baseline.bin",  # Performance test result will be saved into this file
    threshold=1.8,  # Subsequent tests will raise exception if a new test is more than 1.8x slower than the baseline
)

def test_add_should_not_have_regression():
    baseline.set_or_compare("add(2, 3)", name="my addition implementation", globals={"add": add})

def test_sub_should_not_have_regression():
    baseline.set_or_compare("sub(5, 4)", globals={"sub": sub})
    # Note: When absent, name defaults to the current test function's name,
    #       which is "test_sub_should_not_have_regression" in this case
```

That is it.

Now you can run ``pytest`` to test it, or ``pytest --log-cli-level INFO`` to see some logs.
The test case will pass if there is no performance regression, or raise ``RegressionError`` otherwise.

Under the hood, ``perf_baseline`` stores the *initial* test results into the file you specified.
Each file may contain multiple data points, differentiated by their unique names.
If the ``name`` parameter is omitted, the current test case's name will be used instead.

Subsequent tests will automatically be compared with the initial baseline,
and error out when performance regression is detected.

If you want to reset the baseline, simply delete that baseline file and then start afresh.


## What if the test subject requires nontrivial setup?

In real world projects, your test subject may require nontrivial setup,
such as prepopulating some test data,
and those initialization time shall be excluded from benchmark.
How do we achieve that?

A common solution is creating a wrapper class,
which has an expensive constructor and a (typically parameter-less) action method.
And then you can have ``Baseline`` to check that action method.
For example:

```python
class TestDriver:
    def __init__(self, size):
        self.data = list(range(size))
        random.shuffle(self.data)
    def sort(self):
        # Some implementation to sort the self.data in-place

baseline = Baseline("my_baseline.bin", threshold=2.0)

def test_my_sort_implementation():
    driver = TestDriver(1000*1000)
    baseline.set_or_compare(
        driver.sort,  # A parameter-less callable can be tested as-is, without setting globals
    )

    # You may also combine the above two lines into one,
    # and the initialization still runs only once.
    baseline.set_or_compare(
        TestDriver(1000*1000).sort,  # The driver initialization is still done only once
    )
```


## Do NOT commit the baseline file into Git

Add your baseline filename into your ``.gitignore``, so that you won't accidentally commit it.

```
my_baseline.bin
```

Why?

The idea is that a performance baseline (such as 123456 ops/sec) is
only meaningful and consistent when running on the *same* computer.
Switching to a different computer, it will have a different baseline.

By not committing a baseline into the source code repo,
each maintainer of your project (and each of their computers)
will have their own baseline created by the first run.

This way, you won't need to use a large threshold across different computers
(it is impossible to specify a constant threshold that works on different computers anyway).
Per-computer baselines all self-calibrate to match the performance of each computer.


## How to run this in Github Action?

``perf_baseline`` relies on an *updatable* baseline file,
which shall be writable when a new data point (represented by a new ``name``) occurs,
and remain read-only when an old data point (represented by same ``name``) already exists.

As of this writing, [Github's Cache Action](https://github.com/marketplace/actions/cache)
supports the updatable usage via some hack, inspired by
[this hint](https://github.com/actions/toolkit/issues/505#issuecomment-1650290249).
Use the following snippet, and modify its ``path`` and ``hashFiles(...)`` to match your filenames.

```yaml
    - name: Setup an updatable cache for Performance Baselines
      uses: actions/cache@v3
      with:
        path: my_baseline.bin
        key: ${{ runner.os }}-performance-${{ hashFiles('tests/test_benchmark.py') }}
        restore-keys: ${{ runner.os }}-performance-
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/rayluo/perf_baseline",
    "name": "perf-baseline",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "performance,timeit,baseline,regression,detection",
    "author": "Ray Luo",
    "author_email": "rayluo.mba@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/7c/3b/f5551421c4a05996bd5b841af6cc35116c4007f42878e507f0fc90a08732/perf_baseline-0.1.0.tar.gz",
    "platform": null,
    "description": "# Perf Baseline\n\nThe ``perf_baseline`` is a performance regression detection tool for Python projects.\nIt uses ``timeit`` to automatically time your function,\nrecords the result as a baseline into a file,\nand compares subsequent test results against the initial baseline\nto detect performance regression based on your specified threshold.\n(We do not compare against the second-last performance test result,\ntherefore your performance won't suffer from gradual decline.)\n\n\n## Installation\n\n`pip install perf_baseline`\n\n## Usage\n\nLet's say your project contains some important functions like this:\n\n```python\ndef add(a, b):\n    return a + b\n\ndef sub(a, b):\n    return a - b\n```\n\nAnd you want to prevent your future refactoring\nfrom accidentally introducing performance regression.\nHow? All you need to do is to create test cases like this:\n\n```python\nfrom perf_baseline import Baseline\n\nbaseline = Baseline(\n    \"my_baseline.bin\",  # Performance test result will be saved into this file\n    threshold=1.8,  # Subsequent tests will raise exception if a new test is more than 1.8x slower than the baseline\n)\n\ndef test_add_should_not_have_regression():\n    baseline.set_or_compare(\"add(2, 3)\", name=\"my addition implementation\", globals={\"add\": add})\n\ndef test_sub_should_not_have_regression():\n    baseline.set_or_compare(\"sub(5, 4)\", globals={\"sub\": sub})\n    # Note: When absent, name defaults to the current test function's name,\n    #       which is \"test_sub_should_not_have_regression\" in this case\n```\n\nThat is it.\n\nNow you can run ``pytest`` to test it, or ``pytest --log-cli-level INFO`` to see some logs.\nThe test case will pass if there is no performance regression, or raise ``RegressionError`` otherwise.\n\nUnder the hood, ``perf_baseline`` stores the *initial* test results into the file you specified.\nEach file may contain multiple data points, differentiated by their unique names.\nIf the ``name`` parameter is omitted, the current test case's name will be used instead.\n\nSubsequent tests will automatically be compared with the initial baseline,\nand error out when performance regression is detected.\n\nIf you want to reset the baseline, simply delete that baseline file and then start afresh.\n\n\n## What if the test subject requires nontrivial setup?\n\nIn real world projects, your test subject may require nontrivial setup,\nsuch as prepopulating some test data,\nand those initialization time shall be excluded from benchmark.\nHow do we achieve that?\n\nA common solution is creating a wrapper class,\nwhich has an expensive constructor and a (typically parameter-less) action method.\nAnd then you can have ``Baseline`` to check that action method.\nFor example:\n\n```python\nclass TestDriver:\n    def __init__(self, size):\n        self.data = list(range(size))\n        random.shuffle(self.data)\n    def sort(self):\n        # Some implementation to sort the self.data in-place\n\nbaseline = Baseline(\"my_baseline.bin\", threshold=2.0)\n\ndef test_my_sort_implementation():\n    driver = TestDriver(1000*1000)\n    baseline.set_or_compare(\n        driver.sort,  # A parameter-less callable can be tested as-is, without setting globals\n    )\n\n    # You may also combine the above two lines into one,\n    # and the initialization still runs only once.\n    baseline.set_or_compare(\n        TestDriver(1000*1000).sort,  # The driver initialization is still done only once\n    )\n```\n\n\n## Do NOT commit the baseline file into Git\n\nAdd your baseline filename into your ``.gitignore``, so that you won't accidentally commit it.\n\n```\nmy_baseline.bin\n```\n\nWhy?\n\nThe idea is that a performance baseline (such as 123456 ops/sec) is\nonly meaningful and consistent when running on the *same* computer.\nSwitching to a different computer, it will have a different baseline.\n\nBy not committing a baseline into the source code repo,\neach maintainer of your project (and each of their computers)\nwill have their own baseline created by the first run.\n\nThis way, you won't need to use a large threshold across different computers\n(it is impossible to specify a constant threshold that works on different computers anyway).\nPer-computer baselines all self-calibrate to match the performance of each computer.\n\n\n## How to run this in Github Action?\n\n``perf_baseline`` relies on an *updatable* baseline file,\nwhich shall be writable when a new data point (represented by a new ``name``) occurs,\nand remain read-only when an old data point (represented by same ``name``) already exists.\n\nAs of this writing, [Github's Cache Action](https://github.com/marketplace/actions/cache)\nsupports the updatable usage via some hack, inspired by\n[this hint](https://github.com/actions/toolkit/issues/505#issuecomment-1650290249).\nUse the following snippet, and modify its ``path`` and ``hashFiles(...)`` to match your filenames.\n\n```yaml\n    - name: Setup an updatable cache for Performance Baselines\n      uses: actions/cache@v3\n      with:\n        path: my_baseline.bin\n        key: ${{ runner.os }}-performance-${{ hashFiles('tests/test_benchmark.py') }}\n        restore-keys: ${{ runner.os }}-performance-\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "The perf_baseline is a performance regression detection tool for Python projects.",
    "version": "0.1.0",
    "project_urls": {
        "Changelog": "https://github.com/rayluo/perf_baseline/releases",
        "Homepage": "https://github.com/rayluo/perf_baseline"
    },
    "split_keywords": [
        "performance",
        "timeit",
        "baseline",
        "regression",
        "detection"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ace16eb6214e6c45e0ffc2add16a7cf5f5d5032c8524400c95a6f223ded51bf",
                "md5": "e50e5dfee3e434d908ffaccc57bd6fbc",
                "sha256": "bfa0afee9570360dfa5cb1163a5ed74cad42daeadff7598c828813711489235b"
            },
            "downloads": -1,
            "filename": "perf_baseline-0.1.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e50e5dfee3e434d908ffaccc57bd6fbc",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 6841,
            "upload_time": "2023-09-06T05:37:23",
            "upload_time_iso_8601": "2023-09-06T05:37:23.291657Z",
            "url": "https://files.pythonhosted.org/packages/4a/ce/16eb6214e6c45e0ffc2add16a7cf5f5d5032c8524400c95a6f223ded51bf/perf_baseline-0.1.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7c3bf5551421c4a05996bd5b841af6cc35116c4007f42878e507f0fc90a08732",
                "md5": "d0efe53c4b28e3498b68572f4b06a891",
                "sha256": "8ecb686f24a878b3f4a0076a64e3ea61227e16a487b933f5c5275f1d544a2e97"
            },
            "downloads": -1,
            "filename": "perf_baseline-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d0efe53c4b28e3498b68572f4b06a891",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7160,
            "upload_time": "2023-09-06T05:37:24",
            "upload_time_iso_8601": "2023-09-06T05:37:24.267671Z",
            "url": "https://files.pythonhosted.org/packages/7c/3b/f5551421c4a05996bd5b841af6cc35116c4007f42878e507f0fc90a08732/perf_baseline-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-06 05:37:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "rayluo",
    "github_project": "perf_baseline",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "perf-baseline"
}
        
Elapsed time: 0.14942s