Skip to content

Instantly share code, notes, and snippets.

@glurp
Last active April 21, 2018 19:52
Show Gist options
  • Save glurp/face5c9dc2729b1dcc2efbe0ead940ce to your computer and use it in GitHub Desktop.
Save glurp/face5c9dc2729b1dcc2efbe0ead940ce to your computer and use it in GitHub Desktop.
router test : little http server for get local data (dir,file,proc) with Crystal lang and 'router' http server
require "router"
require "io/memory"
############################################################################
# Tools
############################################################################
def to_table(ll)
ll.map {|l|
"<tr><td>#{l.join("</td><td>")}</td></tr>"
}.join("\n")
end
def execute_cmd_to_table(cmd,config={} of Symbol => Int32)
mincol = config[:mincol]? || 100
io = IO::Memory.new
Process.run(cmd, shell: true, output: io)
output = io.to_s
to_table( output.split(/\r?\n/).map {|line|
l= yield(line)
l[0...mincol]+(l.size>mincol ? [l[mincol..-1].join(" ")] : [] of String )
})
end
def head
"<style>
body {margin: 0px;}
div {border: 2px solid black;margin-left: 20px;}
h1,h2,h3 {color: #BBBBDD; background-color: #004455; text-align: center; padding: 10px;}
table {border-collapse: collapse;border: 2px solid black;margin-left: 20px;}
th,td {border: 1px solid black;padding: 3px 5px 2px 3px}
table td, table td * { vertical-align: top;}
</style>"
end
class WebServer
include Router
def dec(href) href.gsub("%2F","/") end
def enc(href) href.gsub("/","%2F") end
def flat(txt) txt.gsub("&","&amp;").gsub("<","&lt;").gsub(">","&gt;") end
def draw_routes
get "/" do |context, params|
content="<html><body><h3>Hello CRRouter!</h3>
<a href='/time'>Time</a>
<a href='/ls/.'>Directory</a>
<a href=/proc>Process</a>
<a href=/partitions>Partitions</a>
<a href=/ipv4>Net Ipv4</a>
<a href=/interfaces>Interfaces</a>
<a href=/memory>RAM</a>
</body></html>"
context.response.print content
context
end
get "/time" do |context, params|
context.response.print Time.now.to_s
context
end
get "/ls/*dir" do |context, q|
dir=(q["dir"]? !=nil ? q["dir"] : ".").gsub("%2F","/")
content=to_table( Dir.glob("#{dir}/*").map {|line|
stat=File.stat(line)
[
line,
File.file?(line) ? stat.size : "<a href=/ls/#{enc(line)}>Go</a>",
File.file?(line) ? "<a href='/file/#{enc(line)}'>See</a>" : "",
stat.mtime()
]
})
title="Directory of #{dir}"
content="<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<code><pre><table><tr><th>Name</th><th>Size</th><th>url</th><th>Time</th></tr>#{content}</table></pre></code>
<br><h3>#{Time.now}</h3>
</body></html>"
context.response.print content
context
end
get "/file/*file" do |context, q|
file=dec( q["file"]? == nil ? "" : q["file"] ).gsub("././","./")
title="File #{file}"
content="<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<div><code><pre>#{flat(File.read(file))}</pre></code></div>
<br><h3>#{Time.now}</h3>
</body></html>"
context.response.print content
context
end
get "/proc" do |context, q|
table=execute_cmd_to_table("ps -ef",{mincol: 7 }) {|line| line.split(/\s+/)}
title="Process running..."
content="<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<code><pre><table>#{table}</table></pre></code>
<br><h3>#{Time.now}</h3>
</body></html>"
context.response.print content
context
end
get "/partitions" do |context, q|
table=execute_cmd_to_table("df -h") {|line| line.split(/\s+/)}
title="Partitions"
content="<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<code><pre><table>#{table}</table></pre></code>
<br><h3>#{Time.now}</h3>
</body></html>"
context.response.print content
context
end
get "/ipv4" do |context, q|
table1=execute_cmd_to_table("echo IpV4-Key:Value; grep '' /proc/sys/net/ipv4/*",) {|line|
line.split(":",2)
}
table2=execute_cmd_to_table("echo Net-Core-Key:Value; grep '' /proc/sys/net/core/*",) {|line|
a=line.split(":",2)
a[1]=a.last.scan(/.{40,40}/).map {|md| md[0]}.join("<br/>") if a.last.size> 50
p a
a
}
title="Kernel tuning IPV4"
content="<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<table><tr>
<td><code><pre><table>#{table1}</table></pre></code></td>
<td><code><pre><table>#{table2}</table></pre></code></td>
</tr></table>
<br><h3>#{Time.now}</h3>
</body></html>"
context.response.print content
context
end
get "/interfaces" do |context, q|
table=execute_cmd_to_table("netstat -i") {|line| line.split(/\s+/)}
title="IP Interfaces"
content="<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<code><pre><table>#{table}</table></pre></code>
<br><h3>#{Time.now}</h3>
</body></html>"
context.response.print content
context
end
get "/memory" do |context, q|
table=execute_cmd_to_table("free -h") {|line|
a=line.split(":",2)
l=a.last.split(/\s+/)
[a.first] + l
}
title="IP Interfaces"
content="<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<code><pre><table>#{table}</table></pre></code>
<br><h3>#{Time.now}</h3>
</body></html>"
context.response.print content
context
end
get "/bidon" do |context, params|
content=""
context.response.print content
context
end
end
def run
puts "Start on :3000 ?..."
server = HTTP::Server.new(3000, route_handler)
puts "Ready on :3000"
server.listen
end
end
web_server = WebServer.new
web_server.draw_routes
web_server.run
@glurp
Copy link
Author

glurp commented Apr 21, 2018

Performance with my (intel core i5 2.5Hz) Laptop, on Windows 7 => Virtualbox => Ubuntu 17 : 11000 requests/secs ! (with ab near 100% cpu) : 11000 Request/seconds ! :

image

$ ab -r -c 4 -n 100000 http://localhost:3000/time
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>

. . . . .

Server Software:
Server Hostname: localhost
Server Port: 3000

Document Path: /time
Document Length: 26 bytes

Concurrency Level: 4
Time taken for tests: 8.900 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 6500000 bytes
HTML transferred: 2600000 bytes
Requests per second: 11235.69 [#/sec] (mean)
Time per request: 0.356 [ms] (mean)
Time per request: 0.089 [ms] (mean, across all concurrent requests)
Transfer rate: 713.20 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 13
Processing: 0 0 0.3 0 15
Waiting: 0 0 0.3 0 15
Total: 0 0 0.4 0 15

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 1
98% 1
99% 1
100% 15 (longest request)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment