Skip to content

Instantly share code, notes, and snippets.

View gnarg's full-sized avatar

Jon Guymon gnarg

  • New Relic
  • Portland, OR
View GitHub Profile
def bisect(suspects, victim)
return suspects[0] if suspects.size == 1
pivot = suspects.size / 2
suspects.partition {|file| suspects.index(file) < pivot }.each do |part|
command = %[ruby -r test/unit -I ./vendor/gems/dependency_detection-0.0.1.build/lib -r dependency_detection -r./#{part.join(' -r ./')} #{victim}]
print command + "\n"
if system(command)
puts "------ PASSED: #{$?} -------"
@gnarg
gnarg / agent_listener.rb
Created November 13, 2012 19:45
swallow agent_listener calls
class AgentListener
def initialize(app, options = {})
@app = app
end
def call(env)
if /^\/agent_listener/ =~ ::Rack::Request.new(env).path_info
[404, {}, ['']]
else
@app.call(env)
@gnarg
gnarg / deferred_query.rb
Created November 6, 2012 07:38
Demo of light weight database futures
require 'benchmark'
ENV['RAILS_ENV'] ||= 'development'
require_relative 'config/environment'
class DeferredQuery
def initialize(query)
@thread = Thread.new do
ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.execute(query)
@gnarg
gnarg / sequel.rb
Created November 1, 2012 20:28
Old Sequel instrumentation for Ruby agent
## NewRelic instrumentation for Sequel
#
# This is based heavily on the DataMapper instrumentation.
# See data_mapper.rb for major differences compared to the ActiveRecord instrumentation.
#
DependencyDetection.defer do
depends_on do
defined?(::Sequel)
end
@gnarg
gnarg / gist:3982829
Created October 30, 2012 20:34
odd behavior of String#tr when applied to single characters
irb(main):018:0> RUBY_ENGINE
=> "jruby"
irb(main):019:0> RUBY_VERSION
=> "1.9.3"
irb(main):020:0> JRUBY_VERSION
=> "1.7.0"
irb(main):027:0> 'x'.tr('x', '_') # correct
=> "_"
irb(main):028:0> 'x'.tr('^x', '_') # incorrect
=> "_"
@gnarg
gnarg / after.dot
Created October 29, 2012 23:27
Graphviz files for reference graph refactoring example
digraph AlphaSoup {
"@state0"
"@state1"
"@state2"
node[shape=box]
"E.report"
node[color=green]
"A#ones"
@gnarg
gnarg / drb_socket_pair.rb
Created October 26, 2012 09:03
socketpair protocol for druby
require 'socket'
require 'drb/drb'
module DRbSocketPair
def self.open(uri, config={})
tag = parse_uri(uri)
parent_socket, child_socket = @parent.sockets[tag]
parent_socket.close unless parent_socket.closed?
Child.new(child_socket, config)
end
@gnarg
gnarg / drb_socket_pair.rb
Created October 25, 2012 21:21
drb socket pair protocol interface
class TimeServer
def get_current_time
Time.now
end
end
################
URI = 'druby://localhost:8787'
@gnarg
gnarg / bigdata.rb
Created October 17, 2012 16:44
histogram coding challenge
def simple(file)
histogram = Hash.new.tap{|h| h.default = 0 }
File.read(file).each_line do |line|
histogram[line.split(',')[1]] += 1
end
histogram
end
def threading(file, thread_count=10)
histogram = Hash.new.tap{|h| h.default = 0 }
@gnarg
gnarg / timmy.rb
Created October 9, 2012 18:19
test app setup script
#!/usr/bin/env ruby
require 'yaml'
module Timmy
class Step
def self.priority
@priority
end
end