Sha256: cf387ad7eae18b98ed93e6aed5f873643b2aed915ed437eefa3be2bbcc22d7a0

Contents?: true

Size: 1.21 KB

Versions: 21

Compression:

Stored size: 1.21 KB

Contents

#!/usr/bin/env ruby

require 'benchmark'

require 'httpx'

require 'async'
require 'async/barrier'
require 'async/semaphore'
require 'async/http/internet'

URL = "https://www.codeotaku.com/index"
REPEATS = 10

Benchmark.bmbm do |x|
	x.report("async-http") do
		Async do
			internet = Async::HTTP::Internet.new
			
			i = 0
			while i < REPEATS
				response = internet.get(URL)
				response.read
				
				i += 1
			end
		ensure
			internet&.close
		end
	end
	
	x.report("async-http (pipelined)") do
		Async do |task|
			internet = Async::HTTP::Internet.new
			semaphore = Async::Semaphore.new(100, parent: task)
			barrier = Async::Barrier.new(parent: semaphore)
			
			# Warm up the connection pool...
			response = internet.get(URL)
			response.read
			
			i = 0
			while i < REPEATS
				barrier.async do
					response = internet.get(URL)
					
					response.read
				end
				
				i += 1
			end
			
			barrier.wait
		ensure
			internet&.close
		end
	end
	
	x.report("httpx") do
		i = 0
		while i < REPEATS
			response = HTTPX.get(URL)
			
			response.read
			
			i += 1
		end
	end
	
	x.report("httpx (pipelined)") do
		urls = [URL] * REPEATS
		responses = HTTPX.get(*urls)
		
		responses.each do |response|
			response.read
		end
	end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
async-http-0.50.5 examples/compare/benchmark.rb