def benchmark(scenario, operation, metric, num_samples):
    scenario_established = scenario.start()

    samples = []

    def collect_samples(ignored):
        collecting = Deferred()
        task = cooperate(
            sample(operation, metric, i).addCallback(samples.append)
            for i in range(num_samples))

        # If the scenario collapses, stop sampling
        def stop_sampling_on_scenario_collapse(failure):
            task.stop()
            collecting.errback(failure)
        scenario.maintained().addErrback(stop_sampling_on_scenario_collapse)

        # Leaving the errback unhandled makes tests fail
        task.whenDone().addCallbacks(
            lambda ignored: collecting.callback(samples),
            lambda ignored: None)

        return collecting

    benchmarking = scenario_established.addCallback(collect_samples)

    def stop_scenario(samples):
        d = scenario.stop()

        def combine_results(scenario_metrics):
            return (samples, scenario_metrics)
        d.addCallback(combine_results)

        return d
    benchmarking.addCallbacks(
        stop_scenario,
        bypass, errbackArgs=[scenario.stop]
    )

    return benchmarking
Perform benchmarking of the operation within a scenario.
dataset/ETHPy150Open ClusterHQ/flocker/benchmark/_driver.py/benchmark