# config/application.rb
# require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
# require "active_job/railtie"
require "active_record/railtie"
# require "action_controller/railtie"
# require "action_mailer/railtie"
# require "action_view/railtie"
# require "action_cable/engine"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"
rails new awesome_ruby_app --api
class ApplicationController < JSONAPI::ResourceControllerMetal
include Knock::Authenticable
before_action :authenticate_user
def head(type, options = {}) end # ...
end
gem "jsonapi-resources"
Started GET "/mail-logs" for ::1 at 2017-03-17 18:50:46 +0100
Processing by MailLogsController#index as API_JSON
MailLog Load (0.4ms)
SELECT "mail_logs".* FROM "mail_logs"
ORDER BY "mail_logs"."number"
DESC LIMIT $1 OFFSET $2 [["LIMIT", 20], ["OFFSET", 0]]
(0.3ms) SELECT COUNT(*) FROM "mail_logs"
Completed 200 OK in 7ms (Views: 0.0ms)
Requests per second: 250.07 [#/sec] (mean)
Time per request: 3.999 [ms] (mean)
Time per request: 3.999 [ms] (mean, across all concurrent requests)
Percentage of the requests served within a certain time (ms)
50% 4
66% 4
75% 4
80% 4
90% 4
95% 5
98% 5
99% 6
100% 39 (longest request)
# frozen_string_literal: true
require 'logger'
require 'sinatra'
require 'sequel'
require 'sinatra/jsonapi'
require 'sinja/sequel/helpers'
DB = Sequel.connect('postgres://localhost/cl-api_development')
DB.extension(:freeze_datasets)
DB.extension(:pagination)
DB.optimize_model_load = true
DB.loggers << Logger.new($stderr) if Sinatra::Base.development?
Sequel::Model.plugin :tactical_eager_loading
class MailLog < Sequel::Model
many_to_one :category
many_to_one :company
end
class Category < Sequel::Model
one_to_many :mail_log
end
class Company < Sequel::Model
one_to_many :mail_log
end
Sequel::Model.finalize_associations
Sequel::Model.freeze
class BaseSerializer
include JSONAPI::Serializer
end
class MailLogSerializer < BaseSerializer
attributes :number, :in_or_out, :document_date_on, :received_on, :external_number, :comment, :value_net, :value_vat
has_one :category
has_one :company
end
class CategorySerializer < BaseSerializer
attributes :name
has_many :mail_logs
end
class CompanySerializer < BaseSerializer
attributes :name, :icon
has_many :mail_logs
end
class MailLogs < Sinatra::Application
register Sinatra::JSONAPI
resource 'mail-logs' do
index do
MailLog.dataset
end
end
end
freeze_jsonapi
Requests per second: 746.18 [#/sec] (mean)
Time per request: 1.340 [ms] (mean)
Time per request: 1.340 [ms] (mean, across all concurrent requests)
Transfer rate: 2731.85 [Kbytes/sec] received
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 2
95% 2
98% 2
99% 2
100% 5 (longest request)
Michał Czyż
@cs3b
@paweldude
(limiting elixir to single core `--erl "+S 1"`)
defmodule PlugBench.Repo do
use Ecto.Repo, otp_app: :plug_bench
end
defmodule PlugBench.Category do
use Ecto.Schema
schema "categories" do
field :name, :string
has_many :mail_log, PlugBench.MailLog
end
end
defmodule PlugBench.CategorySerializer do
use JaSerializer
location "/categories/:id"
attributes [:name]
end
defmodule PlugBench.Company do
use Ecto.Schema
schema "companies" do
field :name, :string
field :icon, :string
has_many :mail_log, PlugBench.MailLog
end
end
defmodule PlugBench.CompanySerializer do
use JaSerializer
location "/companies/:id"
attributes [:name]
end
defmodule PlugBench.MailLog do
use Ecto.Schema
@derive {Poison.Encoder, only: [:number, :in_our_out, :document_date_on, :received_on, :external_number, :comment, :value_net, :value_vat, :lp, :inserted_at, :updated_at]}
schema "mail_logs" do
field :number, :string
field :in_or_out, :string
field :document_date_on, :date
field :received_on, :date
field :external_number, :string
field :comment, :string
field :value_net, :decimal
field :value_vat, :decimal
field :lp, :integer
belongs_to :category, PlugBench.Category
belongs_to :company, PlugBench.Company
end
end
defmodule PlugBench.MailLogSerializer do
use JaSerializer
location "/mail-logs/:id"
attributes [:name, :number, :in_or_out, :document_date_on, :received_on,
:external_number, :comment, :value_net, :value_vat, :lp]
has_one :company,
serializer: PlugBench.CompanySerializer,
include: true,
field: :company_id
has_one :category,
serializer: PlugBench.CategorySerializer,
include: true,
field: :category_id
end
defmodule PlugBench.Endpoint do
import Plug.Conn
use Plug.Router
plug :match
plug Plug.Logger
plug Plug.Parsers, parsers: [:urlencoded],
pass: ["*/*"],
accept: ["*/*"]
plug :dispatch
get "/mail-logs" do
# it's not jsonapi yet
send_resp(conn, 200, PlugBench.Repo.all(PlugBench.MailLog) |> Poison.encode!)
end
match _ do
conn
|> put_resp_content_type("application/json")
|> send_resp(410, ~s({"status": "bad request"}))
end
end
Requests per second: 1306.28 [#/sec] (mean)
Time per request: 0.766 [ms] (mean)
Time per request: 0.766 [ms] (mean, across all concurrent requests)
Transfer rate: 1865.99 [Kbytes/sec] received
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 1
98% 2
99% 2
100% 60 (longest request)
Michał Czyż
@cs3b