Skip to content

Instantly share code, notes, and snippets.

@iarna
iarna / gearman timings.md
Last active August 29, 2015 13:59
Relative gearman speeds with varous combinations of client/worker and server.

We're just issuing an echo here, which is the simplest command in the Gearman protocol. You send an echo packet to the server, the server responds back with what you sent it. The numbers here give a sense of how much overhead different approaches are adding.

The test here is with the word "ping". Substantially larger payloads are likely to be interesting, along with measures on time-to-first packet in the reply, as the node.js implementation is streaming end-to-end and will start replying before the original echo request actually completes. This also means that the entire echo body never need be held in memory.

  1. Client, server in one process, no packet encode/decode:

    echo: 4.07 wallclock secs @ 12289.63/s (n=50000)

  2. Client, server in one process, stream stubs:

var stream = require('stream');
function dosomething() {
var out = new stream.PassThrough();
out.write('two');
return out;
}
var source = new stream.PassThrough()
var stream = require('stream');
var DuplexCombination = require('duplex-combination');
function dosomething() {
var input = new stream.PassThrough();
var output = new stream.PassThrough();
var combined = new DuplexCombination(output,input)
output.write('two');
input.pipe(output);
  1. Create a lockname.pid file.
  2. Readdir, looking for lockname.* files:
  3. If found file pid is < our pid, and it is alive/not stale, FAIL and unlink our lockfile
  4. If we make it through, we have the lock, continue.
@iarna
iarna / index.js
Created October 29, 2014 09:40
progress tracker
"use strict"
var EventEmitter = require("events").EventEmitter
var util = require("util")
var Tracker = exports.Tracker = function (done,todo) {
this.workDone = done || 0
this.workTodo = todo || 0
}
util.extend(Tracker, EventEmitter)
function thingy(cb)
doAsyncThing("arg1", "arg2", "argn", andWarnOnError(log, cb))
}
function andWarnOnError (log, cb) {
return function (er, result) {
if (er) {
log.warn("install", "Couldn't install optional dependency:", er.message)
log.verbose("install", er.stack)
}
@iarna
iarna / gist:3de76d22abbe2aedf3ed
Created March 27, 2015 06:50
Perl style examples
LINE:
for (;;) {
statements;
last LINE if $foo;
next LINE if /^#/;
statements;
}
@iarna
iarna / example.js
Created May 15, 2015 06:43
Hoisting + CPS example
// This the code in the npm multi-stage branch that removes a package directory.
function removeDir (pkg, log, next) {
var modpath = path.join(path.dirname(pkg.path), '.' + path.basename(pkg.path) + '.MODULES')
// Preserve any modules installed in this package, if any
fs.rename(path.join(pkg.path, 'node_modules'), modpath, unbuildPackage)
function unbuildPackage (renameEr) {
npm.commands.unbuild(pkg.path, true, renameEr ? andRemoveEmptyParents(pkg.path) : moveModulesBack)

Keybase proof

I hereby claim:

  • I am iarna on github.
  • I am iarna (https://keybase.io/iarna) on keybase.
  • I have a public key whose fingerprint is 2FA0 EEAC C9D5 DB88 5B43 9AC6 D0F2 2C12 8CD6 A5B0

To claim this, I am signing this object:

@iarna
iarna / colors.md
Last active August 29, 2015 14:22
Syntax Highlighting

Optimize for readability:

  • Emphasize the important things.
  • Demphasize the noise.
  • Leave most of the content uncolored.

I emphasize:

  • literals – light cyan
  • constants - light green
  • immutable operators - bright white
  • mutating operators - yellow