-
-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add benchmarks for ctypes function call overhead #197
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[project] | ||
name = "pyperformance_bm_ctypes_argtypes" | ||
requires-python = ">=3.7" | ||
dependencies = ["pyperf"] | ||
urls = {repository = "https://github.com/python/pyperformance"} | ||
dynamic = ["version"] | ||
|
||
[tool.pyperformance] | ||
name = "ctypes_argtypes" | ||
tags = "extension" | ||
extra_opts = ["--argtypes"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <Python.h> | ||
|
||
#if defined(_WIN32) || defined(__CYGWIN__) | ||
#define EXPORTED_SYMBOL __declspec(dllexport) | ||
#else | ||
#define EXPORTED_SYMBOL | ||
ericsnowcurrently marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
|
||
|
||
EXPORTED_SYMBOL | ||
void void_foo_void(void) { | ||
|
||
} | ||
|
||
EXPORTED_SYMBOL | ||
int int_foo_int(int a) { | ||
return a + 1; | ||
} | ||
|
||
EXPORTED_SYMBOL | ||
void void_foo_int(int a) { | ||
|
||
} | ||
|
||
EXPORTED_SYMBOL | ||
void void_foo_int_int(int a, int b) { | ||
|
||
} | ||
|
||
EXPORTED_SYMBOL | ||
void void_foo_int_int_int(int a, int b, int c) { | ||
|
||
} | ||
|
||
EXPORTED_SYMBOL | ||
void void_foo_int_int_int_int(int a, int b, int c, int d) { | ||
|
||
} | ||
|
||
EXPORTED_SYMBOL | ||
void void_foo_constchar(const char* str) { | ||
|
||
} | ||
|
||
PyMODINIT_FUNC | ||
PyInit_cmodule(void) { | ||
// DELIBERATELY EMPTY | ||
|
||
// This isn't actually a Python extension module (it's used via ctypes), so | ||
// this entry point function will never be called. However, we are utilizing | ||
// setuptools to build it, and on Windows, setuptools explicitly passes the | ||
// flag /EXPORT:PyInit_cmodule, so it must be defined. | ||
return NULL; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[project] | ||
name = "pyperformance_bm_ctypes" | ||
requires-python = ">=3.7" | ||
dependencies = ["pyperf", "setuptools"] | ||
urls = {repository = "https://github.com/python/pyperformance"} | ||
dynamic = ["version"] | ||
|
||
[tool.pyperformance] | ||
name = "ctypes" | ||
tags = "extension" | ||
install_setup = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
setuptools==62.4.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
""" | ||
Test the function call overhead of ctypes. | ||
""" | ||
import pyperf | ||
|
||
|
||
import ctypes | ||
import importlib.util | ||
|
||
|
||
spec = importlib.util.find_spec("bm_ctypes.cmodule") | ||
if spec is None: | ||
raise ImportError("Can't find bm_ctypes.cmodule shared object file") | ||
ext = ctypes.cdll.LoadLibrary(spec.origin) | ||
|
||
|
||
def benchmark_argtypes(loops): | ||
void_foo_void = ext.void_foo_void | ||
void_foo_void.argtypes = [] | ||
void_foo_void.restype = None | ||
|
||
int_foo_int = ext.void_foo_int | ||
int_foo_int.argtypes = [ctypes.c_int] | ||
int_foo_int.restype = ctypes.c_int | ||
|
||
void_foo_int = ext.void_foo_int | ||
void_foo_int.argtypes = [ctypes.c_int] | ||
void_foo_int.restype = None | ||
|
||
void_foo_int_int = ext.void_foo_int_int | ||
void_foo_int_int.argtypes = [ctypes.c_int, ctypes.c_int] | ||
void_foo_int_int.restype = None | ||
|
||
void_foo_int_int_int = ext.void_foo_int_int_int | ||
void_foo_int_int_int.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int] | ||
void_foo_int_int_int.restype = None | ||
|
||
void_foo_int_int_int_int = ext.void_foo_int_int_int_int | ||
void_foo_int_int_int_int.argtypes = [ | ||
ctypes.c_int, | ||
ctypes.c_int, | ||
ctypes.c_int, | ||
ctypes.c_int, | ||
] | ||
void_foo_int_int_int_int.restype = None | ||
|
||
void_foo_constchar = ext.void_foo_constchar | ||
void_foo_constchar.argtypes = [ctypes.c_char_p] | ||
void_foo_constchar.restype = None | ||
|
||
return benchmark(loops) | ||
ericsnowcurrently marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def benchmark(loops): | ||
void_foo_void = ext.void_foo_void | ||
int_foo_int = ext.int_foo_int | ||
void_foo_int = ext.void_foo_int | ||
void_foo_int_int = ext.void_foo_int_int | ||
void_foo_int_int_int = ext.void_foo_int_int_int | ||
void_foo_int_int_int_int = ext.void_foo_int_int_int_int | ||
void_foo_constchar = ext.void_foo_constchar | ||
|
||
range_it = range(loops) | ||
|
||
# Test calling the functions using the implied arguments mechanism | ||
t0 = pyperf.perf_counter() | ||
|
||
for _ in range_it: | ||
void_foo_void() | ||
int_foo_int(1) | ||
void_foo_int(1) | ||
void_foo_int_int(1, 2) | ||
void_foo_int_int_int(1, 2, 3) | ||
void_foo_int_int_int_int(1, 2, 3, 4) | ||
void_foo_constchar(b"bytes") | ||
Comment on lines
+69
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The real benefit of micro-benchmarks is that it narrows down where performance regressions might be. With that in mind, would these different signatures have enough independent potential for regression that it would it make sense to have a separate benchmark for each? Would it be worth bothering even if they did?
ericsnowcurrently marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return pyperf.perf_counter() - t0 | ||
|
||
|
||
def add_cmdline_args(cmd, args): | ||
if args.argtypes: | ||
cmd.append("--argtypes") | ||
|
||
|
||
if __name__ == "__main__": | ||
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args) | ||
ericsnowcurrently marked this conversation as resolved.
Show resolved
Hide resolved
|
||
runner.metadata["description"] = "ctypes function call overhead benchmark" | ||
|
||
runner.argparser.add_argument("--argtypes", action="store_true") | ||
options = runner.parse_args() | ||
|
||
if options.argtypes: | ||
runner.bench_time_func("ctypes_argtypes", benchmark_argtypes) | ||
else: | ||
runner.bench_time_func("ctypes", benchmark) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from setuptools import setup, Extension | ||
|
||
# Compile the C shared object containing functions to call through ctypes. It | ||
# isn't technically a Python C extension, but this is the easiest way to build | ||
# it in a cross-platform way. | ||
ericsnowcurrently marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
setup( | ||
name="pyperformance_bm_ctypes", | ||
ext_modules=[Extension("bm_ctypes.cmodule", sources=["cmodule.c"])], | ||
package_dir={"bm_ctypes": "src"}, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,10 @@ def from_benchmarks(cls, benchmarks): | |
for bench in benchmarks or (): | ||
filename = bench.requirements_lockfile | ||
self._add_from_file(filename) | ||
if bench.setup_py: | ||
# pip doesn't support installing a setup.py, | ||
# but it does support installing from the directory it is in. | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is what I was thinking of above. Consider moving it there. |
||
self._add(bench.setup_py) | ||
return self | ||
|
||
def __init__(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason we use the dirname isn't obvious, so it may be worth adding a comment here indicating pip's limitations.