Skip to content

Instantly share code, notes, and snippets.

View kares's full-sized avatar
💤

Karol Bucek kares

💤
View GitHub Profile
require 'bundler/setup'
require 'celluloid/autostart'
require 'celluloid/probe'
Celluloid.logger = ::Logger.new './celluloid.log'
class UselessActor
include Celluloid
end
/* This is when a refactoring really pays off.
*
* In order to make your code more modular, avoid hard-coding assumptions (or refactor them away).
* The most fundamental, anti-modular assumption in Object-Oriented software is the concrete type of objects.
* Any time you write "new MyClass" in your code (or in Ruby MyClass.new) you've hardcoded
* an assumption about the concrete class of the object you're allocating. These makes it impossible, for example,
* for someone to later add logging around method invocations of that object, or timeouts, or whatever.
*
* In a very dynamic language like Ruby, open classes and method aliasing mitigate this problem, but
* they don't solve it. If you manipulate a class to add logging, all instances of that class will have

JRuby Memcached Client Benchmark Notes

These benchmarks were run using a modified copy of Ruby Memcached's test/profile/benchmark.rb that adds several JRuby-only clients into the mix.

The benchmarking script also runs an Ehcache client for (an unfair) comparison. Ehcache is a configurable Java-based cache, and runs in-memory for this benchmark using the jruby-ehcache-rails3 gem.

The benchmarks were run with the JVM's server (optimizing) compiler. Since the JVM warms up as it goes, later runs are smaller and take into account the warmup.

For cross-Ruby comparison I also ran the same script to exercise the Ruby Memcached/libmemcached client using Ruby 1.9.2-p0.

$ jirb
irb(main):001:0> load 'jruby_thread_dump.rb'
# Elsewhere...
$ kill -USR2 <pid>
Ruby Thread Dump
Thread[SIGUSR2 handler,9,system]
@croaky
croaky / gist:1089598
Created July 18, 2011 14:01
Transfer data from production to staging on Heroku
heroku addons:add pgbackups --remote staging
heroku addons:add pgbackups --remote production
heroku pgbackups:capture --remote production
heroku pgbackups:restore DATABASE `heroku pgbackups:url --remote production` --remote staging
@avdi
avdi / match_method.rb
Created December 6, 2011 21:27
Defining method_missing and respond_to? in one fell swoop
# Do you ever define #method_missing and forget #respond_to? I sure
# do. It would be nice if we could do them both at the same time.
module MatchMethodMacros
def match_method(matcher, &method_body)
mod = Module.new do
define_method(:method_missing) do |method_name, *args|
if matcher === method_name.to_s
instance_exec(method_name, *args, &method_body)
else
@avdi
avdi / ar-class-methods.rb
Created January 19, 2012 01:08
Confused about class methods on association proxies in AR
class Category < ActiveRecord::Base
belongs_to :sock
def self.foo
relation.build(:name => "bar")
end
end
class Sock < ActiveRecord::Base
has_many :categories
end
@cowboyd
cowboyd / inject_this.rb
Created April 24, 2012 17:01
Injecting an value into a binding
# Is there a better way to inject an arbitrary value into a binding?
def inject_this_as_reference(block, this_object)
eval("lambda {|v| @this = v}", block.binding).call(this_object)
end
def inject_this_as_method(block, this_object)
(class << eval('self', block.binding);self;end).send(:define_method, :this) {this_object}
end
@nicksieger
nicksieger / gist:2906871
Created June 10, 2012 18:43
Renaming a Java thread in JRuby
$ jirb
irb(main):001:0> t = Thread.new { sleep }
=> #<Thread:0x64e5b2 sleep>
irb(main):002:0> require 'jruby'
=> true
irb(main):003:0> jt = JRuby.reference(t)
=> #<Thread:0x64e5b2 sleep>
irb(main):004:0> jt.native_thread.name
=> "RubyThread-7: (irb):1"
irb(main):005:0> jt.native_thread.name = "My Ruby thread"
@the8472
the8472 / minimal testing up
Created December 12, 2012 16:28
Rails startup performance tuning with jruby. Note that those settings are *not* for optimal steady state operation.
### ruby 1.9.3 + railsexpress patches + GC tuning + -march=native -O3
time ruby script/rails r 'Post.count'
real 0m1.008s
user 0m0.820s
sys 0m0.180s
### jruby 1.7.2-dev 2012-12-12 + java 64bit 1.8.0-ea-b67 hotspot 25.0-b11