Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

64 linhas
1.9 KiB

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