Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# A script for profiling and benching Rails actions, because Rails's default scripts suck.
# Examples:
#
# Run a profile on 10 runs of /widgets/1
# ./action_performance.rb /widgets/1 10 profile
#
# Run a benchmark on 15 runs of /widgets/1
# ./action_performance.rb /widgets/1 15 bench
<h1><%= @video.title %></h1>
<div class="ad">
<%= image_tag(@video.ad_url) %>
<p class="description"><%= @video.ad_shortDescription %></p>
</div>
@heisters
heisters / sprite_animator.js
Created April 30, 2010 17:20
Turn a sprite image into an animation and easily control playback
(function($){
/*
* var sa = new $.SpriteAnimator({
* url: "/images/carousel-spinner-sprites.gif",
* duration: 5000,
* sprite: {height: 19, width: 18},
* frames: 8
* });
* $("body").append(sa.element);
*/
# Causes a segfault
#
# $ ruby crash.rb
# parsed
# selected
# crash.rb:12: [BUG] Segmentation fault
# ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-linux]
#
# Aborted
#!/usr/bin/env ruby
#
# Get the lines of code in a file, over time as CSV.
require 'rubygems'
require 'fastercsv'
file = ARGV[0] or abort("Must specify a file")
log = `git log --pretty=format:"%H %ai" #{file}`
log.split(/[\r\n]/).map do |log_line|
commit, date = log_line.split(/ /)
@heisters
heisters / patient.rb
Created July 22, 2010 17:16
Make a thread wait for Ruote to do something notable
module Patient
class NestedWaitForError < StandardError; end
class << self
def lock
@lock ||= Mutex.new
end
def waiting
lock.synchronize{@waiting}
@heisters
heisters / benchmarks.rb
Created September 8, 2010 19:33
Monkey patches for memory bloat issues in Ruote
# You should kill the Ruby process in between runs to flush out the memory
# entirely.
puts "memory usage: #{MemoryUsage.get}"
n = 5
Benchmark.bm do |r|
r.report do
n.times do
Workflow.engine.context.history.by_process("20100826-behobijunu")
end
end
@heisters
heisters / parse_ua_strings.rb
Created October 18, 2010 22:05
Get statistics on browser and OS versions from a file of User-Agent strings
#!/usr/bin/env ruby
require 'rubygems'
require 'user-agent'
require 'rational'
require 'yaml'
parsed = nil
File.open(ARGV[0], 'r') do |f|
parsed = f.each_line.map{|l|Agent.new(l.strip)}
end
# db/migrations/2010-11-12_10_54_copy_data_to_new_storage_backend.rb
couch = Workflow::Engine.build_couch_storage
redis = Workflow::Engine.build_redis_storage
# based on http://groups.google.com/group/openwferu-users/browse_thread/thread/8d32d3052eacfba6/423f33bb4b44189f and
# Ruote::StorageBase#copy_to
#
# just limits the types of data that are copied over as compared to copy_to
%w(
@heisters
heisters / gist:674562
Created November 12, 2010 19:42
migration error
/home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/storage/base.rb:280:in `[]': can't convert String into Integer (TypeError)
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/storage/base.rb:280:in `filter_schedules'
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/storage/base.rb:280:in `select'
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/storage/base.rb:280:in `filter_schedules'
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/storage/base.rb:143:in `get_schedules'
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/storage/composite_storage.rb:123:in `send'
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/storage/composite_storage.rb:123:in `get_schedules'
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/ruote/worker.rb:141:in `step'
from /home/ian/.rvm/gems/ruby-1.8.7-p174/gems/ruote-2.1.10/lib/r