raw.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import library.python.import_tracing.lib.converters.base as base_converter
  2. import library.python.import_tracing.lib.constants as constants
  3. class RawTextTraceConverter(base_converter.BaseTraceConverter):
  4. @staticmethod
  5. def _get_columns_length(events):
  6. max_filename = 0
  7. max_cumtime = 0
  8. max_end_time = 0
  9. for event in events:
  10. max_filename = max(max_filename, len(event.filename))
  11. max_cumtime = max(max_cumtime, event.end_time - event.start_time)
  12. max_end_time = max(max_end_time, event.end_time)
  13. return len(str(max_cumtime)), max_filename, max_end_time
  14. @staticmethod
  15. def _get_sorted_events(events):
  16. return sorted(events, key=lambda event: event.end_time - event.start_time, reverse=True)
  17. @staticmethod
  18. def _format_line(cumtime, filename, max_cumtime, max_filename):
  19. return "{0:<{max_cumtime}}\t{1:<{max_filename}}\n".format(
  20. cumtime,
  21. filename,
  22. max_cumtime=max_cumtime,
  23. max_filename=max_filename,
  24. )
  25. def dump(self, events, filepath):
  26. max_cumtime, max_filename, max_end_time = self._get_columns_length(events)
  27. max_line_length = max_cumtime + max_filename
  28. with open(filepath, "w") as file:
  29. # total time taken
  30. file.write("total time taken (seconds): {0:.4f}\n".format(max_end_time / constants.MCS_IN_SEC))
  31. file.write("-" * max_line_length + "\n")
  32. # header
  33. file.write(self._format_line("cumtime", "filename", max_cumtime, max_filename))
  34. file.write("-" * max_line_length + "\n")
  35. # trace info
  36. for event in self._get_sorted_events(events):
  37. time_taken = format(((event.end_time - event.start_time) / constants.MCS_IN_SEC), ".6f")
  38. file.write(
  39. self._format_line(
  40. time_taken,
  41. event.filename,
  42. max_cumtime,
  43. max_filename,
  44. )
  45. )