Skip to content

Commit 2d03e8c

Browse files
authored
Merge pull request #42 from ayasyrev/dev
0.2.4
2 parents 891159c + c36da38 commit 2d03e8c

21 files changed

+723
-111
lines changed

.flake8

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
select = C,E,F,W
33
max-complexity = 10
44
max-line-length = 120
5-
5+
extend-ignore = W503
6+
disable-noqa = True
67
application-import-names = benchmark_utils, tests
78
import-order-style = google
89

.github/workflows/coverage.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ jobs:
88
coverage:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v3
12-
- uses: actions/setup-python@v1
11+
- uses: actions/checkout@v4
12+
- uses: actions/setup-python@v2
1313
with:
14-
python-version: "3.10"
14+
python-version: "3.11"
1515
architecture: x64
1616
- run: pip install nox
17-
- run: nox -f noxfile_cov.py -p "3.10"
17+
- run: nox -f noxfile_cov.py -p "3.11"
1818
env:
1919
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}

.github/workflows/deploy_docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
deploy:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v2
11+
- uses: actions/checkout@v4
1212
- uses: actions/setup-python@v2
1313
with:
1414
python-version: 3.x

.github/workflows/lint.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
tests:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v3
12-
- uses: actions/setup-python@v1
11+
- uses: actions/checkout@v4
12+
- uses: actions/setup-python@v2
1313
with:
14-
python-version: "3.10"
14+
python-version: "3.11"
1515
architecture: x64
1616
- run: pip install nox
17-
- run: nox -p "3.10" -f noxfile_lint.py
17+
- run: nox -p "3.11" -f noxfile_lint.py

.github/workflows/tests.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
python: ["3.7", "3.8", "3.9", "3.10", "3.11"]
12+
python: ["3.8", "3.9", "3.10", "3.11",]
1313
name: Python ${{ matrix.python }}
1414
steps:
15-
- uses: actions/checkout@v3
16-
- uses: actions/setup-python@v1
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-python@v2
1717
with:
1818
python-version: ${{ matrix.python }}
1919
architecture: x64

README.md

