Skip to content

Instantly share code, notes, and snippets.

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / call_metrics.rb
Last active Mar 24, 2019
Trace metrics for methods defined in Ruby code
View call_metrics.rb
require 'pp'
require 'set'
module CountThings
COUNTS = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = 0 } }
TIMINGS = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } }
TRACED_METHODS = Hash.new { |h, k| h[k] = Set.new }
module_function
@jgaskins
jgaskins / hooks.rb
Last active Feb 16, 2019
Hooks implementation for Clearwater
View hooks.rb
module Hooks
def self.component *attrs, &render
Class.new(Component) do
attr_reader *attrs
define_method :initialize do |**props|
super()
attrs.each { |attr| `self[#{attr}] = #{props[attr]}` }
end
@jgaskins
jgaskins / sort-videos.js
Created Dec 11, 2018
Sort Confreaks event videos by number of views
View sort-videos.js
[...document.querySelectorAll('.video-info')]
.sort((a, b) => (
a.querySelector('.video-views strong').innerText|0) >= (b.querySelector('.video-views strong').textContent|0)
? -1
: 1
).map(v => v.querySelector('.video-title').innerText + ' by ' + v.querySelector('.video-presenters').innerText)
@jgaskins
jgaskins / benchmark_optimized_uuid.cr
Created Nov 6, 2018
Optimizing UUID generation from strings in Crystal
View benchmark_optimized_uuid.cr
require "uuid"
require "benchmark"
struct UUID
def self.optimized(value : String, variant = nil, version = nil)
bytes = uninitialized UInt8[16]
case value.size
when 36 # Hyphenated
{8, 13, 18, 23}.each do |offset|
@jgaskins
jgaskins / benchmark_if_vs_exception.rb
Created Oct 21, 2018
Benchmarking conditionals vs exceptions
View benchmark_if_vs_exception.rb
require 'benchmark/ips'
def expensive_operation duration=0.001
sleep duration
false
end
Benchmark.ips do |x|
x.report 'simple if' do
if false
@jgaskins
jgaskins / app.cr
Created Oct 17, 2018
Forking HTTP server
View app.cr
class PrintPid
include HTTP::Handler
def call(context)
context.response.puts Process.pid
end
end
workers = Array.new(8) do |i|
process = fork do
server = HTTP::Server.new([
@jgaskins
jgaskins / benchmark.rb
Created Sep 5, 2018
Benchmark callable vs proc as event handlers
View benchmark.rb
class EventHandler
def call event
end
end
class Eventable
def initialize
@events = Hash.new { |h, k| h[k] = [] }
end
@jgaskins
jgaskins / worker.rb
Created Sep 4, 2018
Distribute a large Sidekiq job across many smaller ones
View worker.rb
class ProcessThings
include Sidekiq::Worker
def perform(ids: Thing.ids)
ids.each(&Individual.method(:perform_async))
end
class Individual
include Sidekiq::Worker
@jgaskins
jgaskins / render_promise.rb
Created Jun 28, 2018
Rendering a promise with Clearwater
View render_promise.rb
require 'clearwater/black_box_node'
require 'clearwater/component'
require 'clearwater/application'
class RenderPromise
include Clearwater::BlackBoxNode
DEFAULT = proc { Clearwater::Component.span }
Wrapper = Struct.new(:render).include(Clearwater::Component)
@jgaskins
jgaskins / app.rb
Last active Feb 16, 2019
API app with Roda
View app.rb
require 'authentication'
class MyApp < Roda
include Authentication
plugin :json
route do |r|
r.on('docs') { r.run Docs }
You can’t perform that action at this time.