Skip to content

Commit

Permalink
Added fmt and lint to benchmarks/ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fzakaria committed Oct 4, 2023
1 parent 572c7e7 commit 92440ba
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 37 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions benchmarks/bin_symbol_size.py
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 26 additions & 17 deletions benchmarks/graph_symbol_size_benchmark.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
20 changes: 12 additions & 8 deletions benchmarks/symbol_size_benchmark.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -93,4 +97,4 @@ def sqlelf_memoized_benchmark(sqlite_database: str, num_functions: int) -> None:
)
)

pprint.pprint(data)
pprint.pprint(data) # noqa: T203
8 changes: 5 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"])


Expand Down
3 changes: 2 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
10 changes: 6 additions & 4 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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"]
Expand Down

0 comments on commit 92440ba

Please sign in to comment.