From 92440ba69416b7539697535ab5d11649e6b862ad Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 4 Oct 2023 13:29:11 +0000 Subject: [PATCH] Added fmt and lint to benchmarks/ tests --- Makefile | 8 ++--- benchmarks/bin_symbol_size.py | 26 ++++++++++++++ benchmarks/graph_symbol_size_benchmark.py | 43 ++++++++++++++--------- benchmarks/symbol_size_benchmark.py | 20 ++++++----- tests/test_cli.py | 8 +++-- tests/test_examples.py | 3 +- tests/test_sql.py | 10 +++--- 7 files changed, 81 insertions(+), 37 deletions(-) create mode 100755 benchmarks/bin_symbol_size.py diff --git a/Makefile b/Makefile index d73145e..ec7c3b9 100644 --- a/Makefile +++ b/Makefile @@ -15,13 +15,13 @@ show: ## Show the current environment. .PHONY: fmt fmt: ## Format code using black & isort. - isort sqlelf/ - black sqlelf/ + isort sqlelf/ benchmarks/ tests/ + black sqlelf/ benchmarks/ tests/ .PHONY: lint lint: ## Run pep8, black, mypy linters. - flake8 sqlelf/ - black --check sqlelf/ + flake8 sqlelf/ benchmarks/ tests/ + black --check sqlelf/ benchmarks/ tests/ pyright mypy --strict --install-types --non-interactive sqlelf tests diff --git a/benchmarks/bin_symbol_size.py b/benchmarks/bin_symbol_size.py new file mode 100755 index 0000000..1b8b625 --- /dev/null +++ b/benchmarks/bin_symbol_size.py @@ -0,0 +1,26 @@ +#! /usr/bin/env python +"""Go through /bin and print out the number of symbols in each binary.""" + +import json +import os +import subprocess +from collections import defaultdict + +data = defaultdict(lambda: 0) + +for root, dirs, files in os.walk("/bin"): + for file in files: + full_path_file = os.path.join(root, file) + if not os.path.isfile(full_path_file) or not os.access(full_path_file, os.X_OK): + pass + result = subprocess.run( + f"readelf -s {full_path_file} | wc -l", + capture_output=True, + text=True, + shell=True, + ) + num_symbols = int(result.stdout) + data[full_path_file] = num_symbols + +with open("symbols.json", "w") as f: + json.dump(data, f, indent=2) diff --git a/benchmarks/graph_symbol_size_benchmark.py b/benchmarks/graph_symbol_size_benchmark.py index 010e27e..235547d 100755 --- a/benchmarks/graph_symbol_size_benchmark.py +++ b/benchmarks/graph_symbol_size_benchmark.py @@ -1,35 +1,44 @@ #! /usr/bin/env python3 -from plotnine import * import pandas as pd -import numpy as np +from plotnine import ( + aes, + geom_line, + ggplot, + labs, + save_as_pdf_pages, + scale_x_continuous, + scale_y_log10, + theme_classic, +) # Raw data data = { "Number of Functions": [10, 100, 1000, 10000, 100000], "readelf": [ - 0.0052862250013276935, - 0.005087099008960649, - 0.005879847012693062, - 0.01378464701701887, - 0.09143825399223715, + 0.004524321004282683, + 0.004533555009402335, + 0.005225651984801516, + 0.012913715007016435, + 0.08858381301979534, ], "sqlelf": [ - 0.026774031983222812, - 0.07022259500809014, - 0.5325515430013184, - 5.426244292000774, - 51.41806371998973, + 0.02732730400748551, + 0.0717524380015675, + 0.5335653759830166, + 5.1397658770147245, + 51.25160684299772, ], "sqlelf-memoized": [ - 0.0006310690077953041, - 0.0012372269993647933, - 0.0005934310029260814, - 0.0008586860203649849, - 0.0030715609900653362, + 0.000236856983974576, + 0.00015781400725245476, + 0.0001789960078895092, + 0.0003311840118840337, + 0.002041122002992779, ], } + # Create the pandas DataFrame df = pd.DataFrame(data=data) diff --git a/benchmarks/symbol_size_benchmark.py b/benchmarks/symbol_size_benchmark.py index bff2226..56c8fe0 100755 --- a/benchmarks/symbol_size_benchmark.py +++ b/benchmarks/symbol_size_benchmark.py @@ -1,12 +1,16 @@ #! /usr/bin/env python3 -"""A benchmark to measure the time it takes to load a binary with a given number of functions.""" -import timeit -import tempfile -import subprocess -from sqlelf import sql, elf +"""A benchmark to measure the time it takes to load a + binary with a given number of functions. + +Afterwards, run the file graph_symbol_size_benchmark.py to +generate a graph of the results.""" import pprint import sqlite3 -import time +import subprocess +import tempfile +import timeit + +from sqlelf import elf, sql def create_executable_file( @@ -61,7 +65,7 @@ def sqlelf_memoized_benchmark(sqlite_database: str, num_functions: int) -> None: num_functions = 10**exponent data["Number of Functions"].append(num_functions) - print(f"Number of functions: {num_functions}") + print(f"Number of functions: {num_functions}") # noqa: T201 # create the executable with tempfile.NamedTemporaryFile(mode="w") as file: file_name = file.name @@ -93,4 +97,4 @@ def sqlelf_memoized_benchmark(sqlite_database: str, num_functions: int) -> None: ) ) -pprint.pprint(data) +pprint.pprint(data) # noqa: T203 diff --git a/tests/test_cli.py b/tests/test_cli.py index 85f1881..31ac345 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,9 @@ -from sqlelf import cli -import pytest from io import StringIO +import pytest + +from sqlelf import cli + def test_cli_bad_arguments() -> None: with pytest.raises(SystemExit): @@ -19,7 +21,7 @@ def test_cli_single_file_arguments() -> None: def test_cli_single_non_existent_file_arguments() -> None: - with pytest.raises(SystemExit) as err: + with pytest.raises(SystemExit): cli.start(["does_not_exist"]) diff --git a/tests/test_examples.py b/tests/test_examples.py index 017a770..9a93946 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -1,8 +1,9 @@ """This file tests the examples in the README.md file""" -from sqlelf import sql, elf import pytest +from sqlelf import elf, sql + @pytest.mark.slow def test_symbol_resolutions() -> None: diff --git a/tests/test_sql.py b/tests/test_sql.py index eefd168..04099af 100644 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -1,8 +1,10 @@ -from sqlelf import sql -import lief from unittest.mock import patch + +import lief import sh # type: ignore +from sqlelf import sql + def test_simple_binary_real() -> None: binary = lief.parse("/bin/ls") @@ -21,14 +23,14 @@ def test_simple_binary_mocked(Command: sh.Command) -> None: fake.so.6 => /some-path/fake.so.6 libc.so.6 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib/libc.so.6 (0x00007f6995bac000) /lib64/ld-linux-x86-64.so.2 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib64/ld-linux-x86-64.so.2 (0x00007f6995dc1000) - """ + """ # noqa: E501 Command(interpreter).return_value = expected_return_value # pyright: ignore result = sql.find_libraries(binary) assert len(result) == 4 assert result["fake.so.6"] == "/some-path/fake.so.6" assert ( result["/lib64/ld-linux-x86-64.so.2"] - == "/nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib64/ld-linux-x86-64.so.2" + == "/nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib64/ld-linux-x86-64.so.2" # noqa: E501 ) assert ( result["libc.so.6"]