Last active
March 14, 2024 19:56
-
-
Save lappi-lynx/954ed068ae0a09c1b31348a6ab45a66f to your computer and use it in GitHub Desktop.
4.2 Quartz aalto course
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
# config.ru | |
class SimpleApp | |
def self.call(env) | |
request = Rack::Request.new(env) | |
case request.path_info | |
when '/' | |
latency = Random.rand(0.05..5) | |
p "I'm lazy, my laziness is #{latency} seconds..." | |
sleep(latency) | |
[200, {"content-type" => "text/plain"}, ["Hello, World! Server latency is #{latency} s"]] | |
when '/exhaust' | |
exhaust_resources | |
when '/sleep' | |
sleep_forever | |
else | |
[404, {"content-type" => "text/plain"}, ["Not Found"]] | |
end | |
end | |
def self.sleep_forever | |
loop do | |
sleep(1000) # actually its around 16 minutes. | |
end | |
end | |
def self.exhaust_resources | |
large_array = Array.new(1_000_000) { Array.new(1000) { rand(1000) } } | |
[200, {"content-type" => "text/plain"}, ["Exhausted Resources"]] | |
end | |
end | |
run SimpleApp | |
# client.rb | |
require 'net/http' | |
require 'uri' | |
path = ARGV.length > 0 ? ARGV[0] : '' | |
uri = URI.parse("http://localhost:4567/#{path}") | |
response = Net::HTTP.get_response(uri) | |
puts response.body | |
# Usage: | |
# run server `rackup -p 4567` | |
# run client cases: | |
# `ruby client.rb` | |
# `ruby client.rb foo` | |
# `ruby client.rb sleep` | |
# `ruby client.rb exhaust` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment