import os
import json
import tabulate
def pretty_time_diff_from_ns(nanoseconds):
units = [
(1, "ns"), (1e3, "μs"), (1e6, "ms"), (1e9, "s"), (60e9, "min"), (3600e9, "h"), (86400e9, "d") ]
for factor, unit in units:
if nanoseconds < factor:
break
time_amount = nanoseconds / (factor / 1000)
return f"{time_amount:.2f} {unit}"
def extract_mean_point_estimate(estimates_path):
with open(estimates_path, "r") as file:
data = json.load(file)
return data["mean"]["point_estimate"]
def calculate_ratios(criterion_dir):
comparisons = {}
subcommand_benches = [d for d in os.listdir(criterion_dir) if d != "report"]
for subcommand in subcommand_benches:
benches = dict()
runs = os.listdir(os.path.join(criterion_dir, subcommand))
for dir in runs:
if dir.startswith("report"):
continue
if dir.startswith("bedtools"):
bedtools_bench = os.path.join(
criterion_dir, subcommand, dir, "new", "estimates.json"
)
benches["bedtools"] = extract_mean_point_estimate(bedtools_bench)
if dir.startswith("granges"):
granges_bench = os.path.join(
criterion_dir, subcommand, dir, "new", "estimates.json"
)
benches["granges"] = extract_mean_point_estimate(granges_bench)
comparisons[subcommand] = benches
headers = ["command", "bedtools time", "granges time", "granges speedup (%)"]
table = []
for comparison, tools in comparisons.items():
if len(tools) == 2:
bedtools_time, granges_time = tools.values()
percent_faster = ((tools["bedtools"] - tools["granges"]) / tools["bedtools"]) * 100
table.append([comparison, pretty_time_diff_from_ns(tools["bedtools"]),
pretty_time_diff_from_ns(tools["granges"]), percent_faster])
print(tabulate.tabulate(table, headers=headers))
def main():
criterion_dir = "target/criterion"
calculate_ratios(criterion_dir)
if __name__ == "__main__":
main()