Skip to content

Instantly share code, notes, and snippets.

View dbenhur's full-sized avatar

Devin Ben-Hur dbenhur

View GitHub Profile
@dbenhur
dbenhur / match_method.rb
Created December 8, 2011 01:03 — forked from avdi/match_method.rb
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, &blk|
if matcher === method_name.to_s
instance_exec(method_name, *args, blk, &method_body)
else
-- Filter an array of events such that there is only one event with each event_id.
-- When more than one event with the same event_id, is present, take the latest one.
CREATE OR REPLACE FUNCTION dedupe_events_2(events HSTORE[]) RETURNS HSTORE[] AS $$
SELECT array_agg(event)
FROM (
-- select first of elements with the same event_id by position in the array, descending.
SELECT first_value(event) as event
OVER (PARTITION BY (event -> 'event_id')::BIGINT ORDER BY index DESC)
FROM (
-- Use unnest instead of generate_subscripts to turn an array into a set.
@dbenhur
dbenhur / block_capture_penalty.rb
Last active August 29, 2015 14:17
Microbenchmark showing the performance penalty of simply specifying a block capture i method arguments
#!/usr/bin/env ruby
require 'benchmark'
N = 10_000_000
def no_capture
end
def block_capture(&_block)
@dbenhur
dbenhur / 0 overview how to reconcile various zk watch metrics
Last active August 29, 2015 14:16
Zookeeper metric discrepencies
# acquire watch metrics
for c in wchs wchc wchp; do echo $c | nc zookeeper3.prod.pages 2181 >$c.zk3; done
# repeated inquiries of wchs show no jitter, these numbers are stable over times much longer
# than the for loop above
cat wchs.zk3
2013 connections watching 218 paths
Total watches:5642
# During this same period, the zk_watch_count metric being reported to graphite was ~ 9200