DigitMagazine

Untitled

Sep 22nd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. class SQLCounter(object):
  2. @classmethod
  3. def clear_data(cls):
  4. cls.current_code = ‘’
  5. cls.check = False
  6. cls.data = defaultdict(list)
  7. cls.hit_count = defaultdict(int)
  8.  
  9. @classmethod
  10. def before(cls, activity, lineno, colno):
  11. cls.check = True
  12. cls.current_code = (“line no {:<4}: {}”.format(lineno, activity),
  13. lineno)
  14. cls.hit_count[cls.current_code] += 1
  15.  
  16. @classmethod
  17. def after(cls, activity, lineno, colno):
  18. cls.check = False
  19.  
  20. @classmethod
  21. def insert(cls, data):
  22. cls.data[cls.current_code].append(data)
  23.  
  24. @classmethod
  25. def current_func(cls, func):
  26. cls.func = func
  27.  
  28. @classmethod
  29. def show_data(cls):
  30. if not cls.data:
  31. return
  32. data = sorted(cls.data.items(),key=lambda a: a[0][1])
  33. table = []
  34. headers = [‘Location’, ‘Hit’, ‘Queries’, ‘Time (ms)’, ‘Avg Time (ms)’]
  35. total_tm = 0.0
  36. total_qs = 0
  37. for current_code, qdata in data:
  38. activity, _ = current_code
  39. qs = len(qdata)
  40. total_qs += qs
  41. tm = sum(k[‘time’] for k in qdata)
  42. total_tm += tm
  43. avg_tm = tm / qs
  44. hit = cls.hit_count[current_code]
  45. table.append([activity, hit, qs, tm, avg_tm])
  46. print “Total SQL queries: {}, Total time: {} ms”.format(
  47. total_qs, total_tm)
  48. tabular_table = tabulate(table, headers, tablefmt=”simple”)
  49. print tabular_table
Add Comment
Please, Sign In to add comment