sinatra 和 grape都是ruby 的api框架。
性能上: sinatra大约是 grape的2倍。高判立下。
sinatra代码
require 'sinatra'
get '/hi' do
"Put this in your pipe & smoke it!, name: #{params[:name]}"
end
$ ab -n 1000 -c 100 http://localhost:4567/hi?name=jim
Requests per second: 1629.18 [#/sec] (mean)
grape代码
require 'sinatra'
require 'grape'
class API < Grape::API
format :json
params do
requires :name, type: String
optional :number, type: String, regexp: /abc.*/
optional :page, type: Integer
end
get :hello do
{
hello: "hello #{params[:name]}, #{params[:number]}"
}
end
get :hi do
{
hi: 'good day!'
}
end
end
run API
$ ab -n 1000 -c 100 http://localhost:9292/hello.json?name=siwei
Requests per second: 647.96 [#/sec] (mean)
$ ab -n 1000 -c 100 http://localhost:9292/hi.json
Requests per second: 813.11 [#/sec] (mean)