Skip to content

Instantly share code, notes, and snippets.

@djo
djo / gc_tuning.md
Created December 5, 2012 11:50
GC tuning

See the current amount of free and used memory in the system:

free -m

What actual time we have (in the console):

time = Benchmark.measure do
  app.get '/needed-page'
end

puts time

@djo
djo / requests.rb
Created December 12, 2012 13:06
Test script to profile requests with GC options in case you have only access into the console.
# Test script to profile requests with GC options in case you have only access into the console.
#
# Invoke the script in the staging/production console:
#
# RAILS_ENV=production rails console
# [1] pry(main)> load '/path/to/the-file/requests.rb'
#
# You will see benchmark results per link for analyzing.
#
# You should run it before and after changing GC options for comparison.
@djo
djo / mround.rb
Created December 13, 2012 17:28
Helper to round a number to the desired multiple 0.5.
# Returns a number rounded to the desired multiple 0.5,
# analog of excel's MROUND(A1, 0.05).
def mround(number)
(number * 20).round.to_f / 20
end
@djo
djo / README.md
Created January 14, 2013 13:29
README sample

Project

Some description how is awesome the project.

How to hit the ground running

  • Follow installation instructions in doc/INSTALLATION
  • Follow deployment instructions in doc/DEPLOYMENT
  • Follow workflow process in doc/WORKFLOW
@djo
djo / backup.sh
Created February 25, 2013 09:32
Backup DB scripts
current_date=`date +%Y-%m-%d-%H:%M`
file_name="db.$current_date.sql.gz"
file_path="/mnt/backup/$file_name"
mysqldump --single-transaction --quick -u*** -p*** DB | gzip -c > $file_name
echo `ls -la $file_path` | mail -s "[backup] DB" admin@site.com
@djo
djo / devise_integration.rb
Created March 6, 2013 11:39
Speed up capybara specs by setting user session directly in the app. To have access to the rack session use https://github.com/railsware/rack_session_access.
def fast_log_in
user = Factory.create(:user)
warden_key = User.serialize_into_session(user).unshift('User')
page.set_rack_session(:'warden.user.user.key' => warden_key)
user
end
@djo
djo / tar.txt
Created August 28, 2013 08:30
When you don't have time to read `man tar`.
# Create the archive
tar -zcvf archive.tar.gz /home/dir
# Restore the archive
tar -zxvf archive.tar.gz
tar -zxvf archive.tar.gz -C /tmp
@djo
djo / fsarchiver
Last active December 22, 2015 05:29
Dump a partition with fsarchiver
# http://www.fsarchiver.org/QuickStart
sudo apt-get install fsarchiver
# Find out what partition is used for the current folder and choose another partition for the dump
df -h .
df -h
# Re-mount /dev/md2 partition to read only mode
sudo mount -o remount,ro /dev/md2
sudo fsarchiver savefs ./ubuntu-md2.fsa /dev/md2
@djo
djo / dequeue.java
Last active April 9, 2017 18:22
Mistake in the Cracking Coding Interview 5th edition book: loitering problem in the queue implementation.
// Enhanced and fixed the dequeue method
if (first == null) return null;
Object item = first.data;
first = first.next;
if (first == null) last = null;
return item;
@djo
djo / towers.rb
Created January 22, 2014 13:58
The classic problem of the Towers of Hanoi.
require 'minitest/autorun'
class Towers
class CantPlaceError < StandardError; end
attr_accessor :from, :to
def initialize(from, to)
@from = from
@to = to