for( auto& post : post_stream ) { …

  • Informal Latency Benchmark for Redis GET/SET using redis_cpp

    (From the half-baked-benchmarking-department in conjunction with the I-should-stash-the-results somewhere department…)

    Benchmarking is serious business on the internet — you don’t want to misrepresent anyone and their hard work on any given product.  That having been said, sometimes I just want ballpark values for a “lazy” estimate on what kind of numbers I’d get with a crude attempt at using a particular product.

    My use case was simple — I have a complex calculation that takes tens of milliseconds.  Calculating it on the fly when needed is too slow given the scale of work involved.  I wanted to precompute the values once and store them in place.  I was curious how low-effort I could get if I just stashed my computation results in redis and then fetched them on demand via a get using cpp_redis (available here https://github.com/Cylix/cpp_redis)  I used some crude code (very crude — I didn’t dig deep and just copied some sample code in cpp_redis) that looks something like this:

     

        auto keyspace = CreateSampleSet( client );                                                                                                                          
        for( auto& key : keyspace ) {                                                                                                                                       
            struct timeval tv1, tv2;                                                                                                                                        
                                                                                                                                                                            
            gettimeofday( &tv1, NULL );                                                                                                                                     
            auto getreply = client.get( key );                                                                                                                              
            client.commit();                                                                                                                                                
                                                                                                                                                                            
            getreply.wait();                                                                                                                                                
            gettimeofday( &tv2, NULL );                                                                                                                                     
                                                                                                                                                                            
            int64_t sample1 = (tv1.tv_sec * 1000000 + tv1.tv_usec);                                                                                                         
            int64_t sample2 = (tv2.tv_sec * 1000000 + tv2.tv_usec );                                                                                                        
            measurements.push_back( sample2 - sample1 );                                                                                                                    
        }

     

    I’ll let the numbers speak for themselves, but this is the histogram I got out of the experiment.  I used an i7-3930k on a moderately loaded system with 64GB of RAM (mostly available.)  The configuration was just over a local socket, same machine, default cpp_redis configuration using tacopie (accompanies cpp_redis), connecting to localhost over port 6379.

    I tossed out the outliers.  There were a few, but they were a tiny fraction of the total number of outcomes.  The above distribution seemed like “typical” performance on my system.  The distribution didn’t change too much from run to run.

     

    In my use case, I will probably stick with stashing values in postgres and load everything up front at once vs get/setting on individual values.  However, it’s nice to know the cost of being lazy, should the need arise.  👌

  • ObjectMutator.js: Generating All (or some subset) of Combinations an Object at the Property (or Key/Value pair) Level

    Greetings, internet.  I’m in the middle of a project moving some old Qt stuff to the browser (using Javascript and React.js.)  The project isn’t all that exciting, but sometimes even the most boring projects can turn into delightful programming games and yield insights on where to increase productivity.  Case in point:  how a language’s type system can make a horrible problem much nicer in terms of programmer-time involved.  Oh, and I’m releasing code — in Javascript!  (The impatient can just go right here for the code.)

    The specific problem I was required to solve was coming up with a flexible way to generate numerous combinations of a given object.  The reason I needed all these combinations was two-fold:  for genetic algorithms and for brute-force testing back-end API endpoints.  (For the purpose of discussion, I’m using problems encountered while programming in C++ as a backdrop.  If you don’t know C++, just assume the struct is sort of similar to a Javascript object.)

    Framing the Problem

    Consider the following problem:

    You have an object with 3 members.  You initialize it to some value.  Now, you want variations of that object such that you want all possible combinations (or a subset of combinations)  for some (or even all) of the 3 members.   How do you do this?

    In probability and statistics, this problem is just a straightforward application of “The Counting Rule.”  So maybe in C++, you’d have some object “Foo” like:

    struct Foo {
        int a;
        float b;
        bool c;
    };

    Suppose Foo’s ‘a’ can take ranges between 0 through 4.  And let’s also say ‘b’ can take a range between 0 and 1 in increments of 0.2, and ‘c’ can be true or false.  To get all combinations of Foo with the mentioned constraints on ‘a’, ‘b’, and ‘c’, you would just program a set of nested for-loops, iterate over the combinations, and then add Foo to some list-like structure and use the combinations.  But what happens if another programmer comes along and changes your code so that it’s now this:

    struct Foo {
        int a;
        float b;
        bool c;
        std::complex<float> d;
    };

    Now what?  Well, if you want the existing program to work but also want to support the ability to generate all combinations of Foo including ‘d’, you’d have to add another loop to generate the next level of combinations for the set of values ‘d’ can take.  If this structure (‘Foo’) changes often in your code at the hands of many developers, then you have a nuisance on your hands in terms of maintenance.  It would be nice to generalize this behavior for a general object.

    Being able to “look inside” an object and figure out its composition is referred to as reflection.  People who’ve been using C++ for years probably recognize that, since the language hasn’t historically had good support for reflection, it’s often been painful to implement code that deals with objects in a very general way.  More specifically, it hasn’t been easy to look at the contents of a given object, enumerate the properties, and generate general code based on knowledge of those properties.  People have been working around these problems for years (with things like meta-object compilers, interface description languages, protobufs for message encoding, etc.)  The truth is that reflection is just not that easy in C++.  (In fact, this thorough and insightful article by Jackie Kay details the problem quite nicely.  While I haven’t looked into specifics, it does seem as if there is hope for the future.)

    During my porting project, the not-so-easy-to-reflect element of C++ essentially “went away” when I moved my code to Javascript.  While I was using Qt, not all of the objects I was using were Qt objects.  In fact, they were generated by yet another tool that had no awareness of the meta-object system.  For my needs, re-coding functions whenever an object’s composition  changed was quickly turning into a serious maintenance headache.  It was one of those times where I was happier to be using Javascript than C++.  Performance not being necessary, Javascript was a much more natural choice.

    Easy Mode:  Enter Javascript

    Javascript’s objects are effectively dictionaries.  That’s it.  You have an object.  You can enumerate the keys of that object, and those keys essentially tell you the members of the object.  So, given the following:

    let lala = { a: 1, b: 2, c: 3 }

    You can figure out all the members simply by:

    Object.keys( lala ); // Gives [ 'a', 'b', 'c' ]

    Obviously, this is a lot easier than having some kind of meta-object compiler or framework and fishing around for information about an object.  What I needed was a generalized way to enumerate properties and generate combinations of an object’s member variables.

    Since my first use case was a form of genetic algorithm, I’m going to discuss my solution in biological terms:

    Given some object (chromosome) (… e.g, { a: 0, b: 1, c: 2 }) , I wanted to be able to generate all versions (mutations) of the object (chromosome) where I could specify a bunch of values for ‘a’, a bunch of values for ‘b’, and so on and so forth until all combinations were generated.  I also wanted the ability to leave some properties alone or untouched (unmutated.)  Finally, given that there could be relationships between properties and that some combinations (invalid mutations) were not permissible, I also wanted a cull function to loop through the result set and get rid of invalid combinations (chromosomes).

    So for example (a very contrived example), given a simple object:

    {a:0,b:0,c:12}

    Suppose I wanted ‘a’ from range [0,2) and ‘b’ from [5,7), I would want results like this:

    To accomplish this, I wrote ObjectMutator.js.  Using ObjectMutator.js, I simply had to do this:

    let chromosome = {
        a: 0,
        b: 0,
        c: 12,
    };
    
    let mutationGroup = [
        { gene: "a", start: 0, checkRange: (i) => (i < 2 ), step: (i) => i+1 },
        { gene: "b", start: 5, checkRange: (i) => ( i < 7 ), step: (i) => i+1 }
    ];
    
    let mutationBag = new Mutator( chromosome, mutationGroup );
    console.log( mutationBag.mutations );

    The output is then (from VS Code’s debug console):

    Array(4) [Object, Object, Object, Object]
    index.js:64
    length:4
    __proto__:Array(0) [, …]
    0:Object {a: 0, b: 5, c: 12}
    1:Object {a: 0, b: 6, c: 12}
    2:Object {a: 1, b: 5, c: 12}
    3:Object {a: 1, b: 6, c: 12}
    

     

    To keep it simple, I didn’t use the ability to filter unwanted mutations.  For example, you could remove all mutations where the member ‘a’ was greater than ‘b’ by passing in a culling function to be used in a final filter pass (Array’s filter()).

    The code for Mutator is below.  I had three parameters:  a chromosome, a mutation group defining the range of mutations, and a cull function for filtering out invalid chromosomes as a result of the mutations (not used above).  I  took advantage of how Javascript objects worked and wrote some code to generate my mutations — below.  (Note that I am not as deeply versed in the pros and cons of various javascript approaches, so you’re welcome to provide optimizations and feedback in the comments.  I’d appreciate it!)

    
    class Mutator {
        /**
         * @constructs Mutator
         * @description Constructs mutator object
         * @param {chromosome} The base object that gets mutated
         * @param {mutationGroup} The set of (joint) mutations to apply to a chromosome
         * @param {cullFunction} The function passed to a filter to remove any invalid/unwanted genes in the mutation set
         */
        constructor( chromosome, mutationGroup, cullFunction ) {
            this.mutations = [];     
            this.chromosome = chromosome;
    
            this.assertGenesToMutatePresent( mutationGroup );
            function generateMutations( chromosome, mutationGroup ) {
                function internalGenerateMutations( mutationGroup ) {
                    let mutationGroupClone = mutationGroup.slice(0);
                    let frameGene = mutationGroupClone.shift();
                    
                    let nextGeneration = [];
                    for( let i = frameGene.start; frameGene.checkRange(i); i = frameGene.step(i) ) {
                        if( mutationGroupClone.length > 0 ) {
                            let subMutations = internalGenerateMutations( mutationGroupClone );
                            nextGeneration.push.apply( nextGeneration, subMutations.map( (submutation ) => {
                                submutation[ frameGene.gene ] = i;
                                return submutation;
                            }));
                        } else {
                            let mutation = {};
                            mutation[ frameGene.gene ] = i;
                            nextGeneration.push( mutation );
                        }
                    }
                    return nextGeneration;
                }
        
                let baseMutations = internalGenerateMutations( mutationGroup );
                return (baseMutations.map( (value) => {
                    return {
                        ...chromosome,
                        ...value
                    };
                }));
            };
    
            this.mutations = (typeof( cullFunction ) === 'function') ?
                generateMutations( chromosome, mutationGroup ).filter( cullFunction ) :
                generateMutations( chromosome, mutationGroup );
        }
    
        /**
         * @function assertGenesToMutatePresent
         * @memberOf Mutator
         * 
         * @param {mutationGroup} Mutation group that needs to be checked
         */
        assertGenesToMutatePresent( mutationGroup ) {
            mutationGroup.forEach(element => {
                if( !this.chromosome.hasOwnProperty( element.gene ) )
                    throw Error( `Missing gene ${ element.gene }` );
            });
        }
    };

    Hopefully, you find some utility in this blog post.  If you find any bugs or have pull requests, let me know.

  • My First Experience with Server-Side Swift (using Perfect from perfect.org) – Part 2

     

    In Part 1, I talked about why I chose Swift on the backend and my general overall experience using Perfect on the backend.  In Part 2, I want to talk about my experience using Perfect-Mustache and Perfect-Redis.  The gist of this post:  I hit some minor issues using Perfect-Mustache, but I was able to work around issues with small syntactic changes.  With Perfect-Redis, however, I hit some suspicious data corruption and, instead, had to switch to using Kitura’s (from IBM) Redis driver.   (I replicated the bugs and submitted bug reports.  If I manage to get some time, I will look into the implementation myself.)

    For the record, despite encountering bugs, I am successfully using Perfect for one particular backend service in production and have not encountered any issues.  Life on the bleeding edge of technical endeavors sometimes involves, well, … bleeding.  Sometimes the bleeding happens on the user end, sometimes the bleeding happens on the vendor side.  C’est la vie.

    Perfect Mustache

    Perfect-Mustache is supposed to perform most of the functions provided by Mustache.js.  (Mustache.js is sort of a nice logic-less templating system.)  For my application, I didn’t intend to do much server-side rendering of content; I tried to keep any content rendering strictly on the client side (using React.js.)  Still, I wanted to dabble in some server side rendering to see what I could get away with.  The current implementation of Perfect Mustache doesn’t faithfully replicate all the features of Mustache.  For example, I tried to use the {{.}} syntax to render elements from an array of strings and this was not supported.  By creating objects instead of strings and providing a key to reference a given string, things worked fine.

    My opinion:  If you end up using Perfect Mustache, the best way to go about using it is to look at the samples provided by Perfect and try to accomplish what you are trying to do by re-using examples provided by Perfect’s developer.  Usually the developers had good reasons for restricting functionality, or they encountered some hiccup dealing with data in one platform (Mac OS) vs another (Linux.)  In most cases, with Perfect, you can accomplish most of what you want by not venturing too far away from example projects.

    Perfect Redis

    Perfect Redis did some small things correctly for me, but did not work properly for my use case.  When I attempted to submit JSON-encoded objects to Redis, I immediately encountered problems.  The strings were not being encoded properly for JSON.  I had to write a Swift extension to encode the values properly on the way in.  (See GitHub pull request:  https://github.com/PerfectlySoft/Perfect-Redis/pull/7 — the developers accepted this.)  Things seemed to work, at first.  When I increased the size of the content submitted to Redis, I started getting crashes and corrupted data.  I submitted a bug request, but stopped pursuing the issue when I realized I had to dig further into talking to Redis than I had time for.  I filed a bug report and moved on.

    I should add that there were some other gotchas I encountered while working with Perfect’s Redis drivers.  For example, calling something like a listPrepend() (i.e. LPUSH) function means that the callback can execute at any time in the future.  Subsequent code can execute before the results of a callback are known.  The documentation doesn’t hint that this node.js-style (asynchronous) of programming is the preferred approach of programming (although I suppose this becomes obvious to the programmer when things stop working properly.)  This situation could be a bit of a surprise to the unsuspecting.

    Fortunately, I was able to drop in IBM’s Kitura Redis driver and was immediately able to get a functional solution.  Being able to swap drivers like this, I think, speaks highly of the current Swift ecosystem (at least with regard to constructing tiny web services.)  If all of Perfect doesn’t work for you, mixing and matching components from other areas in the Swift ecosystem works as a short-term fix.

    You’re not always left stranded on an island of broken code in the Swift ecosystem.  🙂

    Do I still plan to keep using Perfect and Swift on the backend?

    In some cases.  In my case, where it was advantageous to take advantage of existing Swift code, I think Perfect managed to do the job nicely.  The framework’s code is easy to follow and it took very little time to get a minimalist API up and running.  (In fact, I have a production service in Swift running now with almost no downtime and have been incredibly happy with the results.)  In terms of runtime performance, Perfect has been more than satisfactory.

    In terms of a general “which framework is best” sort of question, I think the verdict is still out.  I expect the Swift (and Perfect) ecosystem to advance leaps and bounds over time. However, a lot has to be said for the sheer amount of documentation and examples prevalent in other frameworks that have already become more mainstream.  As an example, as much as I prefer Swift to Javascript, the node.js ecosystem has evolved into a rather pleasant place to be with the sheer number of high quality projects out there.  The Swift ecosystem doesn’t currently offer quite as much, at least outside of Apple-centric product development.  There’s always trade-offs, and server-side Swift doesn’t feel hands-down-no-doubts definitively compelling — although much promise exists for the future.

    On a final note, I’d definitely like to see Swift play a stronger role on the backend.  I would encourage developers to try their hands at tinkering with this ecosystem and contributing when possible.

     

  • React.js: What’s this.props.children in JSX all about?

    If you’re expecting the second installment of the post on Server side swift, don’t worry.  I’m still documenting some things!

    Lately I’ve been toying with the idea of converting an old UI written in Qt to make it work with React, and React is new to me.  In React, the user interface is defined in terms of JSX, where JSX is this markup language that eventually transforms a user interface definition (of sorts) into usable JavaScript.  There’s just one minor issue:  Transpiling often obscures what goes on under the hood.  Normally, I learn things fastest by reading other people’s code; however, with React, I find it more beneficial to read other people’s code AND read what the JSX is transpiled into.

    In my case, I was a little bit confused by what was happening under the hood with regards to JSX and “this.props.children”, at least from skimming documentation and trying to crank out something quickly (at the expense of being thorough.)

    More specifically, I was curious what got generated under the hood when a situation like the following was encountered:

    <SomeReactComponent>
        <SomeChildReactComponent/>
        <SomeChildReactComponent/>
    <SomeReactComponent />

    Normally, after transpiling (via Babel), each React component gets created via React.createElement().  If there were multiple child nodes (like in SomeReactComponent above, where there are two “SomeChildReactComponent” elements), how did react actually handle the element creation?  The documentation and several examples say to use {this.props.children}.  But what is actually happening here?

    I figured I’d construct an example and transform it via Babel to see what it looked like. I have an EncloserApp top level component. Inside the top level component, I created an Encloser. Encloser then “encloses” EnclosedElement. The objective was to just see what the transpiled output looks like:

    class Encloser extends React.Component {
        render() {
            const borderedStyle = {border: "1px solid red", padding: 6};
            return (
                <div style={borderedStyle}>
                {this.props.children}
                </div>
            )
        }
    }
    
    class EnclosedElement extends React.Component {
        static getPropTypes() {
            return {
                "custom_string_property" : React.PropTypes.string.isRequired
            }
        }
        render() {
            return (
                <h1>{this.props.custom_string_property}</h1>
            )
        }
    }
    
    class EncloserApp extends React.Component {
        render() {
            return (
                <Encloser>
                    <EnclosedElement custom_string_property="String One"/>
                    <EnclosedElement custom_string_property="String Two"/>
                </Encloser>
            )
        }
    }
    
    const contentNode = document.getElementById( 'contents' );
    ReactDOM.render( 
        (
            <div>
                <EncloserApp />
            </div>
        )
        , contentNode
    );

    In the transpiled code, as expected, the top level element is created as such:

    var contentNode = document.getElementById('contents');
    ReactDOM.render(React.createElement(
        "div",
        null,
        React.createElement(EncloserApp, null)
    ), contentNode);

    The createElement() call creates the div, as expected, passes along no properties (null), and then creates a singular child node for the EncloserApp.  Now, inspecting EncloserApp, the code (around the render() method) looks like this:

     

    _createClass(EncloserApp, [{
            key: "render",
            value: function render() {
                return React.createElement(
                    Encloser,
                    null,
                    React.createElement(EnclosedElement, { custom_string_property: "String One" }),
                    React.createElement(EnclosedElement, { custom_string_property: "String Two" })
                );
            }
        }]);

    So, essentially, {this.props.children} is transformed into a list of child nodes that are passed as parameters to createElement (which accepts a variable number of arguments.  “this.props.children” exists in JSX specifically for JSX markup that exists as an opening and closing tag (at least according to the documentation.)

    I know this is not an earth shattering blog post, but it helped me to see the nature of the transformation underneath.  The system makes more sense seeing the transformation of JSX into the graph containing React component nodes.

    Ah, clarity.

  • My First Experience with Server-Side Swift (using Perfect from perfect.org) – Part 1

     

    Motivations

    Most of the work I take on involves languages like C++, Swift, or Objective-C and is not typically web based.  While I’m normally nestled comfortably in the embrace of C++ template meta-programming or the warm walls of an operating system kernel, even I can sense the uncivilized barbarians at the gate (Javascript) and the increasing demand that software work more closely with web based APIs and various web stacks (Django, Rails, node.js.)

    Recently, I had the interesting encounter where I was given the chance to explore the use of server-side Swift.  The client was attempting to ferry computational requests from a number of web and iOS-based clients to a very large, computational backend written in C++ (complete with its own ORM)  that was distributed across a number of nodes.  Since one of the core libraries used happened to be written in Swift, there was some hope we could simply use code on the server side without much effort.  Rather than compile a separate application and marshal requests to the Swift library through yet another framework, the decision was made to just service requests outright from a web service authored in Swift.

    Instead of authoring web services from scratch, I looked for a ready-made solution to simply embrace and extend.  There were a few different frameworks to choose from, but I eventually settled on Perfect (from http://www.perfect.org).  The decision to use a server-side swift framework, given other mature technologies, was not without internal friction.  Criticism was leveled that I was using untested “hipster” technology.  In fact, some of my colleagues insisted I go so far as to don a beret, head to the local hipster coffee shop, and offer poorly authored poetry to the public about being oppressed by self-authored technical debt!  Despite critics’ concerns, I want to highlight that I had some success with Perfect!  (Also, not enough technical debt was accumulated to warrant lobbing harangues-inappropriately-called-poetry at the public.)  Of course, the effort was not completely glitch free — and that is part of what I will document here.

    Why Perfect?

    Why Perfect?  My choice was based on a mix of inputs:  GitHub commits, looking into public issue trackers, various blogs, popularity, and out of the box features.  I watched a video on YouTube from the CEO of the organization responsible for Perfect and thought his developers’ take on things aligned with my own views.  Moreover,  in my particular environment, I needed connectors for Redis and Postgres.  The support for web sockets and the basic documentation gave me enough confidence in the product to give it a shot.  If Perfect failed me, I figured, it would fail me quickly and in an obvious enough fashion that I could bail without disastrous results.

    For our internal use case, some “jankiness” in the tech stack was tolerable provided we got results often enough between failures.  I was willing to fix bugs, provided they weren’t too deep rooted.  Basically, Perfect really needed to be “good enough” — and for our use case, it was.

    The Use Case

    Our use case:  Accept work requests from web, desktop, or mobile clients, perform some transformations and filtering on those requests, service those requests, or (in the case of computational expensive requests) dispatch those requests to a work queue (Redis).  Binaries written in C++ would then consume elements from Redis and place their results back in a database (Postgres) or, in some cases, Redis.  As the computational backend progressed on its work requests, updates were provided via WebSockets (also via Perfect) to web based clients.

    Well, did you succeed?

    I did!  In fact, the general architecture of Perfect facilitated our needs quite well.  I would say the initial prototyping and deployment of a basic service went very smoothly.  The transition from Swift 3 to Swift 4 even went smoothly — the developers responsible for Perfect handled this transition in a very timely fashion. There was no extended period in which I had clients on one version of Swift with the server on another version of Swift.

    Routes, URL Variables, Responses

    In Perfect, it is very easy to create a server, add routes, and service requests.  No particularly advanced knowledge of Swift is really required to accomplish this.  Given my experience with other frameworks, I did not encounter too much difficulty just getting up and running.  The basic process is to set up a route, pass a function parameter to handle the route, and reply using response objects.  Most of the basic web primitives for handling URL variables, posted parameters, and managing responses are provided.

    Implementing an API is simple.  Return the right response type, format your response with the proper encoding, and write the response.  I hit no major issues or bugs in performing these basic tasks.

    Encouraging!

    Using Swift on the Server Side in Linux

    Swift on the server-side does have some gotchas — this isn’t Perfect’s fault, but it affected me in that, if I developed code in Xcode and then recompiled and deployed on Linux, some small snippets of code would not compile.

    In addition, the Foundation libraries, while functionally complete, have some corner cases where things can get awkward.  One (cosmetic) thing I found somewhat unusual was the use of NSRegularExpression.  OS X and iOS developers will normally see “NS” namespace objects, but this felt slightly awkward on Linux.  Moreover, NSRegularExpression (and affiliated functions) is less pleasant to use than regex facilities in other languages.  (Obviously, this is an opinion; but I suspect many readers who’ve used other frameworks would agree with me.)

    During the transition between Swift 3 and 4, I hit some snags with certain function signatures changing.  In some cases, my code was littered with preprocessor defines like #if os(OS X) to tweak behavior on different platforms.  I didn’t love this, but it wasn’t too difficult to work around and was only a small drawback.

    Another issue I encountered was doing semi- low-level tasks — I ended up needing third party libraries to do things like atomically incrementing integers.   I found this somewhat distasteful, but not unreasonable given the nature of how Swift has been evolving and changing over the past few years.  Some primitives available on OS X were not available on Linux and suggestions on Stack Overflow seemed clunky.  I ended up using a third-party library that made effective use of features provided by Clang.  Performing some systems-software tasks in Swift still feels awkward compared to using C or C++.  This could be my own experience, but it certainly felt awkward and I’d caution others who have these same scenarios.

    Interfacing to Postgres

    I did not use the ORM (stORM) provided with Perfect.  I did, however, issue SQL requests via their Postgres package.  I think, in many languages and frameworks, authoring SQL requests is a bit of a pain.  It wasn’t any different in Swift while using Perfect.  I’m not sure there’s a way around painfully constructing strings representing complex queries, but there’s no immediately perceptible or dramatically noticeable edge in Swift for performing this task.

    One issue that did cause me to raise an eyebrow was a bug filed in the Perfect issue tracker regarding memory leaks on tables returned from the Perfect driver built on top of libpq.  My use case did not stress the Postgres driver enough for me to experience anything catastrophic with regard to using Postgres.  At the time of writing for this blog post, this issue has not been closed.  This could be of concern for long running services issuing a very large number of requests.  Hopefully the developers will address this.

    What’s Coming in Part 2

    In part 2, I’ll detail a few issues I hit in Perfect-Mustache, issues I encountered in the use of Perfect-Redis, and other architectural gotchas that I encountered.  The issues I touch upon in Part 2 have more substance.  Stay tuned!

     

  • Beat Detection: A Raspberry Pi Hacking of Hallmark’s “Happy Tappers”

    In graduate school, time series analysis was the topic I liked the most. The classic models (like AR(p), MA(q), ARMA(p,q), etc.) are used usually after time series have been sampled in some fashion. Of course, there’s more than one way to look at a time series and many of those perspectives come from the field of digital signal processing. Unfortunately, a lot of the text books on DSP are dry. One can spend hours reading a DSP book, understand the math, and still not really appreciate the material. I happen to think the appreciation for the topic comes more from trying to solve real-world problems. As problems are encountered, the motivation arises to go back and attack the mathematics in a meaningful sense. One of the more interesting problems, in my opinion, is real-time beat detection in music.  Therefore,  I decided to do some experimentation with beat detection.

    After Christmas, I went through pharmacy after-Christmas sales and picked up a bunch of cheap Hallmark ornaments. When I discovered the ornaments had a port to interface to each other, I decided I’d look at the interfacing method and devise my own schemes for controlling them. After figuring out the communications protocol between ornaments, I experimented by building control systems using FPGAs, Arduinos, and the Raspberry Pi.  I ultimately settled on the Raspberry Pi.

    With the Raspberry Pi, I was able to perform FFTs fast enough on the music samples to make a crude attempt at detecting beats while playing mp3 files. The project is still something I tinker with from time to time. I find entertainment in looking at various approaches for beat detection in research papers and documents on the internet.  Below is a sample of what I have been able to create so far, which is a hack of Hallmark’s Happy Tappers, set to a clip Capital Cities’ “Safe and Sound”:

    As precious free time becomes available, I’d like to improve the system to work with onset detection and explore various filtering problems.

    Finally, I’d like to thank the Dishoom Labs crew (MB) for letting me borrow some equipment during the project.

    (Edit:  Removed Flickr Video and replaced with YouTube.)

  • Understanding How Bitcoin Works

    Long time, no update. 🙂 It’s difficult to produce quality content, which is why I was very impressed with the person who made this particular video:

     

     

    Understanding how Bitcoin works can be difficult, but this particular video is exceptional in breaking the concepts down and connecting them together. I consider it another “must-see” YouTube video, if you have any interest at all in cryptocurrency.

  • Using Matplotlib to get XKCD-style Plots

    This past week, I found another developer blog where a post caught my attention: Pythonic Perambulations.  More specifically, it was this post regarding presenting data in an “xkcd” style.  I figured I’d give this blog post some inbound link props because I found the content both useful and amusing.  The author seems to have gotten quite a bit of well-deserved praise already in his blog’s comments section.

    Readers of xkcd are often shown very interesting plots illustrating a humorous point or interesting concept.  In any serious data analysis project, often there are many iterations of sifting and sorting through data until a some hypothesis is rejected or accepted.  Rather than put a disclaimer to warn against drawing serious conclusions from a rough-draft grade presentation, I may just start xkcdifying things.  If nothing else, xkcd-ifying plots may take the sting out of any data-driven negative implications.  🙂

     

  • C++ Reading List

    In the past, I’ve paid for various libraries (such as Anthony Williams’ excellent  just::thread library ) and C++11 documents to make an attempt to get ahead of the curve.  Now, however, C++11 information is becoming more easily accessible.  There is quite a bit of new content out there that is making things more digestible for programmers who aren’t so involved as to be following the evolution of C++ standard.

    I think this particular Dr. Dobbs article/link is technical gold for people navigating the new C++11 standard and its feature set: C++ Reading List

    A key item to note is that Bjarne Stroustrup’s book, “The C++ Programming Language”, has had its 4th edition released recently.  (It is also in the list.)

     

  • C++11 Idioms Slideshow from Silicon Valley Code Camp

    I stumbled across this very nice presentation (see below) by Sumant Tambe on C++11 idioms on Google+ and figured I’d stash a link here.

    I try to integrate as much C++11 as I can into my projects where I think there would be benefit. However, without reading the C++ programming community’s input on various programming techniques, it’s difficult to know the drawbacks of using a particular method until it’s too late (and costly). Whenever new idioms like this are presented in a clean format like in Sumant’s slideshow, I’m always eager to read about the general consensus/feedback/comments of others.  (Incidentally, Sumant’s blog is a gold mine of interesting C++ techniques; many [enjoyable] hours can be lost there.)

    Have a look for yourself: