This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
diskhog.rb
57 lines (50 loc) · 1.57 KB
/
diskhog.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'rubygems'
require 'sinatra'
require 'tempfile'
DEFAULT_FILE_SIZE_MB = 25
KB = 1024
MB = KB * 1024
get '/' do
host = ENV['VMC_APP_HOST']
port = ENV['VMC_APP_PORT']
msg = "<h1>Hello from DiskHog! via: #{host}:#{port} </h1>"
msg += "<h2>Visit /largefile to trigger bad behavior</h2>"
end
def random_str(size)
File.open('/dev/urandom') { |x| x.read(size).unpack('H*')[0] }
end
# GET /largefile
# create a file of size DEFAULT_FILE_SIZE_MB
#
# To create a file size other than default, pass 'size' as a param.
# E.g., to create a 10MB file:
# curl '<myapp>/largefile?size=10'
get '/largefile' do
size_in_mb = params[:size].to_i
size_in_mb = DEFAULT_FILE_SIZE_MB unless size_in_mb > 0
kbytes = size_in_mb * KB
# if sinatra < 1.3
# Note: large files may fail due to gateway timeout
# use Tempfile to generate a temporary file name. We can't use
# the Tempfile itself since it will will be deleted when the
# process ends
tmp = Tempfile.new("garbage", ".")
fname = File.basename(tmp.path)
tmp.unlink
File.open(fname, 'w') do |f|
# write out chunks of KB bytes
(1..kbytes).each { f.write(random_str(KB)) }
end
# if sinatra >= 1.3
# we need to stream the response lest CF think something's amiss
# and returns the dreaded "504 timeout"
#stream do |out|
# File.open('garbage', 'w') do |f|
# write out chunks of KB bytes
# (1..kbytes).each { f.write(random_str(KB)) }
# out << '.'
# end
# out << "\nwrote #{kbytes*KB} bytes to #{fname} (#{size_in_mb}MB)\n"
#end
"wrote #{kbytes*KB} bytes to 'garbage' (#{size_in_mb}MB)\n"
end