Skip to content

Instantly share code, notes, and snippets.

View brianhempel's full-sized avatar

Brian Hempel brianhempel

View GitHub Profile
@brianhempel
brianhempel / bench_rails_memory_usage.rb
Last active October 6, 2022 12:47
A script to test the memory usage of your Rails application over time. It will run 30 requests against the specified action and report the final RSS. Choose the URL to hit on line 45 and then run with `ruby bench_rails_memory_usage.rb`.
require "net/http"
def start_server
# Remove the X to enable the parameters for tuning.
# These are the default values as of Ruby 2.2.0.
@child = spawn(<<-EOC.split.join(" "))
XRUBY_GC_HEAP_FREE_SLOTS=4096
XRUBY_GC_HEAP_INIT_SLOTS=10000
XRUBY_GC_HEAP_GROWTH_FACTOR=1.8
XRUBY_GC_HEAP_GROWTH_MAX_SLOTS=0
@brianhempel
brianhempel / remove_rightslink_images
Last active April 23, 2021 20:31
Remove ugly “RightsLink” images in PDFs from the new ACM Digital Library.
@brianhempel
brianhempel / all_s3_objects.rb
Created March 21, 2012 15:21
List/fetch all objects in a bucket with AWS::S3 Ruby gem
# by default you only get 1000 objects at a time
# so you have to roll your own cursor
S3.connect!
objects = []
last_key = nil
begin
new_objects = AWS::S3::Bucket.objects(bucket_name, :marker => last_key)
objects += new_objects
@brianhempel
brianhempel / enumerator_to_json.rb
Created February 17, 2015 17:32
Lazily convert an Enumerator or Enumerator::Lazy to a JSON array. Save as config/initializers/enumerator_to_json.rb
# Natively, Enumerators get JSONized like "#<Enumerator::Lazy:0x007f8714807080>", or they explode, either of which is a problem.
# We want them to make an array, and do it lazily so we don't have to keep the items in memory!
class Enumerator
def to_json(state)
state.depth += 1
string = "[\n"
first_item = true
self.each do |item|
@brianhempel
brianhempel / DEFLATE_BREACH.md
Last active April 5, 2019 05:41
A working proposal for a BREACH-safe DEFLATE compressor.
@brianhempel
brianhempel / production.rb
Created June 22, 2012 19:10
Production deploy checks
# config/deploy/production.rb
# okay, this is to make sure I don't miss another deploy.
def abort_deploy
puts "Deploy aborted."
exit 1
end
unless `git show-ref heads/stable`.split[0] == `git show-ref heads/master`.split[0]
@brianhempel
brianhempel / retina_rails_benchmarks.txt
Created June 18, 2012 19:33
Retina MacBook Pro Rails Benchmarks (Mid-2012, 15-Inch)
$ time bundle exec cucumber features/admin/authentication.feature
MID-2007 15-INCH MACBOOK PRO
2.2 GHz Core 2 Duo 6GB 320GB 7200rpm HD
# Hot
startup 24.7s
total 28.4s
# Cold
startup 38.3s
total 43.3s
@brianhempel
brianhempel / id_rsa.pub
Created May 22, 2012 14:27
Brian Hempel's Public Key
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1VmCogSSRe2/OWkNI1Be5IxLcr5iX2hlArqCqHPXqzqLP9DQeU93g1Ml0HpdLq2FRqIbWNDLMmULu9zzFqAQIqPfqwonLqjnH3TiiUv0gh9Dg+ePFkGU7FYGtD3EEvFoZEmlEoZC61HLYY0AcItDM2mRVWi4gWw9nJm7E0iZYUylaLxpQVxFDuqWg6CCgHj6GxMQ8IoQp5feuPCe9a70B9w8zqhhNOlKfIddxx2pcBU2gjHyVfslWr4WuRziuZ3nHmnleCawirLi4GyoWhEsGeKw0pDRfazZyVmLgOSF3gB1qxJddFmRHYZ6a9s4oMOdxf54C6j7hb9dKvoihMde2Q== brian@wads-3-229-75.resnet.mtu.edu
@brianhempel
brianhempel / mass_rename.rb
Created April 23, 2012 15:16
Script for mass renaming of files via Regexp
#!/usr/bin/env ruby
require 'fileutils'
if ARGV.size < 3
puts <<-USAGE
mass_rename [file1 file2 ...] <regexp> <replacement>
USAGE
end
@brianhempel
brianhempel / jumpstart.rb
Created January 30, 2012 14:44
Pseudo-shell with Rails application.rb and environment.rb already loaded--like spork but no DRB and speeds up rake commands and rails console in addition to tests
#!/usr/bin/env ruby
require 'benchmark'
if ARGV[0]
ENV['RAILS_ENV'] = %w{development test production staging}.find { |e| e[0] == ARGV[0][0] } || ARGV[0]
else
ENV['RAILS_ENV'] ='test'
end