From bb8ac5f58949e85f9fbadcc3ba68134808de2da0 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 12 Dec 2022 00:02:46 +1300 Subject: [PATCH] A bunch of unsorted examples. --- examples/actioncable-examples | 1 + examples/benchmark/small.md | 53 ++++++++++++++++++++++++++ examples/benchmark/ssl/certificate.pem | 19 +++++++++ examples/benchmark/ssl/config.ru | 10 +++++ examples/benchmark/ssl/falcon.rb | 13 +++++++ examples/benchmark/ssl/private.key | 28 ++++++++++++++ examples/failures/config.ru | 20 ++++++++++ examples/rails | 1 + examples/scheduler/commands.sh | 3 ++ examples/scheduler/config.ru | 22 +++++++++++ examples/server/start-stop.rb | 36 +++++++++++++++++ examples/timeout/remote.rb | 31 +++++++++++++++ 12 files changed, 237 insertions(+) create mode 160000 examples/actioncable-examples create mode 100644 examples/benchmark/small.md create mode 100644 examples/benchmark/ssl/certificate.pem create mode 100644 examples/benchmark/ssl/config.ru create mode 100644 examples/benchmark/ssl/falcon.rb create mode 100644 examples/benchmark/ssl/private.key create mode 100644 examples/failures/config.ru create mode 160000 examples/rails create mode 100644 examples/scheduler/commands.sh create mode 100644 examples/scheduler/config.ru create mode 100644 examples/server/start-stop.rb create mode 100644 examples/timeout/remote.rb diff --git a/examples/actioncable-examples b/examples/actioncable-examples new file mode 160000 index 00000000..916910c9 --- /dev/null +++ b/examples/actioncable-examples @@ -0,0 +1 @@ +Subproject commit 916910c91137df49f56511e57d4d1889634bf2be diff --git a/examples/benchmark/small.md b/examples/benchmark/small.md new file mode 100644 index 00000000..521f521c --- /dev/null +++ b/examples/benchmark/small.md @@ -0,0 +1,53 @@ +## Puma + +``` +Thread Stats Avg Stdev Max +/- Stdev + Latency 2.05ms 312.41us 5.88ms 80.73% + Req/Sec 0.98k 41.36 1.05k 57.50% +3892 requests in 2.00s, 4.87MB read +Requests/sec: 1945.51 +Transfer/sec: 2.43MB + +% time seconds usecs/call calls errors syscall +------ ----------- ----------- --------- --------- ---------------- + 88.23 0.690607 4 163975 write + 5.42 0.042420 5 7792 setsockopt + 2.20 0.017235 4 3898 recvfrom + 1.73 0.013570 3 3898 getsockopt + 1.53 0.011956 3 3896 getpeername + 0.88 0.006876 3 2056 read + 0.00 0.000026 13 2 listen + 0.00 0.000024 2 9 getsockname + 0.00 0.000007 7 1 socket + 0.00 0.000007 0 8 2 accept4 + 0.00 0.000005 5 1 bind +------ ----------- ----------- --------- --------- ---------------- +100.00 0.782733 4 185536 2 total +``` + +## Falcon + +``` +Thread Stats Avg Stdev Max +/- Stdev + Latency 10.92ms 14.15ms 46.63ms 78.30% + Req/Sec 1.02k 222.88 1.45k 65.00% +4059 requests in 2.00s, 4.80MB read +Requests/sec: 2026.72 +Transfer/sec: 2.40MB + +% time seconds usecs/call calls errors syscall +------ ----------- ----------- --------- --------- ---------------- + 69.12 0.042774 5 8128 sendto + 20.04 0.012400 2 4242 174 recvfrom + 9.91 0.006133 2 2071 8 read + 0.47 0.000290 13 21 getsockname + 0.20 0.000126 6 20 getsockopt + 0.19 0.000115 5 20 14 accept4 + 0.06 0.000035 2 14 write + 0.02 0.000014 2 7 setsockopt + 0.00 0.000000 0 1 socket + 0.00 0.000000 0 1 bind + 0.00 0.000000 0 1 listen +------ ----------- ----------- --------- --------- ---------------- +100.00 0.061887 4 14526 196 total +``` diff --git a/examples/benchmark/ssl/certificate.pem b/examples/benchmark/ssl/certificate.pem new file mode 100644 index 00000000..0494ecaf --- /dev/null +++ b/examples/benchmark/ssl/certificate.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDCTCCAfGgAwIBAgIUQ9QvgIwQxiiFY/Apv98LwTizY10wDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIxMDEwNjAxNDgyM1oXDTIyMDEw +NjAxNDgyM1owFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF +AAOCAQ8AMIIBCgKCAQEA4cdRCU0LHu761SxVhzU/H3uc/aXSz38wuW9UpR72M03H +ONLvRaN3DbRmiZ3wtzq1z7LQq7HMwxitcYS4uvUyeZUypKsU0weMnU9OKhvcIO+f +S2AXFqGFdZ2NmMZkQg+FE2edXNekWl2M+XomORMlguwAqXLQTm+Pm6p7qptcg7+j +d1ltzH92EqfWt5cTX87o719XZpla2tZkG3emdzBSnVApYA/Lmd/WsIKhfUYFNU1e +/D9BKJSN/wtlJKgpYLOXhz8bddTc7zEp6SZnLQfi71D3h1pi2IlOodctkZWcShuz +bV4ocL/hbSHX6+BheZtgJQvXZc5ZlzK9S4d5xuuHlQIDAQABo1MwUTAdBgNVHQ4E +FgQUWmV7m6E8mI8wM65fl2WFvUoP9zYwHwYDVR0jBBgwFoAUWmV7m6E8mI8wM65f +l2WFvUoP9zYwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAvVJx ++Z4QUBRrMHqkmJMadqOnevRn9mLn27NXEEvR1nuRTr2aNxiwko04RbfgeE23XPBx +v80yBalCkQOENKyQ52NuRw3kOohT03XcvjEMs06wh1FtV8XvFPxkAdnfJoKqN43T +BjhOs1KlsWcUjMJ4msU6i8BDqHZiNJHMNc0wTtNoMx0w8WzDIOm0l47VvVsDrE8b +Jj4jaY94oFh0ySHAeeHduD7Y0vN2jR73hF8q4XH9u7CH2/hbMyS/KgTXEptSu7yF +Ix1k2MMShfppzBwHcPoKD+RMCK6yXaisdE29zTIHOxPLGjcd2stEZ9bXwBqxJZeN +ULRknPqToIlgrmdjjw== +-----END CERTIFICATE----- diff --git a/examples/benchmark/ssl/config.ru b/examples/benchmark/ssl/config.ru new file mode 100644 index 00000000..81132a83 --- /dev/null +++ b/examples/benchmark/ssl/config.ru @@ -0,0 +1,10 @@ + +require "securerandom" + +response = SecureRandom.hex(ENV.fetch('RESPONSE_SIZE', 1024*8).to_i) + +puts "response.bytesize = #{response.bytesize}" + +run lambda { |env| + [200, {"Content-Type" => "text/plain"}, [response.dup]] +} diff --git a/examples/benchmark/ssl/falcon.rb b/examples/benchmark/ssl/falcon.rb new file mode 100644 index 00000000..c7562e65 --- /dev/null +++ b/examples/benchmark/ssl/falcon.rb @@ -0,0 +1,13 @@ +# falcon.rb + +load :rack, :tls + +rack 'localhost', :tls do + endpoint do + Async::HTTP::Endpoint.for(scheme, 'localhost', port: '3000', ssl_context: ssl_context) + end + + ssl_certificate_path { 'certificate.pem' } + ssl_private_key_path { 'private.key' } +end + diff --git a/examples/benchmark/ssl/private.key b/examples/benchmark/ssl/private.key new file mode 100644 index 00000000..2e8f8988 --- /dev/null +++ b/examples/benchmark/ssl/private.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhx1EJTQse7vrV +LFWHNT8fe5z9pdLPfzC5b1SlHvYzTcc40u9Fo3cNtGaJnfC3OrXPstCrsczDGK1x +hLi69TJ5lTKkqxTTB4ydT04qG9wg759LYBcWoYV1nY2YxmRCD4UTZ51c16RaXYz5 +eiY5EyWC7ACpctBOb4+bqnuqm1yDv6N3WW3Mf3YSp9a3lxNfzujvX1dmmVra1mQb +d6Z3MFKdUClgD8uZ39awgqF9RgU1TV78P0EolI3/C2UkqClgs5eHPxt11NzvMSnp +JmctB+LvUPeHWmLYiU6h1y2RlZxKG7NtXihwv+FtIdfr4GF5m2AlC9dlzlmXMr1L +h3nG64eVAgMBAAECggEAbrFs3pWCRUk/ezJAQIjviLzvLECXiZm/Rrr5kfcLF/4j +FY39N3Q8U2DGAOSJdxv2tXzi57rFPEWmC4jZlPfQJmHqsWcbT5dc7mdKWETOeOti +eu8Cgtrv9skm+ecSknzPbsMXGMS01IUXL90eTk8/zd9tN6aN0FbL3rMH7I49Hm3z +tiImptzBDMY15YxyvqUytA4f9lFnHM/1OFACCehyBq2UMZOka5mykg/CKZiJ/HXe +4++wg23wz7vdDa+B1TDz8YaOReeMBXY8fPDdLt/utpireoxE4LpKexxWa7odyWYP +xwVfsGuEnjbBkYF+teWBz2/ayzsvfvmGgnFKahSOwQKBgQDwxyZQZyWotYqStZAC +QKSUaYTuY9gJzaMBG36wKTwhkBBDQMwhtOnt8M4Tp7oc2uJea78tq00e/UhsIZwi +H517OEhdYIO+8dCeDNvPzsGKj052SqTsRw/M/tqsVGfjNRl+IJIWGyKNduVSKr0X +6lvq9784qEaItKpbSCA4zi4vkQKBgQDwDWlDS/CHpRj5TsI7/Erl5tVh9MLyTT3z +gpiP3E2iTJXiSeAMJ3irI5TwC5d2ss8sXuQIBfrTR2xMF2Yw6whJPZ1n5Xtsrvvn +GgEA6AOU8mQ3Wpaqc67o/IJib8JdyaV3aaVWJX91PFzCn4fp5mjJcMD7XEntcE/q +BlZApmWdxQKBgD99M+Qlnq+C1zxiNcvyQbOEs3Gs0C2dlKrZFinbZaiv9SnWrz0A +GGMOiThzlMbBDMm6/ZlMPzS5stwYbLQuw/bW23YL2hGCb/ENEPUV3sDrktPsUNqg +zlDOrticFRy7RrKUYhEELLNCs6tb66qfuIcXtst4y8iGYxlYxm5UJ6eRAoGANXlX +VIVrHFxbttWjkQc/i/0i1YJXlvA7963E3G8f4CKdVN5OCYcVa1Iu0FZUtD1axzCQ +LXVfjYcgicbAne8NVntiQA2vs+M6/B0f0hrdCWYBiw8j9u50LTRquxDYRFoyAOrI +yOrrfKsoorY06ZbJCu6SHqxNT5YwRxqIRjiK9F0CgYEA0TwQOy4nbUg+BpYplHNn +NQEP03U/7fs/f+0UDaFMFcTPGcYVWjva0qOWUif9w3L19NIdDV0OeahWafs8P/wU +3V0h9u6QgEjcPaseb6EhNpjChLpLISguCrxdg2wkHXcbAi9cxFQ4wm6NyJuam6aP +61JF3oA3ttmKQKJ5GM4JGbY= +-----END PRIVATE KEY----- diff --git a/examples/failures/config.ru b/examples/failures/config.ru new file mode 100644 index 00000000..7c7fb1b5 --- /dev/null +++ b/examples/failures/config.ru @@ -0,0 +1,20 @@ +require 'typhoeus' + +run lambda{|env| + ttl = env['PATH_INFO'].split('/').last.to_i + localhost = "127.0.0.#{rand(1...255)}" + + pp ttl: ttl, thread: Thread.current.name, localhost: localhost + + if ttl < 2 + hydra = Typhoeus::Hydra.new + 2.times do + hydra.queue(Typhoeus::Request.new("#{localhost}:9292/#{ttl+1}")) + end + response = Typhoeus.get("http://#{localhost}:9292/#{ttl+1}") + + hydra.run + end + + [200, [], [ttl.to_s]] +} \ No newline at end of file diff --git a/examples/rails b/examples/rails new file mode 160000 index 00000000..d89d14d2 --- /dev/null +++ b/examples/rails @@ -0,0 +1 @@ +Subproject commit d89d14d241738be3839f095402261ef39509db1b diff --git a/examples/scheduler/commands.sh b/examples/scheduler/commands.sh new file mode 100644 index 00000000..91671ca1 --- /dev/null +++ b/examples/scheduler/commands.sh @@ -0,0 +1,3 @@ + +curl https://www.codeotaku.com/code/hello + diff --git a/examples/scheduler/config.ru b/examples/scheduler/config.ru new file mode 100644 index 00000000..74b71a5e --- /dev/null +++ b/examples/scheduler/config.ru @@ -0,0 +1,22 @@ +#!/usr/bin/env falcon --verbose serve -c +# frozen_string_literal: true + +require 'async' +require 'uri' +require 'net/http' + +class App + def initialize + # Thread.current.scheduler = nil + end + + def call(env) + uri = URI("https://www.codeotaku.com/code/hello") + + body = Net::HTTP.get(uri) + + return [200, {'cache-control' => 'max-age=10, public'}, [body]] + end +end + +run App.new diff --git a/examples/server/start-stop.rb b/examples/server/start-stop.rb new file mode 100644 index 00000000..ea2986c4 --- /dev/null +++ b/examples/server/start-stop.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env ruby + +require 'async' +require 'async/http/endpoint' +require 'falcon' + +module App + def self.call(env) + Async::Task.current.sleep(10) + + return [200, [], ["Hello World"]] + end +end + +def start_server + Async do + endpoint = Async::HTTP::Endpoint.parse('http://127.0.0.1:3000') + + app = Falcon::Server.middleware(App) + + server = Falcon::Server.new(app, endpoint) + + server.run.each(&:wait) + end +end + +Async do |top| + server_task = start_server + + while true + top.print_hierarchy + top.sleep(10) + end + + server_task.wait +end diff --git a/examples/timeout/remote.rb b/examples/timeout/remote.rb new file mode 100644 index 00000000..56cd4d4c --- /dev/null +++ b/examples/timeout/remote.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby + +require 'async' +require 'async/http/endpoint' +require 'async/http/client' + +Async do |task| + # endpoint = Async::HTTP::Endpoint.parse("https://rubyapi-org.herokuapp.com/2.7/o/string") + endpoint = Async::HTTP::Endpoint.parse("https://rubyapi.org/2.7/o/string") + client = Async::HTTP::Client.new(endpoint) + + response = client.get(endpoint.path, { + 'if-none-match' => 'W/"578a945b0772ae259625a9e66f06cdff"' + }) + + Async.logger.info(response, name: "headers") do |buffer| + response.headers.each do |key, value| + buffer.puts "#{key.rjust(40)}: #{value}" + end + end + + body = response.read + + Async.logger.info(response) {"Status: #{response.status} Body: #{body&.bytesize.inspect} bytes"} + + Async.logger.info(response, name: "trailer") do |buffer| + response.headers.trailer.each do |key, value| + buffer.puts "#{key.rjust(40)}: #{value}" + end + end +end