Skip to content

Instantly share code, notes, and snippets.

View kevinkreiser's full-sized avatar

Kevin Kreiser kevinkreiser

  • osm.org/node/158543009
  • 18:13 (UTC -04:00)
View GitHub Profile
@kevinkreiser
kevinkreiser / testing.hpp
Last active August 29, 2015 14:14
Unit testing framework for c++
#ifndef __TESTING_HPP__
#define __TESTING_HPP__
/*
* Test this with something like:
* g++ -std=c++11 -x c++ -DTEST_TESTING testing.hpp -o testing_test
* ./testing_test
*/
@kevinkreiser
kevinkreiser / memory_status.cpp
Last active August 29, 2015 14:15
Programmatically Get at Memory Usage via Proc Status
#include <fstream>
#include <string>
#include <iostream>
#include <cinttypes>
#include <utility>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <list>
@kevinkreiser
kevinkreiser / kyotocabinet_hash_map_test.cpp
Last active August 29, 2015 14:15
Kyotocabinet File Based Hash Map Tests for Reading and Writing
/*
sudo apt-get install libkyotocabinet-dev
g++ kyotocabinet_hash_map_test.cpp -o kyotocabinet_hash_map_test -std=c++11 -O2 -lkyotocabinet
./kyotocabinet_hash_map_test 500000
./kyotocabinet_hash_map_test 500000 threaded_read
*/
#include <fstream>
#include <string>
#include <iostream>
@kevinkreiser
kevinkreiser / memc.cpp
Created March 20, 2015 16:03
Memcached Benchmark which Caches a Directory and Reads it Back
/*
* sudo apt-get install memcached libmemcached-dev libboost-all-dev
* have a look in /etc/memcached.conf and set some reasonable defaults
* changed the following:
* -m 4096m -I 10m
*
* sudo service memcached restart
*
* g++ memc.cpp -o memc -std=c++11 -g -O2 -lboost_system -lboost_filesystem -lmemcached
* memc /some/directory/with/files 2147483648
@kevinkreiser
kevinkreiser / blob_cache.cpp
Last active August 29, 2015 14:17
A Data Blob Cache with Configurable Eviction Policies
/*
* sudo apt-get install libboost-all-dev
* #if you want to mmap more files than what `sysctl vm.max_map_count` says
* sudo sysctl -w vm.max_map_count=some_larger_number
* g++ blob_cache.cpp -o blob_cache -std=c++11 -g -O2 -lboost_system -lboost_filesystem
* blob_cache /some/directory/with/files 1073741824
* blob_cache /some/directory/with/files 1073741824 mm
*/
#include <boost/filesystem.hpp>
@kevinkreiser
kevinkreiser / zmq_server.py
Last active August 29, 2015 14:18
Blocking zmq zeromq Ømq http Request Echo Server
from BaseHTTPServer import BaseHTTPRequestHandler
from StringIO import StringIO
from cgi import urlparse
import json
import zmq
class HTTPRequest(BaseHTTPRequestHandler):
def __init__(self, request_text):
self.rfile = StringIO(request_text)
self.raw_requestline = self.rfile.readline()
@kevinkreiser
kevinkreiser / template_function_specialization.cpp
Created September 10, 2015 21:02
When only the return type differs an overload just isn't enough!
#include <iostream>
#include <list>
#include <vector>
//the guts that both specializations can use
template <class container_t>
void fill(container_t& c, int start, int end) {
for(;start <= end; ++start)
c.push_back(start);
}
@kevinkreiser
kevinkreiser / iterable.cpp
Last active October 13, 2015 20:17
Make primative array iterable
#include <string>
#include <iostream>
template <class T>
struct iterable_t {
public:
using iterator = T*;
iterable_t(T* first, size_t size): head(first), tail(first + size), count(size){}
iterable_t(T* first, T* end): head(first), tail(end), count(end - first){}
T* begin() { return head; }
#include <iostream>
#include <cinttypes>
#include <limits>
//combining a union with an anonymous bit field we can:
// get a summary value for the entire bit field
// reference bit field members directly from the union
// use an initializer list to set them
//TODO: how to initialize via the bit field members without un-anonymizing it
union bit_funion_t {
@kevinkreiser
kevinkreiser / code_timeline.sh
Last active November 11, 2015 21:16
CSV Generator for Graphing Bytes of Code Since a Date in the Past
#!/bin/bash
#run this like: DAYS=365 ./code_timeline.sh https://github.com/org/repo1.git https://github.com/org/repo2.git
#note that the types of files it checks for code are hardcoded below
code='\.cc$|\.h$|\.cpp$|\.hpp$|\.lua$|\.py$|\.js'
: ${DAYS:?Please set DAYS environment variable to a number of days in the past youd like to capture}
#setup each repo
echo -n "day,"
for url in ${@}; do