def test_handler_wrong_signature(self) -> None:
     handler = WrongHandler()
     mock_event = {
         '_name': 'myeventname',
         '_timestamp': 0,
         'cpu_id': 0,
     }
     processor = Processor(handler)
     with self.assertRaises(TypeError):
         processor.process([mock_event])
 def test_handler_method_with_merge(self) -> None:
     handler1 = StubHandler1()
     handler2 = StubHandler2()
     mock_event = {
         '_name': 'myeventname',
         '_timestamp': 0,
         'cpu_id': 0,
     }
     processor = Processor(handler1, handler2)
     processor.process([mock_event])
     self.assertTrue(handler1.handler_called, 'event handler not called')
     self.assertTrue(handler2.handler_called, 'event handler not called')
 def test_processor_quiet(self) -> None:
     handler1 = StubHandler1()
     mock_event = {
         '_name': 'myeventname',
         '_timestamp': 0,
         'cpu_id': 0,
     }
     temp_stdout = StringIO()
     with contextlib.redirect_stdout(temp_stdout):
         processor = Processor(handler1, quiet=True)
         processor.process([mock_event])
     # Shouldn't be any output
     output = temp_stdout.getvalue()
     self.assertEqual(0, len(output),
                      f'Processor was not quiet: "{output}"')
    def test_check_required_events(self) -> None:
        mock_event = {
            '_name': 'myeventname',
            '_timestamp': 0,
            'cpu_id': 0,
        }
        # Fails check
        with self.assertRaises(Processor.RequiredEventNotFoundError):
            Processor(EventHandlerWithRequiredEvent()).process([mock_event])

        required_mock_event = {
            '_name': 'myrequiredevent',
            '_timestamp': 69,
            'cpu_id': 0,
        }
        # Passes check
        Processor(EventHandlerWithRequiredEvent()).process(
            [required_mock_event, mock_event])
def main():
    input_path = get_input_path()

    events = load_file(input_path)
    ust_memory_handler = UserspaceMemoryUsageHandler()
    kernel_memory_handler = KernelMemoryUsageHandler()
    ros2_handler = Ros2Handler()
    Processor(ust_memory_handler, kernel_memory_handler, ros2_handler).process(events)

    memory_data_util = MemoryUsageDataModelUtil(
        userspace=ust_memory_handler.data,
        kernel=kernel_memory_handler.data,
    )
    ros2_data_util = Ros2DataModelUtil(ros2_handler.data)

    summary_df = memory_data_util.get_max_memory_usage_per_tid()
    tids = ros2_data_util.get_tids()
    filtered_df = summary_df.loc[summary_df['tid'].isin(tids)]
    print('\n' + filtered_df.to_string(index=False))
def process(
    input_path: str,
    force_conversion: bool = False,
    hide_results: bool = False,
    convert_only: bool = False,
) -> int:
    """
    Process ROS 2 trace data and output model data.

    The trace data will be automatically converted into
    an internal intermediate representation if needed.

    :param input_path: the path to a converted file or trace directory
    :param force_conversion: whether to re-creating converted file even if it is found
    :param hide_results: whether to hide results and not print them
    :param convert_only: whether to only convert the file into our internal intermediate
        representation, without processing it. This should usually not be necessary since
        conversion is done automatically only when needed or when explicitly requested with
        force_conversion; conversion is mostly just an implementation detail
    """
    input_path = os.path.expanduser(input_path)
    if not os.path.exists(input_path):
        print(f'input path does not exist: {input_path}', file=sys.stderr)
        return 1

    start_time = time.time()

    events = load_file(input_path,
                       do_convert_if_needed=True,
                       force_conversion=force_conversion)

    # Return now if we only need to convert the file
    if convert_only:
        return 0

    processor = Processor(Ros2Handler())
    processor.process(events)

    time_diff = time.time() - start_time
    if not hide_results:
        processor.print_data()
    print(f'processed {len(events)} events in {time_diff_to_str(time_diff)}')
    return 0
 def test_get_handler_by_type(self) -> None:
     handler1 = StubHandler1()
     handler2 = StubHandler2()
     processor = Processor(handler1, handler2)
     result = processor.get_handler_by_type(StubHandler1)
     self.assertTrue(result is handler1)
 def setUpClass(cls):
     cls.transform_fake_fields(input_events)
     cls.expected = cls.build_expected_df(expected)
     cls.handler = ProfileHandler(address_to_func=address_to_func)
     cls.processor = Processor(cls.handler)
     cls.processor.process(input_events)