Skip to content

Instantly share code, notes, and snippets.

@fennb
fennb / gist:1124285
Created August 4, 2011 01:05
Bash string manipulation example
#!/bin/bash
MYSTRING="hello there"
STRLEN=${#MYSTRING}
let STARTPOS=$[STRLEN-5]
echo "First 5: ${MYSTRING:0:5}"
echo "Last 5: ${MYSTRING:$STARTPOS:5}"
@fennb
fennb / gist:1124578
Created August 4, 2011 05:52
Synchronous cache failover example
// synchronous version
myThing = synchronousCache.get("id:3244");
if (myThing == null) {
myThing = synchronousDB.query("SELECT * from something WHERE id = 3244");
}
// Do various stuff with myThing here
@fennb
fennb / gist:1124579
Created August 4, 2011 05:53
Asynchronous cache failover example
// asynchronous version
asynchronousCache.get("id:3244", function(err, myThing) {
if (myThing == null) {
asynchronousDB.query("SELECT * from something WHERE id = 3244", function(err, myThing) {
// We now have a thing from DB, do something with result
// ...
});
} else {
// We have a thing from cache, do something with result
@fennb
fennb / gist:1124580
Created August 4, 2011 05:54
Asynchronous loop issues illustrative example
// Illustrative case
for (var i = 0; i < recentBlogPostIds.length; i++) {
var blogPostId = recentBlogPostIds[i];
var results = [];
// Fetch from DB
asynchronousDB.getBlogPostById(blogPostId, function(err, post) {
htmlFragment = templating.render(post);
results.push(htmlFragment);
});
}
#!/bin/bash
# FLAC encoded example
curl \
--data-binary @example.flac \
--header 'Content-type: audio/x-flac; rate=16000' \
'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&pfilter=2&lang=en-US&maxresults=6'
# Speex encoded example
curl \
@fennb
fennb / gist:1139016
Created August 11, 2011 06:25
Chromium speech recognition details
const int SpeechRecognizer::kAudioSampleRate = 16000;
const int SpeechRecognizer::kAudioPacketIntervalMs = 100;
const ChannelLayout SpeechRecognizer::kChannelLayout = CHANNEL_LAYOUT_MONO;
const int SpeechRecognizer::kNumBitsPerAudioSample = 16;
const int SpeechRecognizer::kNoSpeechTimeoutSec = 8;
const int SpeechRecognizer::kEndpointerEstimationTimeMs = 300;
// ...
const char* const kContentTypeSpeex = "audio/x-speex-with-header-byte; rate=";
const int kSpeexEncodingQuality = 8;
const int kMaxSpeexFrameLength = 110; // (44kbps rate sampled at 32kHz).
@fennb
fennb / gist:1139053
Created August 11, 2011 07:00
Sample speech recognition output
{
"hypotheses": [
{
"confidence": 0.88569070000000005,
"utterance": "this is pretty cool"
},
{
"utterance": "thesis pretty cool"
},
{
@fennb
fennb / gist:1283999
Created October 13, 2011 11:20
Benchmark, wordpress + apache2 + php-fpm + apc / EC2 small
$ ab -n 200 -c 4 http://wp-demo.local:8080/
Server Software: Apache/2.2.17
Server Hostname: wp-demo.local
Server Port: 8080
Document Path: /
Document Length: 5726 bytes
Concurrency Level: 4
@fennb
fennb / gist:1284002
Created October 13, 2011 11:22
nginx microcaching, concurrency 500
$ ab -n 10000 -c 500 http://wp-demo.local/
Server Software: nginx/0.8.54
Server Hostname: wp-demo.local
Server Port: 80
Document Path: /
Document Length: 5728 bytes
Concurrency Level: 500
@fennb
fennb / gist:1124535
Created August 4, 2011 05:04
node.js proxy server in 20 lines of JS (via http://www.catonmat.net/http-proxy-in-nodejs/)
var http = require('http');
http.createServer(function(request, response) {
var proxy = http.createClient(80, request.headers['host'])
var proxy_request = proxy.request(request.method, request.url, request.headers);
proxy_request.addListener('response', function (proxy_response) {
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {