diff --git a/swh/scheduler/cli/simulator.py b/swh/scheduler/cli/simulator.py --- a/swh/scheduler/cli/simulator.py +++ b/swh/scheduler/cli/simulator.py @@ -45,8 +45,14 @@ help="Scheduling policy to simulate (only for origin_scheduler)", ) @click.option("--runtime", "-t", type=float, help="Simulated runtime") +@click.option( + "--plots/--no-plots", + "-H", + "showplots", + help="Show results as plots (with plotille)", +) @click.pass_context -def run_command(ctx, scheduler, policy, runtime): +def run_command(ctx, scheduler, policy, runtime, showplots): """Run the scheduler simulator. By default, the simulation runs forever. You can cap the simulated runtime @@ -60,9 +66,11 @@ from swh.scheduler.simulator import run policy = policy if scheduler == "origin_scheduler" else None - run( + report = run( scheduler=ctx.obj["scheduler"], scheduler_type=scheduler, policy=policy, runtime=runtime, ) + + print(report.format(with_plots=showplots)) diff --git a/swh/scheduler/simulator/__init__.py b/swh/scheduler/simulator/__init__.py --- a/swh/scheduler/simulator/__init__.py +++ b/swh/scheduler/simulator/__init__.py @@ -163,4 +163,4 @@ print("total simulated time:", end_time - start_time) metrics = env.scheduler.update_metrics(timestamp=end_time) env.report.record_metrics(end_time, metrics) - print(env.report.format()) + return env.report diff --git a/swh/scheduler/simulator/common.py b/swh/scheduler/simulator/common.py --- a/swh/scheduler/simulator/common.py +++ b/swh/scheduler/simulator/common.py @@ -90,31 +90,33 @@ return figure.show(legend=True) - def format(self): + def format(self, with_plots=True): full_visits = self.visit_runtimes.get(("full", True), []) - histogram = self.runtime_histogram("full", True) - plot = self.metrics_plot() long_tasks = sum(runtime > self.DURATION_THRESHOLD for runtime in full_visits) - return ( - textwrap.dedent( - f"""\ + output = textwrap.dedent( + f"""\ Total visits: {self.total_visits} Uneventful visits: {self.uneventful_visits} Eventful visits: {len(full_visits)} Very long running tasks: {long_tasks} - Visit time histogram for eventful visits: """ - ) - + histogram - + "\n" - + textwrap.dedent( - """\ - Metrics over time: - """ - ) - + plot ) + if with_plots: + histogram = self.runtime_histogram("full", True) + plot = self.metrics_plot() + output += ( + "Visit time histogram for eventful visits:" + + histogram + + "\n" + + textwrap.dedent( + """\ + Metrics over time: + """ + ) + + plot + ) + return output @dataclass