Skip to content

Instantly share code, notes, and snippets.

View ssimeonov's full-sized avatar

Simeon Simeonov ssimeonov

View GitHub Profile
@ssimeonov
ssimeonov / gist:6041586
Last active December 20, 2015 00:28
ActiveSupport::Cache::MemoryStore does not count the length of keys as it computes its size. This can lead to much more memory than expected being consumed, especially in the case where the keys are big (URLs, IP addresses, etc.) and the values are small, e.g., integers or booleans. In the example below a cache of size 100 bytes adds two entries…
2.0.0p247 :034 > cache = ActiveSupport::Cache.lookup_store :memory_store, size: 100
=> <#ActiveSupport::Cache::MemoryStore entries=0, size=0, options={:size=>100}>
2.0.0p247 :035 > key_prefix = '*' * 50
=> "**************************************************"
2.0.0p247 :036 > cache.fetch("#{key_prefix}-1") { 0 }
=> 0
2.0.0p247 :037 > cache.fetch("#{key_prefix}-2") { 0 }
=> 0
2.0.0p247 :038 > cache
=> <#ActiveSupport::Cache::MemoryStore entries=2, size=8, options={:size=>100}>
@ssimeonov
ssimeonov / memory_snapshot.rb
Created July 18, 2013 07:06
Analyze dyno memory use on Heroku with this memory footprint analyzer.
# Memory snapshot analyzer which parses the /proc file system on *nix
#
# Example (run in Heroku console):
#
# ms = MemorySnapshot.new
# 1.upto(10000).map { |i| Array.new(i) }; nil
# ms.snapshot!; nil
# ms.diff 10
# => {"lib/ld-2.11.1.so"=>156, "heap"=>2068, "all"=>2224}
#
@ssimeonov
ssimeonov / git-branch-cleanup.md
Last active December 19, 2015 14:49
Cleaning local git branches based on the number of branches they have been merged into.

One of the side-effects of using git branching aggressively is that you end up with a lot of branches in both the local and remote repositories.

For cleaning up branches on remote git repositories I use git-sweep. To remove local branches tied to remote branches that no longer exists I run git fetch --prune. This helps a lot but it doesn't handle the issue of local branch proliferation.

git branch --contains mybranch will list the branches that contain the commits of mybranch, i.e., the branches that mybranch has been merged into. If a branch has been merged into many branches it is a good candidate for cleanup.

To get a count of the number of branches all local branches have been merged into:

for k in $(git branch | tr -d "*");do git branch --contains "$k" | wc -l | tr -d '\n\t '; echo " $k" ;done | sort -n
# RSpec matcher to spec delegations.
# Forked from https://gist.github.com/txus/807456 with added bug fixes
#
# Usage:
#
# describe Post do
# it { should delegate(:name).to(:author).with_prefix } # post.author_name
# it { should delegate(:month).to(:created_at) }
# it { should delegate(:year).to(:created_at) }
# it { should delegate(:something).to(:'@instance_var') }
app.process("web-1") do |process|
process.start_command = "bundle exec thin start --ssl --ssl-verify --ssl-key-file web.key --ssl-cert-file web.pem -p 3000"
process.working_dir = working_dir
process.daemonize = true
process.environment = env
process.stop_signals = [:quit, 30.seconds, :term, 5.seconds, :kill]
process.stop_grace_time = 45.seconds
process.stdout = process.stderr = "log/web-1.log"
@ssimeonov
ssimeonov / bluepill_override.rb
Created June 26, 2013 03:04
This gist shows how to make the Bluepill Chef cookbook use rvm. The trick is to subclass the provider in the Bluepill LWRP and override shell_out! to make it use rvm_shell instead.
class BluepillOverride
def self.provider
Class.new(Chef::Provider::BluepillService) do
def shell_out!(*args)
args.each do |command|
rvm_shell "#{command}" do
code command
end
end
end
@ssimeonov
ssimeonov / 01_example.rb
Last active December 18, 2015 16:29
Generalized Chef LWRP subclassing and inheritance example. Chef::LWRPFactory will even generate ChefSpec matchers for new resources.
#
# Cookbook Name:: rake-chef
# Recipe:: spec
#
# Copyright (C) 2013 Swoop, Inc.
#
include_recipe 'rvm'
# rake subclasses rvm_shell LWRP
@ssimeonov
ssimeonov / chef_resource_rake.rb
Created June 18, 2013 22:40
Chef LWRP subclassing example: rake resource subclasses rvm_shell.
class Chef
class ResourceFactory
class Rake
def self.create(*args, &block)
register
Chef::Resource::Rake.new(*args, &block)
end
def self.register
@klass ||= begin
1.9.3p392 :001 > r = Redis.new
=> #<Redis client v3.0.4 for redis://127.0.0.1:6379/0>
1.9.3p392 :002 > r.set("key:5", "\xad\xad")
=> "OK"
1.9.3p392 :003 > r.get("key:5")
=> "\xAD\xAD"
mkdir ~/.wireshark/plugins && cd ~/.wireshark/plugins && curl -O https://raw.github.com/jzwinck/redis-wireshark/master/redis-wireshark.lua