Skip to content

Instantly share code, notes, and snippets.

@dmkc
Last active October 30, 2020 21:04
Show Gist options
  • Save dmkc/c1a0f9f0b126af2b64e9219d53f7944e to your computer and use it in GitHub Desktop.
Save dmkc/c1a0f9f0b126af2b64e9219d53f7944e to your computer and use it in GitHub Desktop.
track how quickly file size grows until Ctrl+C. follows symlinks
start_time = Time.now
files = {}
SAMPLE_INTERVAL = 2
def record_filesize(fds, sizes)
loop do
fds.each do |file|
unless sizes[file]
sizes[file] = []
end
sizes[file] << file.stat.size
end
sleep(SAMPLE_INTERVAL)
end
end
def open_fds(filenames)
fds = []
filenames.map do |f|
next if File.directory?(f)
fds << File.open(f)
rescue
puts("File doesn't exist: #{f}, ignoring.")
end
fds
end
def calc_change(files, start_time)
total_bytes = 0
files.each do |fd, sizes|
difference = []
rotated = false
sizes.each_with_index do |s, i|
next if i==0
difference << s - sizes[i-1]
if difference.last < 0
rotated = true
break
end
total_bytes += difference.last
end
rate_of_change = difference.sum / difference.size
next if rate_of_change == 0
puts fd.path
puts "Avg rate of change: #{rate_of_change/SAMPLE_INTERVAL} bytes/s #{"(rotated}" if rotated}\n\n"
end
avg_increase = total_bytes / (Time.now-start_time)
puts "Total file size increase: #{total_bytes} bytes (in #{Time.now-start_time} seconds), or #{avg_increase} bytes/s"
end
Signal.trap("SIGINT") do
puts("\nWatching: #{files.size} files")
puts("Sampling interval: #{SAMPLE_INTERVAL} seconds\n")
calc_change(files, start_time)
exit
end
record_filesize(open_fds(ARGV), files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment