Skip to content

Instantly share code, notes, and snippets.

var HTTP = require('http'), Sys = require('sys');
function readableSize(bytes) {
var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
var e = Math.floor(Math.log(bytes)/Math.log(1024));
return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}
function getMem(nice) {
@nanha
nanha / round-robin-proxy.js
Created February 9, 2012 00:47 — forked from indexzero/round-robin-proxy.js
A simple example of how to do round-robin proxying for a single domain
var httpProxy = require('http-proxy');
//
// Addresses to use in the round robin proxy
//
var addresses = [
{
host: 'ws1.0.0.0',
port: 80
},
@nanha
nanha / b2.js
Created February 5, 2012 02:08
var startFast;
var startSlow;
var size = 1000000; // million
var loopCount = 0;
var setTimeoutTest = function () {
loopCount++;
if ( loopCount == ( size - 1 ) ) {
console.log( 'setTimeout result: ' + ( new Date() - startFast ) + 'ms' );
@nanha
nanha / nextTick.js
Created February 4, 2012 11:35 — forked from laverdet/nextTick.js
nextTick will ALWAYS fire before a setTimeout from the same tick
setTimeout(function() {
console.log('timeout');
}, 0);
// simulate extreme cpu load
var d = +new Date;
console.log(new Date);
while (d + 2000 > new Date);
console.log(new Date);
@nanha
nanha / chat_room.opa
Created January 31, 2012 02:01 — forked from strobemonkey/chat_room.opa
Opa Hello World examples
type message = {author: string
; text: string}
room = Network.cloud("room"): Network.network(message)
user_update(x: message) =
line = <div class="line">
<div class="user">{x.author}:</div>
<div class="message">{x.text}</div>
</div>
@nanha
nanha / gist:1696879
Created January 29, 2012 02:42
HAProxy Config with ACL for Load Balancing Multiple Named Virtual Hosts
# Config ideal for load balancing many back-end Virtual Hosts with a pair of load balancers (one is for fail-over).
# HAProxy's ACL is ideal for dedicating more resources to certain virtual hosts especially to handle traffic spikes.
# Reference:
# http://www.techrawr.com/2009/09/18/using-the-acl-in-haproxy-for-load-balancing-named-virtual-hosts
# Don't copy blindly, change to suit your needs; at a minimum:-
# change maxconn based on your back-end server(s) capabilities
# enable/disable cookie option - disable if you don't want sticky sessions
@nanha
nanha / application.js
Created January 27, 2012 13:03 — forked from fabware/application.js
socket.io application and haproxy configuration
var express = require('express');
var redis = require('redis');
const serverType = process.argv[2];
const serverHost = process.argv[3];
const serverPort = parseInt(process.argv[4]);
const redisPort = 6379;
const redisHost = '127.0.0.1';
@nanha
nanha / nextTick.js
Created January 27, 2012 07:58 — forked from xk/nextTick.js
nextTick.js
(function (ctr,t) {
function inc () { ctr++ }
function display (ntps,ntpsStr,i,lps,lpsStr,ratioStr) {
ntps= ctr*1e3/(Date.now()-t); //nextTicks per second
ntpsStr= ", nextTicksPerSecond: "+ ntps.toFixed(1);
ctr= 0, i= 100e6, t= Date.now();
while (i--) { inc() }
@nanha
nanha / b2.js
Created January 27, 2012 07:55
var startFast;
var startSlow;
var size = 1000000; // million
var loopCount = 0;
var setTimeoutTest = function () {
loopCount++;
if ( loopCount == ( size - 1 ) ) {
console.log( 'setTimeout result: ' + ( new Date() - startFast ) + 'ms' );
@nanha
nanha / node console
Created January 25, 2012 13:02 — forked from benmonty/node console
simple node.js v8 example
> var p = require('./point');
> var o = new p.Point();
> o.setX(6);
> o.setY(9);
> console.log(o.get());
{ x: 6, y: 9 }