Skip to content

Instantly share code, notes, and snippets.

View splattael's full-sized avatar
🏠
Working from home

Peter Leitzen splattael

🏠
Working from home
View GitHub Profile
@splattael
splattael / route.rb
Created February 16, 2016 11:37
Rails route rewriting
Rails::Application.routes.draw do
...
get '/shop/facebook' => 'facebook_shop#index'
get '/shop/paypal' => 'paypal_shop#index'
rewrite_shop_path = proc do |env|
if env['HTTP_HOST'] =~ /facebook/
env['PATH_INFO'] = "/shop/facebook"
else
@splattael
splattael / 10k_multi_thread.rb
Last active January 12, 2016 16:56 — forked from solnic/10k_multi_thread.rb
Validating 10k hashes 1 thread vs 2 (mri, jruby, rbx results)
require 'dry-validation'
require 'active_model'
require 'benchmark'
schema = Class.new(Dry::Validation::Schema) do
key(:name, &:filled?)
key(:age) { |v| v.int? & v.gt?(18) }
end.new
class User
@splattael
splattael / snafu.rb
Last active September 16, 2016 12:57
"self has wrong type to call super in this context" under weird circumstances
module ThreadSafe
class NonConcurrentCacheBackend
def self.method(key, value)
end
end
class MriCacheBackend < NonConcurrentCacheBackend
WRITE_LOCK = Mutex.new
def self.method(key, value)
@splattael
splattael / type_safe_entity_vs_ar.rb
Last active December 29, 2015 11:57 — forked from solnic/type_safe_entity_vs_ar.rb
Fetching type-safe entity with rom repo vs AR
Calculating -------------------------------------
type-safe users 273.000 i/100ms
ar user models 257.000 i/100ms
-------------------------------------------------
type-safe users 2.813k (± 1.7%) i/s - 14.196k
ar user models 2.574k (±10.7%) i/s - 12.850k
Comparison:
type-safe users: 2812.7 i/s
ar user models: 2574.2 i/s - 1.09x slower
@splattael
splattael / bench.rb
Last active December 10, 2015 20:59
Compiling...
require "benchmark/ips"
HASH = { :foo => 1, :bar => 2 }
def hc
{
:foo => HASH.fetch(:foo),
:bar => HASH.fetch(:bar),
}
end
# apt-get install crystal
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package crystal
@splattael
splattael / test.rb
Last active August 29, 2015 14:19
mt 5.6
require 'minitest/spec'
require 'minitest/autorun'
require 'ostruct'
describe "Failure" do
it "fails with value" do
data = OpenStruct.new(:value => "bar")
assert_equal "bar", data.value
end
@splattael
splattael / allocation_tracer.rb
Created February 28, 2015 09:21
Trace object allocations and sum them per path
require 'allocation_tracer'
ObjectSpace::AllocationTracer.setup(%i{path line type})
ObjectSpace::AllocationTracer.trace
at_exit{
results = ObjectSpace::AllocationTracer.stop
sum = Hash.new { |hash, key| hash[key] = [0] * 6 }
puts ObjectSpace::AllocationTracer.header.join("\t")
results.sort_by{|k, v| v[0]}.each{|k, v|
path, line, type = k
require 'set'
require 'benchmark/ips'
KEYS = (:a .. :z).to_a
SET = Set.new(KEYS)
HASH = Hash[KEYS.product([true])]
Benchmark.ips do |x|
x.report "set.include" do
SET.include?(:z)
end
@splattael
splattael / basic.rb
Created February 24, 2015 09:08
ROM benchmark
#!/usr/bin/env ruby
# encoding: utf-8
require_relative 'setup'
run "Loading ONE user object" do |x|
x.expect { |user| user.name == 'name 1' }
x.report("AR") { ARUser.by_name('name 1').first }
x.report("ROM") { users.by_name('name 1').first }
end