Skip to content

Instantly share code, notes, and snippets.

@loopj
loopj / dotfiles.sh
Created June 17, 2012 21:01
Bootstrap dotfiles
#!/bin/sh
#
# Quickly set up a server the way I like it (dotfiles + zsh + oh-my-zsh)
#
# Requires git and zsh to be installed on the machine
#
# Usage:
# curl https://raw.github.com/gist/2945733/82c7487f5a4d99508e7a6150dfa88156b8e88943/dotfiles.sh | sh
#
@loopj
loopj / install-ruby-1.9-ubuntu.sh
Created June 17, 2012 03:59
Install Ruby 1.9 on Ubuntu 12.04
#!/bin/sh
# Make sure we have the latest package info
apt-get update
# Install ruby 1.9 and some other useful related packages
apt-get install -y ruby1.9.3 build-essential libssl-dev zlib1g-dev
# Let "update-alternatives" know about the new ruby
update-alternatives \
@loopj
loopj / install-nginx-with-spdy.sh
Created June 17, 2012 02:04
Install Nginx with SPDY support.
#!/bin/sh
NGINX_VERSION=1.3.4
TMP_PATH=/tmp
# Fetch and extract nginx source
cd $TMP_PATH
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
tar xvfz nginx-$NGINX_VERSION.tar.gz
cd nginx-$NGINX_VERSION
@loopj
loopj / mongo-functions.js
Created June 8, 2012 18:58
Useful Mongo Shell Functions
function getCurrentOpStats() {
intervals = [1,5,10,30]
waitingForLock = 0;
secsRunningStats = {};
inProg = db.currentOp()["inprog"]
inProg.forEach(function (op) {
if(op["waitingForLock"]) {
waitingForLock += 1;
}
@loopj
loopj / android-memory.java
Created May 29, 2012 00:51
Android memory metrics
// Device memory data
ActivityManager activityManager = (ActivityManager) androidContext.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
// Runtime data memory numbers are in bytes (dalvik process?)
int processors = runtime.availableProcessors();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
System.out.println(String.format("Runtime: %d processors, %d totalMemory (bytes), %d freeMemory (bytes)", processors, totalMemory, freeMemory));
// Debug data (system wide) memory numbers are in bytes
@loopj
loopj / average_timer.rb
Created January 7, 2012 00:32
Rough script for counting how many events per second you are doing, with rolling average
# Usage:
#
# timer = AverageTimer.new
# my_loop do
# # Track an avent
# timer.event
#
# # Print the events per second (averaged over 5 seconds)
# puts timer.events_per_second(5)
#
@loopj
loopj / redis-cache-example.coffee
Created December 30, 2011 10:47
Redis passthrough cache for node
# Example usage
AvatarCache = new RedisCache
keyPrefix: "userAvatar:"
expiry: 3600
generator: (key, callback) ->
request "https://api.github.com/users/#{key}", (error, response, body) ->
if !error and response.statusCode == 200
user = JSON.parse(body)
generate user.avatar_url
#
# Requirements for js framework
#
# - Cache Page "chrome", javascripts, css, templates in app-cache
# - Optionally
# - Load json data from android/ios client (eg. android Parcelable data)
# - Load json data immediately from cache, if present
# - Load json data from http json endpoint, store to cache
# - Update view elements when associated model data changes (knockout.js?)
class MongoHelpers
# Sum over fields
def self.sum(collection, fields, group_by, conditions=nil)
map = <<-EOD
function() {
self = this;
key = {};
if(group_by && group_by.length > 0) {
group_by.forEach(function (g) {
@loopj
loopj / colorlogcat.py
Created March 11, 2011 20:36
Script to highlight Android's adb logcat output for console
#!/usr/bin/python
'''
Copyright 2009, The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0