You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 rivejä
1.9 KiB

  1. #!/usr/bin/python
  2. import json
  3. import csv
  4. import sys
  5. class FancyFloat(float):
  6. def __repr__(self):
  7. return format(Decimal(self), "f")
  8. class JsonRpcEncoder(json.JSONEncoder):
  9. def decimalize(self, val):
  10. if isinstance(val, dict):
  11. return {k:self.decimalize(v) for k,v in val.items()}
  12. if isinstance(val, (list, tuple)):
  13. return type(val)(self.decimalize(v) for v in val)
  14. if isinstance(val, float):
  15. return FancyFloat(val)
  16. return val
  17. def encode(self, val):
  18. return super().encode(self.decimalize(val))
  19. if len(sys.argv) == 1:
  20. print("No file was provided")
  21. else:
  22. trace_events = []
  23. call_stack = []
  24. with open(sys.argv[1], "r") as in_file:
  25. csv_reader = csv.reader(in_file, delimiter=',')
  26. pf = 1
  27. first_line = True
  28. last_ts = -1;
  29. for line in csv_reader:
  30. if first_line:
  31. pf = float(line[0]) / 1000
  32. first_line = False
  33. continue
  34. if line[0] == "->":
  35. call_stack.append(line)
  36. elif line[0] == "<-":
  37. call = call_stack.pop()
  38. ts = float(call[1])
  39. dur = (float(line[1])-ts)
  40. dict = {
  41. "pid": 1,
  42. "tid": 1,
  43. "ts" : ts,
  44. "dur": dur,
  45. "ph" : "X",
  46. "name": call[2],
  47. "args": {
  48. "file": f"({call[3]}:{call[4]})",
  49. }
  50. }
  51. if call[5]:
  52. dict["args"]["info1"] = call[5]
  53. if call[6]:
  54. dict["args"]["info2"] = call[6]
  55. trace_events.append(dict)
  56. else:
  57. print("invalid syntax")
  58. break
  59. with open("out.json", "w") as out_file:
  60. out_file.write(json.dumps({"traceEvents": trace_events}, indent=4))