mirror of
https://github.com/mandiant/capa.git
synced 2025-12-22 07:10:29 -08:00
replace tqdm, termcolor, tabulate with rich (#2374)
* logging: use rich handler for logging * tqdm: remove unneeded redirecting_print_to_tqdm function * tqdm: introduce `CapaProgressBar` rich `Progress` bar * tqdm: replace tqdm with rich Progress bar * tqdm: remove tqdm dependency * termcolor: replace termcolor and update `scripts/` * tests: update `test_render.py` to use rich.console.Console * termcolor: remove termcolor dependency * capa.render.utils: add `write` & `writeln` methods to subclass `Console` * update markup util functions to use fmt strings * tests: update `test_render.py` to use `capa.render.utils.Console` * replace kwarg `end=""` with `write` and `writeln` methods * tabulate: replace tabulate with `rich.table` * tabulate: remove `tabulate` and its dependency `wcwidth` * logging: handle logging in `capa.main` * logging: set up logging in `capa.main` this commit sets up logging in `capa.main` and uses a shared `log_console` in `capa.helpers` for logging purposes * changelog: replace packages with rich * remove entry from pyinstaller and unneeded progress.update call * update requirements.txt * scripts: use `capa.helpers.log_console` in `CapaProgressBar` * logging: configure root logger to use `RichHandler` * remove unused import `inspect`
This commit is contained in:
@@ -42,9 +42,10 @@ import logging
|
||||
import argparse
|
||||
import subprocess
|
||||
|
||||
import tqdm
|
||||
import humanize
|
||||
import tabulate
|
||||
from rich import box
|
||||
from rich.table import Table
|
||||
from rich.console import Console
|
||||
|
||||
import capa.main
|
||||
import capa.perf
|
||||
@@ -92,51 +93,61 @@ def main(argv=None):
|
||||
except capa.main.ShouldExitError as e:
|
||||
return e.status_code
|
||||
|
||||
with tqdm.tqdm(total=args.number * args.repeat, leave=False) as pbar:
|
||||
with capa.helpers.CapaProgressBar(console=capa.helpers.log_console) as progress:
|
||||
total_iterations = args.number * args.repeat
|
||||
task = progress.add_task("profiling", total=total_iterations)
|
||||
|
||||
def do_iteration():
|
||||
capa.perf.reset()
|
||||
capa.capabilities.common.find_capabilities(rules, extractor, disable_progress=True)
|
||||
pbar.update(1)
|
||||
|
||||
progress.advance(task)
|
||||
|
||||
samples = timeit.repeat(do_iteration, number=args.number, repeat=args.repeat)
|
||||
|
||||
logger.debug("perf: find capabilities: min: %0.2fs", (min(samples) / float(args.number)))
|
||||
logger.debug("perf: find capabilities: avg: %0.2fs", (sum(samples) / float(args.repeat) / float(args.number)))
|
||||
logger.debug(
|
||||
"perf: find capabilities: avg: %0.2fs",
|
||||
(sum(samples) / float(args.repeat) / float(args.number)),
|
||||
)
|
||||
logger.debug("perf: find capabilities: max: %0.2fs", (max(samples) / float(args.number)))
|
||||
|
||||
for counter, count in capa.perf.counters.most_common():
|
||||
logger.debug("perf: counter: %s: %s", counter, count)
|
||||
|
||||
print(
|
||||
tabulate.tabulate(
|
||||
[(counter, humanize.intcomma(count)) for counter, count in capa.perf.counters.most_common()],
|
||||
headers=["feature class", "evaluation count"],
|
||||
tablefmt="github",
|
||||
)
|
||||
)
|
||||
print()
|
||||
console = Console()
|
||||
|
||||
print(
|
||||
tabulate.tabulate(
|
||||
[
|
||||
(
|
||||
args.label,
|
||||
"{:,}".format(capa.perf.counters["evaluate.feature"]),
|
||||
# python documentation indicates that min(samples) should be preferred,
|
||||
# so lets put that first.
|
||||
#
|
||||
# https://docs.python.org/3/library/timeit.html#timeit.Timer.repeat
|
||||
f"{(min(samples) / float(args.number)):.2f}s",
|
||||
f"{(sum(samples) / float(args.repeat) / float(args.number)):.2f}s",
|
||||
f"{(max(samples) / float(args.number)):.2f}s",
|
||||
)
|
||||
],
|
||||
headers=["label", "count(evaluations)", "min(time)", "avg(time)", "max(time)"],
|
||||
tablefmt="github",
|
||||
)
|
||||
table1 = Table(box=box.MARKDOWN)
|
||||
table1.add_column("feature class")
|
||||
table1.add_column("evaluation count")
|
||||
|
||||
for counter, count in capa.perf.counters.most_common():
|
||||
table1.add_row(counter, humanize.intcomma(count))
|
||||
|
||||
console.print(table1)
|
||||
console.print()
|
||||
|
||||
table2 = Table(box=box.MARKDOWN)
|
||||
table2.add_column("label")
|
||||
table2.add_column("count(evaluations)", style="magenta")
|
||||
table2.add_column("min(time)", style="green")
|
||||
table2.add_column("avg(time)", style="yellow")
|
||||
table2.add_column("max(time)", style="red")
|
||||
|
||||
table2.add_row(
|
||||
args.label,
|
||||
# python documentation indicates that min(samples) should be preferred,
|
||||
# so lets put that first.
|
||||
#
|
||||
# https://docs.python.org/3/library/timeit.html#timeit.Timer.repeat
|
||||
"{:,}".format(capa.perf.counters["evaluate.feature"]),
|
||||
f"{(min(samples) / float(args.number)):.2f}s",
|
||||
f"{(sum(samples) / float(args.repeat) / float(args.number)):.2f}s",
|
||||
f"{(max(samples) / float(args.number)):.2f}s",
|
||||
)
|
||||
|
||||
console.print(table2)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user