<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>Node - Blogs at Near Infinity</title>


        <link>http://www.nearinfinity.com/blogs/</link>
        <description>Employee Blogs</description>
        <language>en</language>
        <copyright>Copyright 2012</copyright>
        <lastBuildDate>Mon, 30 Apr 2012 21:58:16 -0500</lastBuildDate>
        <generator>http://www.sixapart.com/movabletype/</generator>
        <docs>http://www.rssboard.org/rss-specification</docs>
        
        <item>
            <title>ZooKeeper and HBase presentation slides</title>
            <description><![CDATA[<p>Last Friday (April 27, 2012) I gave a presentation on <a href="http://zookeeper.apache.org/">Apache ZooKeeper</a> and a lightning talk on <a href="http://hbase.apache.org/">Apache HBase</a> at the <a href="http://www.nearinfinity.com">Near Infinity</a> spring conference -- we have a fall and a spring conference each year with presentations on a wide variety of different subjects.</p>

<p>This past Friday the presentations included <a href="http://mahout.apache.org/">Apache Mahout</a>; <a href="(http://zookeeper.apache.org/">ZooKeeper</a>; <a href="http://couchdb.apache.org/">CouchDB</a> apps with <a href="http://www.sencha.com/products/extjs/">ExtJS</a>; Expressive Code; and a lively discussion on development processes (e.g. Scrum, Kanban, XP, customizations to these, other processes, etc.).</p>

<p>Then we always have a bunch of lightning talks - this time included talks on <a href="http://hbase.apache.org">HBase</a>, <a href="https://github.com/robbyrussell/oh-my-zsh">oh-my-zsh</a>, <a href="http://www.opscode.com/chef/">Chef</a>, <a href="http://overtone.github.com/">Overtone</a>, <a href="http://twitter.github.com/bootstrap/">Twitter Bootstrap</a>, <a href="https://github.com/nearinfinity/node-java">node-java</a>, and <a href="http://en.wikipedia.org/wiki/Responsive_Web_Design">Responsive Design</a>. So, a pretty wide variety of current and emerging technologies.</p>

<p>Anyway, I published my talks on slideshare at <a href="http://www.slideshare.net/scottleber/apache-zookeeper">ZooKeeper</a> and <a href="http://www.slideshare.net/scottleber/hbase-lightningtalk">HBase</a>. I also posted the sample code for these presentations on GitHub: <a href="https://github.com/sleberknight/zookeeper-samples">ZooKeeper sample code</a> and <a href="https://github.com/sleberknight/basic-hbase-examples">HBase sample code</a>.</p>
]]></description>
            <link>http://www.nearinfinity.com/blogs/scott_leberknight/zookeeper_and_hbase_presentati.html</link>
            <guid>http://www.nearinfinity.com/blogs/scott_leberknight/zookeeper_and_hbase_presentati.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Agile Development</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">CSS</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Database</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">General</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">JavaScript</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Node</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Persistence</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Web Development</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">Chef</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">CouchDB</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">ExtJS</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">HBase</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Kanban</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Mahout</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Node.js</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Overtone</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Responsive Design</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Scrum</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">Twitter Bootstrap</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">ZooKeeper</category>
            
            <pubDate>Mon, 30 Apr 2012 21:58:16 -0500</pubDate>
        </item>
        
        <item>
            <title>Node.js Basics Explained</title>
            <description><![CDATA[<p><span style="float: left; padding-right: 1em">
<img src="/blogs/assets/jkunkle/nodejs-dark.png" alt="Node.js Explained" title="Node.js Logo" />
</span></p>

<p>Rising from non-existence three short years ago, Node.js is already attracting the accolades and disdain enjoyed and endured by the Ruby and Rails community just a short time ago. It overtook Rails as the most popular Github repository last November (now superseded by Twitter's Bootstrap project) and was selected by InfoWorld for the Technology of the Year Award in 2012.</p>

<p>If you've never used Node.js before or have limited experience, it may not be obvious why people are so excited about it. This post will attempt to explain the basic theory central to Node's approach, arming you with a better frame of reference for the debates you'll undoubtedly encounter.</p>

<p>Please note that this post won't make much sense without a very basic familiarity with Node. Consider checking out the <a href="http://nodejs.org">Node.js website</a> and reading a few articles on Node first if that's the case.</p>

<h1>I/O Latency</h1>

<p>The core premise behind Node's approach is that I/O operations are slow compared to the computation done in your application. Consider the table below, taken from Ryan Dahl's 2008.11.08 presentation on Node.js. It shows various I/O operations on the left and the number of CPU cycles it takes to perform them. Don't focus on the actual numbers, but on the differences in magnitude for the  operations.</p>

<table border="1" cellspacing="0" cellpadding="10">
    <tr><th>Operation</th><th>CPU cycles</th></tr>
    <tr><td>L1</td><td align="right">3</td></tr>
    <tr><td>L2</td><td align="right">14</td></tr>
    <tr><td>RAM</td><td align="right">250</td></tr>
    <tr><td>Disk</td><td align="right">41,000,000</td></tr>
    <tr><td>Network</td><td align="right">240,000,000</td></tr>
</table>

<p>From the table you can see that disk and network access times dwarf things like memory access or L1 and L2 cache access. The chart below makes the magnitude differences even more obvious. The L1, L2, and RAM access times are so much smaller than disk and network access that their bars don't even appear on the graph.</p>

<p><img src="/blogs/assets/jkunkle/latency-bar-graph.png" alt="i/o magnitudes" title="I/O Latency" /></p>

<h1>Waiting</h1>

<p>If you can buy that I/O operations are often orders of magnitude slower than the computation you're performing in your app, then what is your app doing during the I/O operations? It's waiting! It's execution is literally blocked until the I/O operation completes.</p>

<p>Consider the following fictitious web request. The slim green bars represent the time your application devotes to processing and the gray bars represent the time spent waiting for I/O to complete. The example starts off with some logic to process the request. </p>

<ol>
<li>Parse the request and invoke the appropriate controller logic</li>
<li>Initiate a database query or maybe a request to an external service</li>
<li>Wait for I/O</li>
<li>Process the query results and write some data to a log file</li>
<li>Wait for I/O</li>
<li>Perform some final formatting of the results and return them to the client</li>
</ol>

<p><img src="/blogs/assets/jkunkle/waiting.png" alt="waiting" title="Waiting" /></p>

<p>The vast majority of time spent during the request involved waiting for the I/O operations, and there were only two. Very little time is actually used to perform application-specific processing. Node.js was designed from the start to exploit this imbalance.</p>

<h1>Scaling</h1>

<p>Before we get into the approach Node.js takes to scaling, let's consider how our made-up web request would be scaled with other models.</p>

<h2>Scaling with Threads</h2>

<p>Using a thread based model, you'd scale the above example by creating multiple threads, one for each concurrent connection. The diagram below depicts a threaded model capable of handling four concurrent connections.</p>

<p><img src="/blogs/assets/jkunkle/scaling-threads.png" alt="scaling with threads" title="Scaling with Threads" /></p>

<p>While this approach allows us to scale by adding more threads, each thread still spends most of its time waiting for I/O, not processing your application logic. Unfortunately, continuing to add threads introduces context switching overhead and uses considerable memory to maintain execution stacks.</p>

<h2>Scaling with Processes</h2>

<p>Another popular approach to scaling your application is to run multiple processes. As you can see from the diagram below, the theory behind scaling with multiple processes is basically the same as scaling with threads, although it does use more memory. Like the threading model, each process still spends most of its time waiting on I/O.</p>

<p><img src="/blogs/assets/jkunkle/scaling-processes.png" alt="scaling with processes" title="Scaling with Processes" /></p>

<h2>Scaling with Node.js</h2>

<p>Since the code you write for Node.js executes in a single thread within a single process, it takes a different approach to scaling. It extracts the "I/O waiting" by using an internal thread pool or leveraging asynchronous I/O APIs of the host operating system to free your thread for processing other connections. Instead of your code blocking on I/O operations, it's freed to process other connections. When the I/O operation completes, your code is called back to handle the results.</p>

<p><img src="/blogs/assets/jkunkle/scaling-event-loop.png" alt="scaling with event loop" title="Scaling with Node.js" /></p>

<p>With Node.js your code never blocks for I/O operations, eliminating those long gray bars of waiting time. This non-blocking mode of operation is what allows Node.js to handle large numbers of concurrent connections without overly-straining system resources.</p>

<h1>Conclusion</h1>

<p>The success of Node.js relies on the premise that time spent waiting for I/O far outweighs the time spent executing application logic. While that's true of many of todays' web/network-based applications, it doesn't always apply. If your application is more CPU-intensive with minimal I/O by comparison, then Node.js is probably not the right platform. As my father used to say, don't try to fit a square peg in a round hole.</p>
]]></description>
            <link>http://www.nearinfinity.com/blogs/jeff_kunkle/nodejs_basics_explained.html</link>
            <guid>http://www.nearinfinity.com/blogs/jeff_kunkle/nodejs_basics_explained.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">JavaScript</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Node</category>
            
            
            <pubDate>Fri, 13 Apr 2012 10:02:42 -0500</pubDate>
        </item>
        
        <item>
            <title>Automatic Code Coverage Switching for Vows.js</title>
            <description><![CDATA[<p>If you've used <a href="http://nodejs.org">Node.js</a> lately you've likely run across <a href="http://vowsjs.org/">Vows</a>, the asynchronous behavior driven development test framework for Node. Vows does a really nice job of forcing you to separate your setup logic from your assertions, but that's not what this post is about.</p>

<p>Vows can generate test coverage reports with its --cover-plain, --cover-html, and --cover-json options. Unfortunately, creating code coverage metrics isn't as straightforward as simply including these options in your call to vows. You need to</p>

<ol>
<li>Pre-instrument your code using <a href="https://github.com/visionmedia/node-jscoverage">node-jscoverage</a>.</li>
<li>Require the instrumented versions of your code from your tests.</li>
</ol>

<p>The first prerequisite is fairly easy to satisfy, although you need to remember to re-run node-jscoverage any time you change the source files you're interested in covering. Setting up a script using something like <a href="https://github.com/balupton/watchr">watchr</a> to re-instrument your files if anything changes might be a good option. Anyway, simply executing the following (assuming you've installed node-jscoverage and added it to your path) from the root of your node project will create the instrumented versions of the files in your lib directory:</p>

<pre class="prettyprint">
node-jscoverage lib lib-cov
</pre>

<p>The second prerequisite is not as straightforward to resolve as you might expect unless you want to generate the code coverage on every test run. Why? Because requiring the instrumented code from your test is not the same as requiring the un-instrumented versions. Consider</p>

<pre class="prettyprint lang-js">
// test/myfile_test.js
var fut = require('../lib/myfile.js'); // regular version
var fut = require('../lib-cov/myfile.js'); // instrumented version
</pre>

<p>Since the statement requiring the file under test is in every test you write, having to change it when you want to run the instrumented or un-instrumented files is, in my opinion, not an option. So, on my current project we wrote the following javascript to to serve as a replacement to requiring your files under test.</p>

<pre class="prettyprint lang-js">
// test/coverage.js
var covererageOn = process.argv.some(function(arg) {
  return /^--cover/.test(arg);  
});

if (covererageOn) {
  console.log('Code coverage on');

  exports.require = function(path) {
    var instrumentedPath = path.replace('/lib', '/lib-cov');

    try {
      require.resolve(instrumentedPath);
      return require(instrumentedPath);
    } catch (e) {
      console.log('Coverage on, but no instrumented file found at ' 
        + instrumentedPath);
      return require(path);
    }
  }
} else {
  console.log('Code coverage off');
  exports.require = require;
}
</pre>

<p>In essence, the script checks the process arguments to see if one of the vows --cover-* arguments were passed and changes its require method to substitute /lib-cov for any /lib references. If none of the coverage arguments are detected it simply requires the file as usual. Since your files under test normally use relative references to other files in your lib directory, they get covered too. Check out the example below to see how you would use the script in a test.</p>

<pre class="prettyprint lang-js">
// test/mytest.js
var vows = require('vows');
var assert = require('assert');
var coverage = require('coverage');
var fileUnderTest = coverage.require('../lib/myfile.js');

vows.describe('your test').addBatch({
    // do some testing
}).export(module);
</pre>
]]></description>
            <link>http://www.nearinfinity.com/blogs/jeff_kunkle/automatic_code_coverage_switch.html</link>
            <guid>http://www.nearinfinity.com/blogs/jeff_kunkle/automatic_code_coverage_switch.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Node</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">javascript</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">node.js</category>
            
            <pubDate>Thu, 26 Jan 2012 15:22:16 -0500</pubDate>
        </item>
        
        <item>
            <title>Parsing pcap Files in Node.js with pcap-parser</title>
            <description><![CDATA[<p>On my current project we've been using <a href="http://nodejs.org">Node.js</a> for an app that does a lot of packet capture and processing. In the past we used <a href="https://github.com/mranney/node_pcap">node-pcap</a> for packet capture but were looking for an easier way to simply parse raw pcap files. It turns out the <a href="http://wiki.wireshark.org/Development/LibpcapFileFormat">pcap file format</a> is pretty simple as was writing a node module to parse it.</p>

<p>The module, called <a href="https://github.com/nearinfinity/node-pcap-parser">pcap-parser</a>, can be used to parse any pcap file or readable pcap stream, such as the piped output of <a href="http://www.tcpdump.org/">tcpdump</a>. As packets are parsed, pcap-parser emits relevant events to which your node program can listen. It's a really simple way to process a theoretically infinite stream of pcap data. The code below shows a basic example of it in action. Please check out the <a href="https://github.com/nearinfinity/node-pcap-parser">project page</a> for more details.</p>

<pre class="prettyprint">
var pcapp = require('pcap-parser');

var parser = new pcapp.Parser('/path/to/file.pcap');
// var parser = new pcapp.Parser(process.stdin);

parser.on('packet', function(packet) {
  console.log(packet.header);
  console.log(packet.data);
});

parser.parse(); // to kick things off
</pre> 
]]></description>
            <link>http://www.nearinfinity.com/blogs/jeff_kunkle/parsing_pcap_files_in_nodejs_w.html</link>
            <guid>http://www.nearinfinity.com/blogs/jeff_kunkle/parsing_pcap_files_in_nodejs_w.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Node</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">javascript</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">node</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">pcap</category>
            
            <pubDate>Thu, 12 Jan 2012 08:00:00 -0500</pubDate>
        </item>
        
    </channel>
</rss>

