Skip to content

Instantly share code, notes, and snippets.

@glurp
Last active April 19, 2018 01:24
Show Gist options
  • Save glurp/066e7b6984c258197541a41fdc8d3fe1 to your computer and use it in GitHub Desktop.
Save glurp/066e7b6984c258197541a41fdc8d3fe1 to your computer and use it in GitHub Desktop.
kemal test : little http server for get local data (dir,file,proc) with Crystal lang
require "../src/kemal.cr"
require "io/memory"
############################################################################
# Tools
############################################################################
def to_table(ll)
ll.map {|l|
"<tr><td>#{l.join("</td><td>")}</td></tr>"
}.join("\n")
end
def head
"<style>
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}
</style>"
end
############################################################################
# Routes
############################################################################
get "/" do
"Hello Kemal!
<a href=time>Time</a>
<a href=ls>Directory</a>
<a href=proc>Process</a>
"
end
get "/time" do
"It is exactly #{Time.now}"
end
get "/ls" do |env|
q=env.params.query
dir=q["dir"]? !=nil ? q.["dir"] : "."
content=to_table( Dir.glob("#{dir}/*").map {|line|
stat=File.stat(line)
[
line,
File.file?(line) ? stat.size : "<a href=/ls?dir=#{dir}/#{line}>Go</a>",
File.file?(line) ? "<a href=/file?file=#{line}>See</a>" : "",
stat.mtime()
]
})
title="Directory of #{Dir.current}"
"<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>"
end
get "/file" do |env|
q=env.params.query
file=( q["file"]? == nil ? "" : q["file"] ).gsub("././","./")
puts file
title="File #{file}"
"<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<div><code><pre>#{File.read(file)}</pre></code></div>
<br><h3>#{Time.now}</h3>
</body></html>"
end
get "/proc" do |env|
command = "ps -ef"
io = IO::Memory.new
Process.run(command, shell: true, output: io)
output = io.to_s
content=to_table( output.split(/\r?\n/).map {|line|
l=line.split(/\s+/) +[""]
l[0..6]+(l.size>7 ? [l[7..-1].join(" ")] : [] of String )
})
title="Process running..."
"<html><head>#{head}</head>
<body><h2>#{title}</h2><br/>
<code><pre><table>#{content}</table></pre></code>
<br><h3>#{Time.now}</h3>
</body></html>"
end
Kemal.run
@glurp
Copy link
Author

glurp commented Apr 19, 2018

Performance with my (intel 2 cores) Laptop, on Ubuntu / Virtualbox (under Windows 7) : 5000 requests/secs

$ ab -r -s 2 -c 1000 -n 100000 http://127.0.0.1:3000/
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
...
Document Path:          /
Document Length:        91 bytes

Concurrency Level:      1000
Time taken for tests:   18.214 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      17600000 bytes
HTML transferred:       9100000 bytes
__Requests per second:    5490.31 [#/sec] (mean)__
Time per request:       182.139 [ms] (mean)
Time per request:       0.182 [ms] (mean, across all concurrent requests)
Transfer rate:          943.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  142 399.7      0    7160
Processing:     8   32  57.6     23    1729
Waiting:        8   32  57.6     23    1729
Total:         15  174 425.4     24    7189

Percentage of the requests served within a certain time (ms)
  50%     24
  66%     26
  75%     29
  80%     32
  90%   1037
  95%   1054
  98%   1252
  99%   1441
 100%   7189 (longest request)

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