Skip to content

Instantly share code, notes, and snippets.

@exAspArk
Last active March 6, 2017 16:58
Show Gist options
  • Save exAspArk/7213b8b12a2340f0b74811d72f9be8ac to your computer and use it in GitHub Desktop.
Save exAspArk/7213b8b12a2340f0b74811d72f9be8ac to your computer and use it in GitHub Desktop.

Concurrent Server

Requirements

  • puma
  • sinatra
  • typhoeus

Installation

bundle install

Running server

PORT=3003 REQUESTS_FROM_SERVER=5 make run_server

Running experiment

time PORT=3003 REQUESTS_TO_SERVER=4 make run_experiment

Results

Workers Threads Concurrent requests from server Concurrent requests to server Time
2 2 1 1 1.6
2 2 1 4 1.6
2 2 1 8 4.5
2 2 1 16 6.4
2 2 1 32 13.5
2 2 5 1 1.6
2 2 5 4 1.6
2 2 5 8 4.5
2 2 5 16 6.9
2 2 5 32 14.1

chart

Conclusion

  • Number of concurrent requests from server doesn't affect performance.
  • Time of concurrent requests to server which fit thread pool size is constant.
  • Time of concurrent requests to server which doesn't fit thread pool size grows linearly.
require 'sinatra'
require 'typhoeus'
require 'typhoeus/adapters/faraday'
require 'json'
class App < Sinatra::Base
REQUESTS_FROM_SERVER = ENV['REQUESTS_FROM_SERVER'].to_i
get '/' do
responses = make_concurrent_requests
sleep 1
responses.map(&:status).to_json
end
private
def make_concurrent_requests
responses = []
conn = Faraday.new(url: "https://google.com") { |faraday| faraday.adapter(:typhoeus) }
conn.in_parallel do
REQUESTS_FROM_SERVER.times { responses << conn.get('/') }
end
responses
end
end
require_relative './app'
configure do
set :server, :puma
end
puts "Concurrent request count from server: #{App::REQUESTS_FROM_SERVER}"
run App
source 'https://rubygems.org'
gem 'puma'
gem 'sinatra'
gem 'faraday'
gem 'typhoeus'
group :development do
gem 'pry'
end
run_server:
bundle exec puma -p $(PORT) -t 0:2 -w 2 --preload config.ru
run_experiment:
for n in {1..$(REQUESTS_TO_SERVER)}; do echo 'fetching..'; curl -s -o /dev/null http://0.0.0.0:$(PORT) & done; wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment