Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@karlseguin
karlseguin / stat.go
Last active August 29, 2015 13:56
Track data points with constant-space via sampling (can be used to calculate percentiles, for example)
const (
MAX_SAMPLE_COUNT = 50
MAX_SAMPLE_COUNT_F = float64(MAX_SAMPLE_COUNT)
)
type Stat struct {
sync.Mutex
hits int64
samples []int
}
@karlseguin
karlseguin / gist:9686555
Created March 21, 2014 13:43
A wrapper about lib/pq that auto-prepares queries and caches them.
type PgAutoPrepare struct {
driver.Conn
cache map[string]driver.Stmt
}
func (pap *PgAutoPrepare) Open(name string) (driver.Conn, error) {
conn, err := pq.Open(name)
if err != nil {
return nil, err
}
package main
// @see https://twitter.com/karlseguin/status/524452778093977600
import (
"math/rand"
"strconv"
"testing"
)
const (
@karlseguin
karlseguin / gist:0f38b9ad87ebd54375da
Last active August 29, 2015 14:13
UnixBench run on a AWS EC2 c4.8xlarge
BYTE UNIX Benchmarks (Version 5.1.3)
System: ip-172-30-0-254: GNU/Linux
OS: GNU/Linux -- 3.13.0-36-generic -- #63-Ubuntu SMP Wed Sep 3 21:30:07 UTC 2014
Machine: x86_64 (x86_64)
Language: en_US.utf8 (charmap="ANSI_X3.4-1968", collate="ANSI_X3.4-1968")
CPU 0: Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz (5786.7 bogomips)
Hyper-Threading, x86-64, MMX, Physical Address Ext, SYSENTER/SYSEXIT, SYSCALL/SYSRET
CPU 1: Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz (5786.7 bogomips)
Hyper-Threading, x86-64, MMX, Physical Address Ext, SYSENTER/SYSEXIT, SYSCALL/SYSRET
@karlseguin
karlseguin / gist:5a6a45ace2048545b6c3
Last active August 29, 2015 14:13
UnixBench run on a dedicated i7-4770
BYTE UNIX Benchmarks (Version 5.1.3)
System: p2.kapi.io: GNU/Linux
OS: GNU/Linux -- 3.13.0-36-generic -- #63-Ubuntu SMP Wed Sep 3 21:30:07 UTC 2014
Machine: x86_64 (x86_64)
Language: en_US.utf8 (charmap="ANSI_X3.4-1968", collate="ANSI_X3.4-1968")
CPU 0: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz (6799.8 bogomips)
Hyper-Threading, x86-64, MMX, Physical Address Ext, SYSENTER/SYSEXIT, SYSCALL/SYSRET, Intel virtualization
CPU 1: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz (6799.8 bogomips)
Hyper-Threading, x86-64, MMX, Physical Address Ext, SYSENTER/SYSEXIT, SYSCALL/SYSRET, Intel virtualization
@karlseguin
karlseguin / gist:a6b441269d82690381e5
Created January 16, 2015 12:37
specialized int set
/*
Playing with building a specialized set for integers. Specifically:
- Integers are natually randomly distributed (auto-incrementing ids for millions of records would do)
- The number of items is known upfront (can be approximate)
- The set sizes changes infrequently (we're mostly interested in membership tests)
Accomodating constraints, to be sure, but if we can meet the first two requirements,
we'll end up with a little less than 1/2 the memory of a map[int]struct{}, and membership
tests taking about ~1/2 the time.
@karlseguin
karlseguin / init.coffee
Created February 13, 2015 03:54
Atom editor multiple wrap guides
# place this in your init.coffee
atom.workspace.observeTextEditors (e) ->
editor = atom.views.getView(e)
clone = editor.querySelector('.wrap-guide').cloneNode()
clone.style.left = (editor.getDefaultCharacterWidth() * 120) + "px"
editor.querySelector('.underlayer').appendChild(clone)
@karlseguin
karlseguin / gist:0ba24030fb12b10b686b
Created March 4, 2015 01:52
bolt vs redis (appendonly on) vs postgresql
/*
bolt 5000 277963 ns/op
redis 30000 48081 ns/op
pg 10000 149691 ns/op
Yes, the Bolt transactions could be batched. But so too could the PG transactions,
and the Redis work could be pipelined. And that isn't always a workable solution.
*/
import (
loadArticle (res, req) ->
article = yield findArticlebyId(...)
return res.notFound() unless artilce?
res.ok(article)
findArticleById (id) ->
conn = yield conn.Open().
row = yield conn.QueryAsync("....", id)
new Article(row)
Bootstrap.DefineConnection(c => c.ConnectTo("192.168.10.151", 9160)
.KeySpaceIs("ApolloSandbox")
.WithName("Primary")
.ReadConsistency(ReadConsistencyLevel.One)
.WriteConsistency(WriteConsistencyLevel.One));
using (var c = new Client("Primary"))
{
c.Insert(new ColumnPath { Family = "ColumnFamily", Column = "Column" }, "Key", "Value");