Last active
October 30, 2020 21:04
-
-
Save dmkc/c1a0f9f0b126af2b64e9219d53f7944e to your computer and use it in GitHub Desktop.
track how quickly file size grows until Ctrl+C. follows symlinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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