+128-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Utils for benchmark - wrapper over python timeit.
66
[![PyPI Status](https://badge.fury.io/py/benchmark-utils.svg)](https://badge.fury.io/py/benchmark-utils)
77
[![Tests](https://github.com/ayasyrev/benchmark_utils/workflows/Tests/badge.svg)](https://github.com/ayasyrev/benchmark_utils/actions?workflow=Tests) [![Codecov](https://codecov.io/gh/ayasyrev/benchmark_utils/branch/main/graph/badge.svg)](https://codecov.io/gh/ayasyrev/benchmark_utils)
88

9-
Tested on python 3.7 - 3.11
9+
Tested on python 3.7 - 3.12
1010

1111
## Install
1212

@@ -20,12 +20,13 @@ Or install from github repo:
2020

2121
## Basic use.
2222

23-
Lets benchmark some (dump) functions.
23+
Lets benchmark some (dummy) functions.
2424

2525

2626
```python
2727
from time import sleep
2828

29+
2930
def func_to_test_1(sleep_time: float = 0.1, mult: int = 1) -> None:
3031
"""simple 'sleep' func for test"""
3132
sleep(sleep_time * mult)
@@ -34,7 +35,6 @@ def func_to_test_1(sleep_time: float = 0.1, mult: int = 1) -> None:
3435
def func_to_test_2(sleep_time: float = 0.11, mult: int = 1) -> None:
3536
"""simple 'sleep' func for test"""
3637
sleep(sleep_time * mult)
37-
3838
```
3939

4040
Let's create benchmark.
@@ -56,7 +56,9 @@ bench = Benchmark(
5656
bench
5757
```
5858
<details open> <summary>output</summary>
59-
<pre>Benchmark(func_to_test_1, func_to_test_2)</pre>
59+
<pre>
60+
Benchmark(func_to_test_1, func_to_test_2)
61+
</pre>
6062
</details>
6163

6264

@@ -65,6 +67,7 @@ Now we can benchmark that functions.
6567

6668

6769
```python
70+
# we can run bench.run() or just:
6871
bench()
6972
```
7073
<details open> <summary>output</summary>
@@ -80,7 +83,7 @@ bench()
8083

8184

8285

83-
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_2: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-9.6</span>%
86+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_2: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-8.9</span>%
8487
</pre>
8588

8689
</details>
@@ -105,7 +108,7 @@ bench.run(num_repeats=10)
105108

106109

107110

108-
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_2: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-8.8</span>%
111+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_2: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-9.3</span>%
109112
</pre>
110113

111114
</details>
@@ -130,30 +133,33 @@ bench.print_results(reverse=True)
130133

131134

132135

133-
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_1: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.10</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">9.7</span>%
136+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">func_to_test_1: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.10</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">10.2</span>%
134137
</pre>
135138

136139
</details>
137140

138141

139-
We can add functions to bencmark as list of funtions (or partial) ar as dictionary: `{"name": function}`.
142+
We can add functions to benchmark as list of functions (or partial) or as dictionary: `{"name": function}`.
140143

141144

142145
```python
143-
bench = Benchmark([
144-
func_to_test_1,
145-
partial(func_to_test_1, 0.12),
146-
partial(func_to_test_1, sleep_time=0.11),
147-
])
148-
146+
bench = Benchmark(
147+
[
148+
func_to_test_1,
149+
partial(func_to_test_1, 0.12),
150+
partial(func_to_test_1, sleep_time=0.11),
151+
]
152+
)
149153
```
150154

151155

152156
```python
153157
bench
154158
```
155159
<details open> <summary>output</summary>
156-
<pre>Benchmark(func_to_test_1, func_to_test_1(0.12), func_to_test_1(sleep_time=0.11))</pre>
160+
<pre>
161+
Benchmark(func_to_test_1, func_to_test_1(0.12), func_to_test_1(sleep_time=0.11))
162+
</pre>
157163
</details>
158164

159165

@@ -175,32 +181,132 @@ bench.run()
175181

176182

177183

178-
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800080; text-decoration-color: #800080; font-weight: bold">func_to_test_1</span><span style="font-weight: bold">(</span><span style="color: #808000; text-decoration-color: #808000">sleep_time</span>=<span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span><span style="font-weight: bold">)</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-8.9</span>%
184+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800080; text-decoration-color: #800080; font-weight: bold">func_to_test_1</span><span style="font-weight: bold">(</span><span style="color: #808000; text-decoration-color: #808000">sleep_time</span>=<span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span><span style="font-weight: bold">)</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.11</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-8.8</span>%
179185
</pre>
180186

181187

182188

183189

184-
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800080; text-decoration-color: #800080; font-weight: bold">func_to_test_1</span><span style="font-weight: bold">(</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.12</span><span style="font-weight: bold">)</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.12</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-16.5</span>%
190+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800080; text-decoration-color: #800080; font-weight: bold">func_to_test_1</span><span style="font-weight: bold">(</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.12</span><span style="font-weight: bold">)</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.12</span> <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">-16.4</span>%
185191
</pre>
186192

187193
</details>
188194

189195

190196

191197
```python
192-
bench = Benchmark({
193-
"func_1": func_to_test_1,
194-
"func_2": func_to_test_2,
195-
})
198+
bench = Benchmark(
199+
{
200+
"func_1": func_to_test_1,
201+
"func_2": func_to_test_2,
202+
}
203+
)
196204
```
197205

198206

199207
```python
200208
bench
201209
```
202210
<details open> <summary>output</summary>
203-
<pre>Benchmark(func_1, func_2)</pre>
211+
<pre>
212+
Benchmark(func_1, func_2)
213+
</pre>
214+
</details>
215+
216+
217+
218+
When we run benchmark script in terminal, we got pretty progress thanks to rich. Lets run example_1.py from example folder:
219+
220+
![exaple_1](images/run_example_1.gif)
221+
222+
# BenchmarkIter
223+
224+
With BenchmarkIter we can benchmark functions over iterables, for example read list of files or run functions with different arguments.
225+
226+
227+
```python
228+
def func_to_test_1(x: int) -> None:
229+
"""simple 'sleep' func for test"""
230+
sleep(0.01)
231+
232+
233+
def func_to_test_2(x: int) -> None:
234+
"""simple 'sleep' func for test"""
235+
sleep(0.015)
236+
237+
238+
dummy_params = list(range(10))
239+
```
240+
241+
242+
```python
243+
from benchmark_utils import BenchmarkIter
244+
245+
bench = BenchmarkIter(
246+
func=[func_to_test_1, func_to_test_2],
247+
item_list=dummy_params,
248+
)
249+
```
250+
251+
252+
```python
253+
bench()
254+
```
255+
<details open> <summary>output</summary>
256+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Items/sec
257+
</pre>
258+
259+
</details>
260+
261+
262+
263+
264+
265+
266+
267+
268+
269+
270+
We can run it again, all functions, some of it, exclude some and change number of repeats.
271+
And we can limit number of items with `num_samples` argument:
272+
`bench.run(num_samples=5)`
273+
274+
# Multiprocessing
275+
276+
By default we tun functions in one thread.
277+
But we can use multiprocessing with `multiprocessing=True` argument:
278+
`bench.run(multiprocessing=True)`
279+
It will use all available cpu cores.
280+
And we can use `num_workers` argument to limit used cpu cores:
281+
`bench.run(multiprocessing=True, num_workers=2)`
282+
283+
284+
```python
285+
bench.run(multiprocessing=True, num_workers=2)
286+
```
287+
<details open> <summary>output</summary>
288+
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> Func name | Items/sec
289+
</pre>
290+
291+
</details>
292+
293+
294+
295+
296+
297+
298+
299+
300+
301+
302+
303+
```python
304+
0.1022316165981465 / 0.09319195459829643
305+
```
306+
<details open> <summary>output</summary>
307+
<pre>
308+
1.0970004550158379
309+
</pre>
204310
</details>
205311

206312

0 commit comments

Comments
 (0)