Skip to content

Instantly share code, notes, and snippets.

@ota42y
Created April 22, 2019 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ota42y/3ed68a2cb0dc7c98122bdfd1a696ab72 to your computer and use it in GitHub Desktop.
Save ota42y/3ed68a2cb0dc7c98122bdfd1a696ab72 to your computer and use it in GitHub Desktop.
committee benchmark
require 'benchmark'
require 'net/http'
APPS = '/apps?page=1'
COUNT = 1000
Benchmark.bm(6) do |r|
r.report "NotUse" do
COUNT.times do
req = Net::HTTP.new('localhost', 5678)
req.get(APPS)
end
end
r.report "Use" do
COUNT.times do
req = Net::HTTP.new('localhost', 6789)
req.get(APPS)
end
end
end
require 'benchmark'
require 'net/http'
require 'json'
DEEP = '/deep'
COUNT = 1000
BASE = [
{
"a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d", "e"=>"e", "f"=>"f", "g"=>"g",
"h"=>"h", "i"=>"i", "j"=>"j", "k"=>"k", "l"=>"l", "m"=>"m", "n"=>"n",
"o"=>"o", "p"=>"p", "q"=>"q", "r"=>"r", "s"=>"s", "t"=>"t", "u"=>"u",
"v"=>"v", "w"=>"w", "x"=>"x", "y"=>"y", "z"=>"z"
}
]
BODY = {data: BASE * 100}.to_json
Benchmark.bm(6) do |r|
r.report "NotUse" do
COUNT.times do
http = Net::HTTP.new('localhost', 5678)
req = Net::HTTP::Post.new(DEEP)
req["Content-Type"] = "application/json"
req.body = BODY
http.request(req)
end
end
r.report "Use" do
COUNT.times do
http = Net::HTTP.new('localhost', 6789)
req = Net::HTTP::Post.new(DEEP)
req["Content-Type"] = "application/json"
req.body = BODY
http.request(req)
end
end
end
require "sinatra"
require "committee"
use Committee::Middleware::RequestValidation, schema_path: 'demo.yaml'
use Committee::Middleware::ResponseValidation, schema_path: 'demo.yaml'
#set :logging, nil
get "/apps" do
content_type :json
{ret: :apps}.to_json
end
post "/deep" do
content_type :json
{ret: :apps}.to_json
end
openapi: 3.0.0
info:
title: Sample API
version: 0.1.0
paths:
"/normal":
get:
description: get app names by array
parameters:
- name: page
in: query
required: true
description: specific page setting
schema:
type: integer
responses:
'200':
description: return array include strings
content:
'application/json':
schema:
description: app name stirngs
type: array
items:
type: string
"/deep":
post:
description: deep data test
requestBody:
content:
'application/json':
schema:
type: object
properties:
data:
type: array
items:
type: object
properties:
a:
type: string
b:
type: string
c:
type: string
d:
type: string
e:
type: string
f:
type: string
g:
type: string
h:
type: string
i:
type: string
j:
type: string
k:
type: string
l:
type: string
m:
type: string
n:
type: string
o:
type: string
p:
type: string
q:
type: string
r:
type: string
s:
type: string
t:
type: string
u:
type: string
v:
type: string
w:
type: string
x:
type: string
y:
type: string
z:
type: string
responses:
'200':
description: return array include strings
content:
'application/json':
schema:
type: object
properties:
ret:
type: string
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "committee"
gem "sinatra"
gem "pry"
gem "pry-byebug"
gem 'minitest'
gem 'rack-test'
gem 'rack-cors'
GEM
remote: https://rubygems.org/
specs:
byebug (11.0.0)
coderay (1.1.2)
committee (3.0.1)
json_schema (~> 0.14, >= 0.14.3)
openapi_parser (>= 0.2.2)
rack (>= 1.5)
json_schema (0.20.4)
method_source (0.9.2)
minitest (5.11.3)
mustermann (1.0.3)
openapi_parser (0.2.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.7.0)
byebug (~> 11.0)
pry (~> 0.10)
rack (2.0.7)
rack-cors (1.0.3)
rack-protection (2.0.5)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
sinatra (2.0.5)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.5)
tilt (~> 2.0)
tilt (2.0.9)
PLATFORMS
ruby
DEPENDENCIES
committee
minitest
pry
pry-byebug
rack-cors
rack-test
sinatra
BUNDLED WITH
2.0.1
require "sinatra"
require "committee"
#use Committee::Middleware::RequestValidation, schema_path: 'demo.yaml'
#use Committee::Middleware::ResponseValidation, schema_path: 'demo.yaml'
#set :logging, nil
get "/apps" do
content_type :json
{ret: :apps}.to_json
end
post "/deep" do
content_type :json
{ret: :apps}.to_json
end
@ota42y
Copy link
Author

ota42y commented Apr 22, 2019

ruby no_committee.rb -p 5678
ruby committee.rb -p 6789
1000 request time

small json
             user     system      total        real
NotUse   0.259781   0.180380   0.440161 (  1.079704)
Use      0.267121   0.195445   0.462566 (  1.205519)

committee need 0.2 milliseconds

big json
             user     system      total        real
NotUse   0.287812   0.189765   0.477577 (  1.165129)
Use      0.472995   0.286107   0.759102 (  7.442165)

Perhaps, validate 2600 objects by 6 milliseconds

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