Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bunch of unsorted examples. #198

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/actioncable-examples
Submodule actioncable-examples added at 916910
53 changes: 53 additions & 0 deletions examples/benchmark/small.md
Original file line number Diff line number Diff line change
@@ -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
```
19 changes: 19 additions & 0 deletions examples/benchmark/ssl/certificate.pem
Original file line number Diff line number Diff line change
@@ -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-----
10 changes: 10 additions & 0 deletions examples/benchmark/ssl/config.ru
Original file line number Diff line number Diff line change
@@ -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]]
}
13 changes: 13 additions & 0 deletions examples/benchmark/ssl/falcon.rb
Original file line number Diff line number Diff line change
@@ -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

28 changes: 28 additions & 0 deletions examples/benchmark/ssl/private.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhx1EJTQse7vrV
LFWHNT8fe5z9pdLPfzC5b1SlHvYzTcc40u9Fo3cNtGaJnfC3OrXPstCrsczDGK1x
hLi69TJ5lTKkqxTTB4ydT04qG9wg759LYBcWoYV1nY2YxmRCD4UTZ51c16RaXYz5
eiY5EyWC7ACpctBOb4+bqnuqm1yDv6N3WW3Mf3YSp9a3lxNfzujvX1dmmVra1mQb
d6Z3MFKdUClgD8uZ39awgqF9RgU1TV78P0EolI3/C2UkqClgs5eHPxt11NzvMSnp
JmctB+LvUPeHWmLYiU6h1y2RlZxKG7N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oorY06ZbJCu6SHqxNT5YwRxqIRjiK9F0CgYEA0TwQOy4nbUg+BpYplHNn
NQEP03U/7fs/f+0UDaFMFcTPGcYVWjva0qOWUif9w3L19NIdDV0OeahWafs8P/wU
3V0h9u6QgEjcPaseb6EhNpjChLpLISguCrxdg2wkHXcbAi9cxFQ4wm6NyJuam6aP
61JF3oA3ttmKQKJ5GM4JGbY=
-----END PRIVATE KEY-----
20 changes: 20 additions & 0 deletions examples/failures/config.ru
Original file line number Diff line number Diff line change
@@ -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]]
}
1 change: 1 addition & 0 deletions examples/rails
Submodule rails added at d89d14
3 changes: 3 additions & 0 deletions examples/scheduler/commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

curl https://www.codeotaku.com/code/hello

22 changes: 22 additions & 0 deletions examples/scheduler/config.ru
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions examples/server/start-stop.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions examples/timeout/remote.rb
Original file line number Diff line number Diff line change
@@ -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