class TestWhenThereAreMoreThreadsThanMaxThreads(TestSampler):
    @before
    def before(self):
        super().before()
        self.environment["max_threads"] = 1
        self.subject = Sampler(environment=self.environment)

    def test_it_calls_the_get_stacks_method_with_a_subset_of_the_threads(self):
        self.subject.sample()

        allowed_results = [
            mock.call(
                threads_to_sample=[("fake_thread_1", "fake_thread_frames_1")],
                excluded_threads=ANY,
                max_depth=ANY,
            ),
            mock.call(
                threads_to_sample=[("fake_thread_2", "fake_thread_frames_2")],
                excluded_threads=ANY,
                max_depth=ANY,
            ),
        ]

        assert (self.mock_get_stacks.call_args in allowed_results)

    def test_it_includes_the_number_of_threads_it_attempted_to_sample_and_how_many_in_total_were_seen_in_the_system(
            self):
        self.mock_get_stacks.result = [["dummy_stack_sample"]]

        result = self.subject.sample()

        assert (result.attempted_sample_threads_count == 1)
        assert (result.seen_threads_count == 2)
    def test_it_includes_the_number_of_threads_it_attempted_to_sample_and_how_many_in_total_were_seen_in_the_system(
            self):
        self.mock_get_stacks.result = [["dummy_stack_sample"]]
        sampler = Sampler(environment=self.environment)

        result = sampler.sample()

        assert (result.attempted_sample_threads_count == 2)
        assert (result.seen_threads_count == 2)
    def test_it_calls_the_get_stacks_method_with_the_custom_excluded_threads_list(
            self):
        self.environment["excluded_threads"] = {"exclude_me"}
        sampler = Sampler(environment=self.environment)

        sampler.sample()

        self.mock_get_stacks.assert_called_once_with(
            threads_to_sample=ANY,
            excluded_threads={"exclude_me"},
            max_depth=ANY)
    def test_it_calls_the_get_stacks_method_with_the_current_threads_and_the_default_excluded_threads_and_default_max_depth(
            self):
        default_excluded_threads = set()
        default_max_depth = 1000

        sampler = Sampler(environment=self.environment)

        sampler.sample()

        self.mock_get_stacks.assert_called_once_with(
            threads_to_sample=self._current_frames_reply.items(),
            excluded_threads=default_excluded_threads,
            max_depth=default_max_depth,
        )
Beispiel #5
0
    def __init__(self, environment=dict()):
        """
        :param environment: dependency container dictionary for the current profiler
        :param sampling_interval: (required inside environment) delay between profile reports in datetime.timedelta
        :param killswitch_filepath: (required inside environment) filepath pointing to the killswitch file. This path
            gets checked every time the profiler samples; the profiler is immediately stopped if this file exists.
        :param collector: (required inside environment) collector object to handle sample processing
        :param initial_sampling_interval: (required inside environment) Initial delay signal sampler takes for starting
        to sample
        :param profiler_thread_name: (required inside environment) Thread name used for running the
        report_orchestration_scheduler
        """
        self.timer = environment.get("timer")
        self.sampler = environment.get("sampler") or Sampler(
            environment=environment)

        self.scheduler = Scheduler(
            command=self._profiling_command,
            delay_provider=lambda: AgentConfiguration.get().sampling_interval,
            initial_delay=environment["initial_sampling_interval"],
            thread_name=environment["profiler_thread_name"])
        self.collector = environment["collector"]
        self.profiler_disabler = environment["profiler_disabler"]
        self.is_profiling_in_progress = False
        self._first_execution = True
    def test_it_calls_the_get_stacks_method_with_the_custom_max_stack_depth(
            self):
        AgentConfiguration.set(AgentConfiguration._get_new_config(configure_agent_response={
            "agentParameters": {
                "SamplingIntervalInMilliseconds": "2700",
                "MinimumTimeForReportingInMilliseconds": "60000",
                "MaxStackDepth": "10"
            },
            "periodInSeconds": 123
        }))
        sampler = Sampler(environment=self.environment)

        sampler.sample()

        self.mock_get_stacks.assert_called_once_with(
            threads_to_sample=ANY,
            excluded_threads=ANY,
            max_depth=10,
        )
 def before(self):
     super().before()
     self.environment["max_threads"] = 1
     self.subject = Sampler(environment=self.environment)