SHARE
TWEET

Untitled

a guest May 19th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'set'
  2. require 'json'
  3.  
  4. if ARGV.length != 2
  5.   puts "Usage: detect_leaks [FIRST.json] [SECOND.json]"
  6.   exit 1
  7. end
  8.  
  9. first_addrs = Set.new
  10. second_addrs = Set.new
  11.  
  12. # Get a list of memory addresses from the first dump
  13. File.open(ARGV[0], "r").each_line do |line|
  14.   parsed = JSON.parse(line)
  15.   first_addrs << parsed["address"] if parsed && parsed["address"]
  16. end
  17.  
  18. # Get a list of memory addresses from the last dump
  19. File.open(ARGV[1], "r").each_line do |line|
  20.   parsed = JSON.parse(line)
  21.   second_addrs << parsed["address"] if parsed && parsed["address"]
  22. end
  23.  
  24. diff = []
  25.  
  26. # Get a list of all items present in both the second and
  27. # third dumps but not in the first.
  28. File.open(ARGV[1], "r").each_line do |line|
  29.   parsed = JSON.parse(line)
  30.   if parsed && parsed["address"]
  31.     diff << parsed unless first_addrs.include?(parsed["address"])
  32.   end
  33. end
  34.  
  35. # Group items
  36. diff.group_by do |x|
  37.   [x["type"], x["file"], x["line"]]
  38. end.map do |x,y|
  39.   # Collect memory size
  40.   [x, y.count, y.inject(0){|sum,i| sum + (i['bytesize'] || 0) }, y.inject(0){|sum,i| sum + (i['memsize'] || 0) }]
  41. end.sort do |a,b|
  42.   b[1] <=> a[1]
  43. end.each do |x,y,bytesize,memsize|
  44.   # Output information about each potential leak
  45.   puts "Leaked #{y} #{x[0]} objects of size #{bytesize}/#{memsize} at: #{x[1]}:#{x[2]}"
  46. end
  47.  
  48. # Also output total memory usage, because why not?
  49. memsize = diff.inject(0){|sum,i| sum + (i['memsize'] || 0) }
  50. bytesize = diff.inject(0){|sum,i| sum + (i['bytesize'] || 0) }
  51. puts "\n\nTotal Diff: #{diff.count}"
  52. puts "\n\nTotal Size: #{bytesize}/#{memsize}"
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top