-
Notifications
You must be signed in to change notification settings - Fork 42
feat: Pytest benchmark for comparing against other engines locally #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
37a6cc9
Initial support
petern48 b3dbc33
Create tables of geometry columns in advance and use them
petern48 2078c36
move benchmarks folder from python to root folder
petern48 0263b8a
Parametrize st_area as an example
petern48 2ee15cf
Use 'simple' and 'complex' tables instead of different sized columns
petern48 3057d84
Parametrize rest of functions
petern48 987d6d9
Add licenses
petern48 b2304fd
Add benchmarks/README.md
petern48 18a9a34
Update README.md
petern48 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <!-- Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. --> | ||
|
|
||
| # Running Benchmarks | ||
|
|
||
| ## pytest-benchmark | ||
|
|
||
| These benchmarks provide a convenient way to compare the results of running queries on sedona-db to other engines like DuckDB and postgis. | ||
|
|
||
| ### Setup | ||
|
|
||
| Install pytest-benchmark: | ||
| ```bash | ||
| pip install pytest-benchmark | ||
| ``` | ||
|
|
||
| ### Running benchmarks | ||
|
|
||
| The below commands assume your working directory is in `benchmarks`. | ||
|
|
||
| ```bash | ||
| cd benchmarks/ | ||
| ``` | ||
|
|
||
| To run a benchmark, simply run the corresponding test function. For example, to run the benchmarks for st_buffer, you can run | ||
|
|
||
| ```bash | ||
| pytest test_functions.py::TestBenchFunctions::test_st_buffer | ||
| ``` | ||
|
|
||
| Most of the time, you'll also want to group by `param:table` or `func` (function) by using the `--benchmark-group-by=param:table` flag. pytest-benchmark will highlight the "best" value in green (e.g fastest for median, lowest for stddev) and "worse" value in red for each column per each group. | ||
|
|
||
| ```bash | ||
| pytest --benchmark-group-by=param:table test_functions.py::TestBenchFunctions::test_st_buffer | ||
| ``` | ||
|
|
||
| You can also reduce the number of columns that display by using the `--benchmark-columns` flag. | ||
|
|
||
| ```bash | ||
| pytest --benchmark-group-by=param:table --benchmark-columns=median,mean,stddev test_functions.py::TestBenchFunctions::test_st_buffer | ||
| ``` | ||
|
|
||
| Example output of the last command: | ||
|
|
||
| ``` | ||
| ----------------------------- benchmark 'table=collections_complex': 3 tests ----------------------------- | ||
| Name (time in ms) Median Mean StdDev | ||
| ---------------------------------------------------------------------------------------------------------- | ||
| test_st_buffer[collections_complex-SedonaDB] 87.0095 (1.0) 87.7874 (1.0) 3.7269 (1.0) | ||
| test_st_buffer[collections_complex-DuckDB] 440.4810 (5.06) 444.6948 (5.07) 12.1143 (3.25) | ||
| test_st_buffer[collections_complex-PostGIS] 864.5841 (9.94) 883.3661 (10.06) 50.4996 (13.55) | ||
| ---------------------------------------------------------------------------------------------------------- | ||
|
|
||
| ---------------------------- benchmark 'table=collections_simple': 3 tests ----------------------------- | ||
| Name (time in ms) Median Mean StdDev | ||
| -------------------------------------------------------------------------------------------------------- | ||
| test_st_buffer[collections_simple-SedonaDB] 85.8510 (1.0) 86.5050 (1.0) 3.8481 (1.0) | ||
| test_st_buffer[collections_simple-DuckDB] 442.6664 (5.16) 444.5187 (5.14) 5.6186 (1.46) | ||
| test_st_buffer[collections_simple-PostGIS] 855.3329 (9.96) 854.7194 (9.88) 7.6190 (1.98) | ||
| -------------------------------------------------------------------------------------------------------- | ||
| ``` | ||
|
|
||
| For more details and command line options, refer to the official [pytest-benchmark documentation](https://pytest-benchmark.readthedocs.io/en/latest/usage.html) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import json | ||
| from sedonadb.testing import DuckDB, PostGIS, SedonaDB | ||
|
|
||
|
|
||
| class TestBenchBase: | ||
| def setup_class(self): | ||
| self.sedonadb = SedonaDB.create_or_skip() | ||
| self.postgis = PostGIS.create_or_skip() | ||
| self.duckdb = DuckDB.create_or_skip() | ||
|
|
||
| num_geoms = 100_000 | ||
|
|
||
| # Setup tables | ||
| for name, options in [ | ||
| ( | ||
| "segments_large", | ||
| { | ||
| "geom_type": "LineString", | ||
| "target_rows": num_geoms, | ||
| "vertices_per_linestring_range": [2, 2], | ||
| }, | ||
| ), | ||
| ( | ||
| "polygons_simple", | ||
| { | ||
| "geom_type": "Polygon", | ||
| "target_rows": num_geoms, | ||
| "vertices_per_linestring_range": [10, 10], | ||
| }, | ||
| ), | ||
| ( | ||
| "polygons_complex", | ||
| { | ||
| "geom_type": "Polygon", | ||
| "target_rows": num_geoms, | ||
| "vertices_per_linestring_range": [500, 500], | ||
| }, | ||
| ), | ||
| ( | ||
| "collections_simple", | ||
| { | ||
| "geom_type": "GeometryCollection", | ||
| "target_rows": num_geoms, | ||
| "vertices_per_linestring_range": [10, 10], | ||
| }, | ||
| ), | ||
| ( | ||
| "collections_complex", | ||
| { | ||
| "geom_type": "GeometryCollection", | ||
| "target_rows": num_geoms, | ||
| "vertices_per_linestring_range": [500, 500], | ||
| }, | ||
| ), | ||
| ]: | ||
| # Generate synthetic data | ||
| query = f""" | ||
| SELECT | ||
| geometry as geom1, | ||
| geometry as geom2, | ||
| round(random() * 100) as integer | ||
| FROM sd_random_geometry('{json.dumps(options)}') | ||
| """ | ||
| tab = self.sedonadb.execute_and_collect(query) | ||
|
|
||
| self.sedonadb.create_table_arrow(name, tab) | ||
| self.postgis.create_table_arrow(name, tab) | ||
| self.duckdb.create_table_arrow(name, tab) | ||
|
|
||
| def _get_eng(self, eng): | ||
| if eng == SedonaDB: | ||
| return self.sedonadb | ||
| elif eng == PostGIS: | ||
| return self.postgis | ||
| elif eng == DuckDB: | ||
| return self.duckdb | ||
| else: | ||
| raise ValueError(f"Unsupported engine: {eng}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import pytest | ||
| from test_bench_base import TestBenchBase | ||
| from sedonadb.testing import DuckDB, PostGIS, SedonaDB | ||
|
|
||
|
|
||
| class TestBenchPredicates(TestBenchBase): | ||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "polygons_simple", | ||
| "polygons_complex", | ||
| ], | ||
| ) | ||
| def test_st_distance(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_Distance(geom1, geom2) from {table}") | ||
|
|
||
| benchmark(queries) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import pytest | ||
| from test_bench_base import TestBenchBase | ||
| from sedonadb.testing import DuckDB, PostGIS, SedonaDB | ||
|
|
||
|
|
||
| class TestBenchFunctions(TestBenchBase): | ||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "polygons_simple", | ||
| "polygons_complex", | ||
| ], | ||
| ) | ||
| def test_st_area(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_Area(geom1) from {table}") | ||
|
|
||
| benchmark(queries) | ||
|
|
||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "collections_simple", | ||
| "collections_complex", | ||
| ], | ||
| ) | ||
| def test_st_buffer(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_Buffer(geom1, 2.0) from {table}") | ||
|
|
||
| benchmark(queries) | ||
|
|
||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "polygons_simple", | ||
| "polygons_complex", | ||
| ], | ||
| ) | ||
| def test_st_centroid(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_Centroid(geom1) from {table}") | ||
|
|
||
| benchmark(queries) | ||
|
|
||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "collections_simple", | ||
| "collections_complex", | ||
| ], | ||
| ) | ||
| def test_st_dimension(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_Dimension(geom1) from {table}") | ||
|
|
||
| benchmark(queries) | ||
|
|
||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "collections_simple", | ||
| "collections_complex", | ||
| ], | ||
| ) | ||
| def test_st_envelope(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_Envelope(geom1) from {table}") | ||
|
|
||
| benchmark(queries) | ||
|
|
||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "collections_simple", | ||
| "collections_complex", | ||
| ], | ||
| ) | ||
| def test_st_geometrytype(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_GeometryType(geom1) from {table}") | ||
|
|
||
| benchmark(queries) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import pytest | ||
| from test_bench_base import TestBenchBase | ||
| from sedonadb.testing import DuckDB, PostGIS, SedonaDB | ||
|
|
||
|
|
||
| class TestBenchPredicates(TestBenchBase): | ||
| @pytest.mark.parametrize("eng", [SedonaDB, PostGIS, DuckDB]) | ||
| @pytest.mark.parametrize( | ||
| "table", | ||
| [ | ||
| "polygons_simple", | ||
| "polygons_complex", | ||
| ], | ||
| ) | ||
| def test_st_difference(self, benchmark, eng, table): | ||
| eng = self._get_eng(eng) | ||
|
|
||
| def queries(): | ||
| eng.execute_and_collect(f"SELECT ST_Difference(geom1, geom2) from {table}") | ||
|
|
||
| benchmark(queries) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.