This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove stage endpoint in cpu app clarify usage in diskhog app add ability to vary file size generated in diskhog app Change-Id: I0f27c1850f0f8d30c138958b8bff1836730fcedb
- Loading branch information
Showing
2 changed files
with
46 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,57 @@ | ||
require 'rubygems' | ||
require 'sinatra' | ||
require 'tempfile' | ||
|
||
BLOWUP_MB = 100 | ||
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 /evil to trigger bad behavior to create a file of size #{BLOWUP_MB} MB.</h2>" | ||
msg += "<h2>Visit /largefile to trigger bad behavior</h2>" | ||
end | ||
|
||
get '/evil' do | ||
a = Array.new(1024*1024, 'a') | ||
File.open('garbage', 'w') do |f| | ||
(1..BLOWUP_MB).each { f.puts "#{a}" } | ||
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 | ||
"BAD BAD!" | ||
|
||
# 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 |