Skip to content

Instantly share code, notes, and snippets.

@prathmeshranaut
prathmeshranaut / log_sql.rb
Created June 16, 2020 15:17
Stacktracing SQL in Ruby
# typed: false
# frozen_string_literal: true
#
# Log SQL calls that get made to stdout along with their backtrace with formatting
#
# Usage:
# LogSql.start
# ...
# <code with db calls you wish to log>
# ...

Before

Rehearsal ----------------------------------------------
pool - 10    0.060000   0.000000   0.060000 (  0.065491)
pool - 100   0.050000   0.000000   0.050000 (  0.058047)
------------------------------------- total: 0.110000sec
            user     system      total        real
pool - 10    0.140000   0.000000   0.140000 (  0.145850)
pool - 100   0.040000   0.000000   0.040000 (  0.040523)

After

module Celluloid
module Supervision
class Container
# Manages a fixed-size pool of actors
# Delegates work (i.e. methods) and supervises actors
# Don't use this class directly. Instead use MyKlass.pool
class Pool
include Celluloid
trap_exit :__crash_handler__
➜ celluloid git:(multiplex) ✗ ruby benchmarks/pool.rb
I, [2016-08-10T20:03:53.171191 #13540] INFO -- : Celluloid 0.17.5 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
Celluloid::Group::Pool
Rehearsal -----------------------------------------------
pool - 10 2.190000 0.110000 2.300000 ( 2.277025)
pool - 100 1.290000 0.060000 1.350000 ( 1.335328)
pool - 1000 2.090000 0.050000 2.140000 ( 2.130047)
-------------------------------------- total: 5.790000sec
user system total real
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "celluloid/autostart"
require "benchmark"
class Worker
include Celluloid
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "celluloid/autostart"
require "benchmark"
class Worker
include Celluloid
require "logger"
require "thread"
require "timeout"
require "set"
$CELLULOID_DEBUG = false
$CELLULOID_MANAGED ||= false
require "celluloid/version"
require "celluloid/notices"
#!/usr/bin/env ruby
$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
require "celluloid/autostart"
require "mathn"
#
# Basic spawn example
#
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'celluloid/autostart'
require 'benchmark'
message = :ohai
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'celluloid/autostart'
require 'benchmark'
message = :ohai