<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Upon my shoulder</title>
	<atom:link href="http://www.uponmyshoulder.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.uponmyshoulder.com/blog</link>
	<description>// TODO: insert witty tagline</description>
	<lastBuildDate>Thu, 04 Apr 2013 10:24:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Simulating bad network conditions on Linux</title>
		<link>http://www.uponmyshoulder.com/blog/2013/simulating-bad-network-conditions-on-linux/</link>
		<comments>http://www.uponmyshoulder.com/blog/2013/simulating-bad-network-conditions-on-linux/#comments</comments>
		<pubDate>Thu, 04 Apr 2013 10:24:36 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=615</guid>
		<description><![CDATA[Simulate bad network conditions with tc.]]></description>
				<content:encoded><![CDATA[<p>Sometimes, your network is just <em>too</em> good.</p>
<p>Today I ran into this issue as I was testing an application running off a VM in the local network. Latency and bandwidth were excellent, as you&#8217;d expect, but nowhere near the conditions you&#8217;d encounter over the internet. Testing in these conditions is unrealistic and can lead to underestimating issues your users will experience with your app once it&#8217;s deployed.</p>
<p>So let&#8217;s change that and add artificial latency, bandwidth limitations, and even drop a few packets, using <a href="http://linux.die.net/man/8/tc">tc</a>.</p>
<p>Just put the following script in /etc/init.d, modify the values to fit your needs, make it executable, and run /etc/init.d/traffic_shaping.sh start to degrade performance accordingly.</p>
<p><script src="https://gist.github.com/Pluies/5309135.js"></script></p>
<p>I originally found the script on <a href="http://www.iplocation.net/tools/traffic-control.php">top web hosts&#8217; website</a>, and added a few things. Props!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2013/simulating-bad-network-conditions-on-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On overflowing stacks</title>
		<link>http://www.uponmyshoulder.com/blog/2013/on-overflowing-stacks/</link>
		<comments>http://www.uponmyshoulder.com/blog/2013/on-overflowing-stacks/#comments</comments>
		<pubDate>Sat, 02 Feb 2013 03:00:29 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[stack overflow]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=591</guid>
		<description><![CDATA[I recently set out to implement a few basic data structures in C for the hell of it (and to reassure myself that I can still code C), and ran into an interesting compiler wart&#8230; I was trying to instantiate a static array of 10 million integers (who doesn&#8217;t?), in order to test insertions and [...]]]></description>
				<content:encoded><![CDATA[<p>I recently set out to implement a few basic data structures in C for the hell of it (and to reassure myself that I can still code C), and ran into an interesting compiler wart&#8230;</p>
<p>I was trying to instantiate a static array of 10 million integers (who doesn&#8217;t?), in order to test insertions and deletions in my tree. However, as you can astutely deduce from the title of this post, this was too much for the stack of my poor program and ended up in a segfault &#8211; a textbook stack overflow.</p>
<p>I did not think of that at first though, and tried to isolate the offending piece of code by inserting a <span class="code">return 0;</span> in the <span class="code">main()</span> after a piece of code I knew to be working, and working my way down to pinpoint the issue.</p>
<p>Much to my dismay, this didn&#8217;t really work out. Why? Check the following code:</p>
<p><script src="https://gist.github.com/4695893.js"></script></p>
<p>Do you think it works with that last line uncommented? You&#8217;d be wrong!</p>
<pre>[15:20:35]florent@Air:~/Experiments/minefield$ gcc boom.c
[15:20:40]florent@Air:~/Experiments/minefield$ ./a.out
Segmentation fault</pre>
<p>GCC (4.2.1) wants to instantiate the array <em>even though it&#8217;s declared after the function returns</em>!</p>
<p>Interestingly enough, when you tell GCC to optimise the code, it realises the array will never get reached and prunes it away.</p>
<pre>[15:26:06]florent@Air:~/Experiments/minefield$ gcc -O2 boom.c
[15:26:16]florent@Air:~/Experiments/minefield$ ./a.out
Hello world!</pre>
<p>Clang (1.7) exhibits exactly the same behaviour.</p>
<p>Lessons learnt? return is no way of debugging a program.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2013/on-overflowing-stacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript closures as a way to retain state</title>
		<link>http://www.uponmyshoulder.com/blog/2013/javascript-closures-as-a-way-to-retain-state/</link>
		<comments>http://www.uponmyshoulder.com/blog/2013/javascript-closures-as-a-way-to-retain-state/#comments</comments>
		<pubDate>Fri, 01 Feb 2013 08:15:46 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=583</guid>
		<description><![CDATA[Long time no blog! Let&#8217;s get back into it with a nifty and clean way of retaining state in Javascript &#8211; closures. I was recently looking for an easy way to call a specific function after two separate/unrelated AJAX calls to two remote endpoints have been completed. The naive method would be to make the [...]]]></description>
				<content:encoded><![CDATA[<p>Long time no blog! Let&#8217;s get back into it with a nifty and clean way of retaining state in Javascript &#8211; <a title="Closures on wikipedia" href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">closures</a>.</p>
<p>I was recently looking for an easy way to call a specific function after two separate/unrelated AJAX calls to two remote endpoints have been completed. The naive method would be to make the first AJAX call -&gt; callback to the second AJAX  call -&gt; callback to doSomething, but we can use the fact that these two AJAX calls are not related and run them concurrently. An easy way to achieve that is to:</p>
<p>1. set flags, say initialising two global variables at the beginning of the file:</p>
<pre>var callback_one_done = false;
var callback_two_done = false;</pre>
<p>2. have each callbacks set its own flag to &#8216;true&#8217; upon completion and call doSomething<br />
3. check both flags in doSomething:</p>
<pre>var doSomething = function() {
    if (callback_one_done &amp;&amp; callback_two_done){
        // actually do something
    }
}</pre>
<p>But this is a bit ugly, as it litters the global namespace with two flags that are only used there. Instead, thanks to Javascript&#8217;s lexical scope we can declare doSomething as a <strong>closure</strong> and have the flags live inside the function itself:</p>
<pre>var doSomething = (function(){
        var callback_one_done = false;
        var callback_two_done = false;
        return function(source) {
            if (source === 'callback_one') { callback_one_done = true; }
            if (source === 'callback_two') { callback_two_done = true; }
            if (callback_one_done &amp;&amp; callback_two_done) {
                // actually do something
            }
        };
    }());</pre>
<p>What we&#8217;ve done here is declare an anonymous function that returns a function. This newly created function, that gets attributed to doSomething, is a closure that contains both the code needed to run <em>and</em> the flag variables. The state is set and kept inside the function itself, without leaking on the outside environment.</p>
<p>Now we just need to call doSomething(&#8216;callback_one&#8217;) from the first AJAX call and doSomething(&#8216;callback_two&#8217;) from the second AJAX call and doSomething will wait until both calls are complete to run the &#8220;actually do something&#8221; part.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2013/javascript-closures-as-a-way-to-retain-state/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bits of javascript goodness</title>
		<link>http://www.uponmyshoulder.com/blog/2012/bits-of-javascript-goodness/</link>
		<comments>http://www.uponmyshoulder.com/blog/2012/bits-of-javascript-goodness/#comments</comments>
		<pubDate>Sun, 27 May 2012 05:14:26 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=570</guid>
		<description><![CDATA[(This blog post is better followed with the associated github repo. Run the Sinatra server with &#8216;ruby slowserver&#8217;, pop up a browser, and follow the revisions to see how the code gradually gets better. :) ) Recently at work, we wanted to modify some js ad code to include weather data for better ad targeting. [...]]]></description>
				<content:encoded><![CDATA[<p>(This blog post is better followed with <a href="https://github.com/Pluies/Bits-and-pieces/JSBlogPost-May2012/">the associated github repo</a>. Run the Sinatra server with &#8216;ruby slowserver&#8217;, pop up a browser, and follow the revisions to see how the code gradually gets better. :) )</p>
<p>Recently at work, we wanted to modify some js ad code to include weather data for better ad targeting. For certain caching reasons, weather data has to be fetched by an AJAX call, then fed to the ad code.</p>
<p>The existing ad code was (schematically) <a href="https://github.com/Pluies/Bits-and-pieces/blob/28dc08ef21cde9dd854f2b2a0a79b1775034420a/JSBlogPost-May2012/index.html">as follows</a>.</p>
<p>My first idea was to replace those calls by a call to a single js method, which would stall the ad calls using, say, a setTimeout, until the weather data is retrieved. The js looked <a href="https://github.com/Pluies/Bits-and-pieces/blob/450ba36613fd904852e26c1fe9048608f062a712/JSBlogPost-May2012/public/patience.js">like this</a>.</p>
<p>Before you go get the pitchforks, I knew that this code was crap, and asked one of my amazing colleagues for help. He advised me to get rid of the timeOuts and instead use a function that would either, depending if the data was retrieved or not, shelf the task in an array, or execute it.</p>
<p>Using an array of tasks (actually callbacks, or anonymous functions, or closures) allows us to have actual event-driven javascript. This means executing the ad loading code only once, not using resources for timeouts when the resource is not available yet, and maybe most importantly not looking stupid when code review time codes.</p>
<p>Additionally, my code littered the general namespace with his variables. We could instead create a module, and have only that module&#8217;s name public &#8211; we clean up the global namespace and also get private variables for free!<br />
The module pattern is an amazingly useful javascript pattern, implemented as a self-executing anonymous function. If that sounds clear as mud, Ben Cherry has a better, in-depth explanation <a href="http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth" title="Ben Cherry's blogpost on the Module pattern">on his blog</a>.</p>
<p>The js code now looked <a href="https://github.com/Pluies/Bits-and-pieces/blob/9edd3e3524cf56ca3206d18d959ca1e2ca053c2f/JSBlogPost-May2012/public/patience.js">like this</a>.</p>
<p>But the HTML still contained &lt;script&gt; tags, and separation of concerns taught us it&#8217;s better to keep HTML and JS as separate as possible. Cool beans, let&#8217;s just add a couple of line <a href="https://github.com/Pluies/Bits-and-pieces/blob/8d1d9644739015a4cbde28e2f79a9d9a728fdea0/JSBlogPost-May2012/public/patience.js">to our js file</a>.</p>
<p>Result: a pure event-driven wait for a callback, a single variable in the global js namespace, and a cleaned-up HTML.</p>
<p>And of course&#8230; We ended up not using it. The ad code actually expects a document.write, making this delayed approach impossible (document.write appends data inline while the page is still loading, but completely rewrites the page if called after pageload). Good learning experience still!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2012/bits-of-javascript-goodness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Format perlcritic output as TAP to integrate with Jenkins</title>
		<link>http://www.uponmyshoulder.com/blog/2012/format-perlcritic-output-as-tap-to-integrate-with-jenkins/</link>
		<comments>http://www.uponmyshoulder.com/blog/2012/format-perlcritic-output-as-tap-to-integrate-with-jenkins/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 11:23:17 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jenkins]]></category>
		<category><![CDATA[perlcritic]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=560</guid>
		<description><![CDATA[Using shell black magic to tie perlcritic's output to Jenkins]]></description>
				<content:encoded><![CDATA[<p><a href="http://search.cpan.org/~thaljef/Perl-Critic-1.117/lib/Perl/Critic.pm">Perl::Critic</a> is a nifty syntax analyzer able to parse your Perl code, warn you against common mistakes and hint you towards best practices. It&#8217;s available either as a Perl module or a standalone shell script (perlcritic). Unfortunately, there is no standard way to integrate it with Jenkins.</p>
<p><a href="http://jenkins-ci.org/">Jenkins</a>, the continuous-integration-tool-formerly-known-as-Hudson, is the cornerstone of our continuous building process at work. It checks out the latest build from Git, runs a bunch of tests (mainly Selenium, as we develop a website) and keeps track of what goes wrong and what goes right. We wanted to integrate Perl::Critic to Jenkins&#8217; diagnostics to keep an eye on some errors that could creep in our codebase.</p>
<p>So Jenkins doesn&#8217;t do Perl::Critic. However, Jenkins supports TAP. <a href="http://testanything.org/">TAP, the Test Anything Protocol</a>, is an awesomely simple format to express test results that goes as follow:</p>
<pre class="code">1..4
ok 1 my first test
ok 2 another successful test
not ok 3 oh, this one failed
ok 4 the last one's ok</pre>
<p>The first line (the plan) announces how many tests there are, and each following line is a test result beginning by either &#8220;ok&#8221; or &#8220;not ok&#8221; depending on what gave.</p>
<p>Based on such a simple format, we can use a bit of shell scripting to mangle perlcritic&#8217;s output to be TAP-compatible:</p>
<pre class="code"># Perl::Critic                             \
# with line numbers (nl)...                \
# prepend 'ok' for passed files...         \
# and 'not ok' for each error...           \
# and put everything in a temp file

perlcritic $WORKSPACE                      \
  | nl -nln                                \
  | sed 's/\(.*source OK\)$/ok \1/'        \
  | sed '/source OK$/!s/^.*$/not ok &#038;/'    \
  > $WORKSPACE/perlcritic_tap.results.tmp

# Formatting: add the TAP plan, and output the tap results file.
# TAP plan is a line "1..N", N being the number of tests
echo 1..`wc -l < $WORKSPACE/perlcritic_tap.results.tmp` \
 |cat - $WORKSPACE/perlcritic_tap.results.tmp > $WORKSPACE/perlcritic_tap.results

# Cleanup
rm -f $WORKSPACE/perlcritic_tap.results.tmp</pre>
<p>(§WORKSPACE is a Jenkins-set variable referring to where the current build is being worked on.)</p>
<p>And voilà! Jenkins reads the TAP result file and everything runs smoothly.</p>
<p>The only downside of this approach is that the number of tests will vary. For example, a single .pm file containing 5 Perl::Critic violations will show up as 5 failed tests, but fixing these will turn into a single successful test.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2012/format-perlcritic-output-as-tap-to-integrate-with-jenkins/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A gentle introduction to GNU screen</title>
		<link>http://www.uponmyshoulder.com/blog/2012/a-gentle-introduction-to-gnu-screen/</link>
		<comments>http://www.uponmyshoulder.com/blog/2012/a-gentle-introduction-to-gnu-screen/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 06:01:39 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[UNIX]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[screen]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=546</guid>
		<description><![CDATA[A gentle introduction to GNU screen]]></description>
				<content:encoded><![CDATA[<p>You probably heard of <a title="GNU screen on wikipedia" href="http://en.wikipedia.org/wiki/GNU_Screen">GNU screen</a>. It&#8217;s handy, ubiquitous, and dead simple. Here&#8217;s how to use it!</p>
<p>Open a terminal and type:</p>
<pre class="command">screen</pre>
<p>You&#8217;re welcomed by an introduction message, press enter, and&#8230; You&#8217;re in a shell. Uh?</p>
<p><a href="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.52.56-PM.png"><img src="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.52.56-PM.png" alt="" title="Screen shot 2012-01-05 at 6.52.56 PM" width="561" height="381" class="aligncenter size-full wp-image-551" /></a></p>
<h2>Screen is simple</h2>
<p><span class="command">screen</span> is a terminal manager, so it&#8217;s logical that the first thing you see when you start it is a terminal.</p>
<p>This terminal is as vanilla as the terminal we started from. Just try it:</p>
<p><a href="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.52.42-PM.png"><img src="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.52.42-PM.png" alt="" title="Screen shot 2012-01-05 at 6.52.42 PM" width="561" height="381" class="aligncenter size-full wp-image-549" /></a></p>
<p>See? No black magic here, simply a terminal.</p>
<h2>Screen is simple</h2>
<p>The only difference is that <strong>Ctrl+a</strong> is now a special key combination that you can use to invoke <span class="command">screen</span>&#8216;s commands.</p>
<p>So let&#8217;s take a break and quit <span class="command">screen</span>. Type Ctrl+a to let <span class="command">screen</span> know you want its attention, then <strong>d</strong>, as in <strong>detach</strong>. There! You&#8217;re back in your first terminal.</p>
<p><a href="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.56.48-PM.png"><img src="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.56.48-PM.png" alt="" title="Screen shot 2012-01-05 at 6.56.48 PM" width="561" height="381" class="aligncenter size-full wp-image-553" /></a></p>
<p>Let&#8217;s go back in <span class="command">screen</span> and learn some more! Just type:</p>
<pre class="command">screen -r</pre>
<p>The <strong>-r</strong> stands for <strong>reattach</strong>: <span class="command">screen</span> will re-open the last session, the one we detached from. You can see the results of the commands we entered earlier are still here.</p>
<p>We just saw a great feature of <span class="command">screen</span>: the ability to log out and log back in without losing anything. Do you have something long to on a server? SSH into the server, launch <span class="command">screen</span>, launch the task, detach from <span class="command">screen</span>, log out from SSH, go back home, enjoy a good dinner and a well-deserved night of sleep, come back to work, SSH into the server, launch <span class="command">screen -r</span>, and it&#8217;s just as if you never left.</p>
<h2>Screen is simple</h2>
<p>You can already use <span class="command">screen</span> just like that, but let&#8217;s just see another nifty feature: multiple terminals!</p>
<p>In <span class="command">screen</span>, type Ctrl-a, then &#8216;c&#8217;, short for <strong>create</strong>. You&#8217;re in a shell. Uh?</p>
<h2>Screen is simple</h2>
<p>You just created another terminal. <span class="command">screen</span> can manage plenty of simultaneous terminals, not just one. To see a list of them, type Ctrl+a, then the quote symbol &#8220;, and you will see your two terminals. Just use the arrows to select which one you want to open.</p>
<p><a href="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.52.33-PM.png"><img src="http://www.uponmyshoulder.com/blog/wp-content/uploads/2012/01/Screen-shot-2012-01-05-at-6.52.33-PM.png" alt="" title="Screen shot 2012-01-05 at 6.52.33 PM" width="561" height="381" class="aligncenter size-full wp-image-550" /></a></p>
<p>There you go, you know <span class="command">screen</span>! See, I told you it was simple.</p>
<h2>Misc useful commands</h2>
<p>Do you want to change the name of a terminal in <span class="command">screen</span>&#8216;s list? In that terminal, Ctrl+a and A.<br />
Do you want to go directly to a specific terminal? Ctrl+a and its number.<br />
Do you want to go to the <strong>p</strong>revious/<strong>n</strong>ext terminal? Ctrl+a and p or n.<br />
Do you want to switch to the previous terminal quickly? Ctrl+a and Ctrl+a.<br />
Do you want to remap Ctrl+a to another key, say Ctrl+b? Just put escape ^b in your <span class="command">.screenrc</span>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2012/a-gentle-introduction-to-gnu-screen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep GimmeSomeTune running</title>
		<link>http://www.uponmyshoulder.com/blog/2011/keep-gimmesometune-running/</link>
		<comments>http://www.uponmyshoulder.com/blog/2011/keep-gimmesometune-running/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 10:45:31 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[OS X]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=539</guid>
		<description><![CDATA[Keep GimmeSomeTune running with launchd]]></description>
				<content:encoded><![CDATA[<p>As a follow-up on my <a title="Automatically restart applications on OS X" href="http://www.uponmyshoulder.com/blog/2011/automatically-restart-applications-on-os-x">previous post on the question</a>, which advocated a simple (but bad) approach to keeping GimmeSomeTune running, here&#8217;s a better way!</p>
<p>The Good Thing &#8482; to do is to use OS X&#8217;s built-in mechanism to start and keep processes running, namely <a title="Launchd on Wikipedia" href="http://en.wikipedia.org/wiki/Launchd">launchd</a>.</p>
<p>What we have to do is simply to write a plist containing the info needed by launchd, namely:</p>
<pre class="command">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
	&lt;dict&gt;
		&lt;key&gt;KeepAlive&lt;/key&gt;
		&lt;true/&gt;
		&lt;key&gt;Label&lt;/key&gt;
		&lt;string&gt;at.eternalstorms.gstlauncherdaemon&lt;/string&gt;
		&lt;key&gt;ProgramArguments&lt;/key&gt;
		&lt;array&gt;
			&lt;string&gt;/Applications/GimmeSomeTune.app/Contents/MacOS/GimmeSomeTune&lt;/string&gt;
		&lt;/array&gt;
		&lt;key&gt;RunAtLoad&lt;/key&gt;
		&lt;true/&gt;
	&lt;/dict&gt;
&lt;/plist&gt;</pre>
<p>Save this as a plist file in <span class="filename">~/Library/LaunchAgents</span>. The name doesn&#8217;t really matter, but the best way to keep everything tidy and adhere to OS X&#8217;s standards is to call it <span class="filename">at.eternalstorms.gstlauncherdaemon.plist</span>.</p>
<p>Alright! Now GimmeSomeTune is going to start when you log in, and launchd will make sure it keeps running (i.e. relaunch it if it crashes). To tell launchd to use that plist file right now without having to log out and back in again, run:</p>
<pre class="command">launchctl load -w ~/Library/LaunchAgents/at.eternalstorms.gstlauncherdaemon.plist</pre>
<p>And conversely, to stop it:</p>
<pre class="command">launchctl unload -w ~/Library/LaunchAgents/at.eternalstorms.gstlauncherdaemon.plist</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2011/keep-gimmesometune-running/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Optimising a video editor plugin</title>
		<link>http://www.uponmyshoulder.com/blog/2011/optimising-a-video-editor-plugin/</link>
		<comments>http://www.uponmyshoulder.com/blog/2011/optimising-a-video-editor-plugin/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 00:26:19 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[optimisation]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=523</guid>
		<description><![CDATA[During the past few weeks, I have been writing a C++ plugin to grade C41 digital intermediates in Cinelerra, an open-source Linux video editor. C41 is the most common chemical process for negatives, resulting in films that look like this — you probably know it if you&#8217;ve ever shot film cameras. Of course, after scanning [...]]]></description>
				<content:encoded><![CDATA[<p>During the past few weeks, I have been writing <a href="https://github.com/Pluies/C41">a C++ plugin</a> to grade C41 digital intermediates in <a href="cinelerra.org">Cinelerra</a>, an open-source Linux video editor. <a href="http://en.wikipedia.org/wiki/C-41_process">C41</a> is the most common chemical process for negatives, resulting in films that look like <a href="http://www.ag-photographic.co.uk/ekmps/shops/matt5791/resources/Design/filmstrip.jpg">this</a> — you probably know it if you&#8217;ve ever shot film cameras.</p>
<p>Of course, after scanning those negatives, you have to process (&#8220;grade&#8221;) them to turn them back to positive. And it&#8217;s not as simple as merely inverting the values of each channel for each pixel; C41 has a very pronounced orange shift that you have to take into account.</p>
<h2>The algorithm</h2>
<p>The core algorithm for this plugin was lifted from <a href="http://sites.google.com/site/negfix/">a script</a> written by JaZ99wro for still photographs, based on ImageMagick, which does two things:<br />
- Compute &#8220;magic&#8221; values for the image<br />
- Apply a transformation to each channel (R, G, B) based on those magic values</p>
<p>The problem with film is that due to tiny changes between the images, the magic values were all over the place from one frame to the other. Merely applying JaZ&#8217;s script on a series of frames gave a sort of &#8220;flickering&#8221; effect, with colours varying from one frame to the other, which is unacceptable effect for video editing.</p>
<p>The plugin computes those magic values for each frame of the scene, but lets you pick and fix specific values for the duration of the scene. The values are therefore not &#8220;optimal&#8221; for each frame, but the end result is visually very good.</p>
<p>However, doing things this way is slow: less than 1 image/second for 1624*1234 frames.</p>
<h2>Optimising: do less</h2>
<p>The first idea was to make optional the computing of the magic values: after all, when you&#8217;re batch processing a scene with fixed magic values, you don&#8217;t need to compute them again for each frame.</p>
<p>It was a bit faster, but not by much. A tad more than an image/second maybe.</p>
<h2>Optimising: measure</h2>
<p>The next step —which should have been the first!— was to actually benchmark the plugin, and see where the time was spent. Using <a href="http://linux.die.net/man/3/clock_gettime"><span class="command">clock_gettime()</span></a> for maximum precision, the results were:</p>
<p>~0.3 seconds to compute magic values (0.2s to apply a box blur to smooth out noise, and 0.1s to actually compute the values)</p>
<p>~0.9 seconds to apply the transformation</p>
<p>Optional computing of the magic values was indeed a step in the right direction, but the core of the algorithm was definitely the more computationally expensive. Here&#8217;s what&#8217;s to be computed for each pixel:</p>
<pre class="code">row[0] = (magic1 / row[0]) - magic4;
row[1] = pow((magic2 / row[1]),1/magic5) - magic4;
row[2] = pow((magic3 / row[2]),1/magic6) - magic4;</pre>
<p>With <span class="command">row[0]</span> being the red channel, <span class="command">row[1]</span> the green channel, and <span class="command">row[2]</span> the blue channel.</p>
<p>The most expensive call here is <span class="command">pow()</span>, part of <span class="command">math.h</span>. We don&#8217;t need to be extremely precise for each pixel value, so maybe we can trade some accuracy for raw speed?</p>
<h2>Optimising: do better</h2>
<p>Our faithful friend Google, tasked with searching for a fast float <span class="command">pow()</span> implementation, gives back <a href="http://www.dctsystems.co.uk/Software/power.html">Ian Stephenson&#8217;s implementation</a>, a short and clear (and more importantly, working) version of <span class="command">pow()</span>.</p>
<p>But we can&#8217;t just throw that in without analysing how it affects the resulting frame. The next thing to do was to add a button that would switch between the &#8220;exact&#8221; version and the approximation: the results were visually identical.</p>
<p>Just to be sure, I measured the difference between the two methods, and it shows an average 0.2% difference, going as high as 5% for the worst case, which are acceptable values.</p>
<p>And the good news is that the plugin now only takes 0.15 to 0.20 second to treat each image, i.e. between 5 and 6 images/second — an 8-fold gain since the first version. Mission accomplished!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2011/optimising-a-video-editor-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails in a week — day 7</title>
		<link>http://www.uponmyshoulder.com/blog/2011/rails-in-a-week-day-7/</link>
		<comments>http://www.uponmyshoulder.com/blog/2011/rails-in-a-week-day-7/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 20:46:58 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[railsinaweek]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=518</guid>
		<description><![CDATA[Today was the last day of my Rails week. I added some database-backing to my app (with a fully scaffolded model and all!) for the countries&#8217; data and refactored a fair bit, though I&#8217;m still unsure about a few decisions I made, such as if I should put the base data in seeds.rb or in [...]]]></description>
				<content:encoded><![CDATA[<p>Today was the last day of my Rails week. I added some database-backing to my app (with a fully scaffolded model and all!) for the countries&#8217; data and refactored a fair bit, though I&#8217;m still unsure about a few decisions I made, such as if I should put the base data in seeds.rb or in a migration. Oh, well.</p>
<p>The website is available here:</p>
<p><a href="http://antipodes.plui.es">http://antipodes.plui.es</a></p>
<p>And its source code <a href="https://github.com/Pluies/Antipodes">is on GitHub</a>.</p>
<h2>So?</h2>
<p>Writing this website taught me quite a lot about Rails in general, from its innards to automated deployment. I still have a lot to learn, and more importantly a lot of practice to do before I can say I&#8217;m competent with Rails, but that was a great start. *self-pat on the back*</p>
<p>The more important thing to me is that even though I didn&#8217;t have the time to learn each of the aspects of the Rails ecosystem inside and out, I have a better overview of &#8220;what does what&#8221; in Rails, and a lot of good pointers to learn more when needed.</p>
<h2>Buzzword bingo!</h2>
<p>All in all, I learned (&#8220;began learning&#8221; would be more appropriate):</p>
<ul>
<li>Vagrant, and a tiny bit of Chef</li>
<li>Rails (eh!): MVC, migrations, the global directory structure, views/partials, the asset pipeline, etc.</li>
<li>RVM</li>
<li>Bundler</li>
<li>Rake</li>
<li>Spork</li>
<li>Autotest</li>
<li>RSpec</li>
<li>Capistrano</li>
</ul>
<h2>Loose ends</h2>
<p>Well, although I think I understood a fair amount of what I set out to learn, I still don&#8217;t grasp Chef at all, and didn&#8217;t really adhere to the philosophy of TDD through the week. My tests are really basic and not very satisfying; writing meaningful tests seems like quite an difficult art that I&#8217;ll have to learn more about.</p>
<p>Another thing that bugs me a bit is that it took more time than I originally thought to do a lot of the things I set out to do. This is probably due to inexperience, so in a way I&#8217;m curing it? I guess? I probably could have gained some time by asking a few questions on things like IRC, but it felt a bit stupid when there&#8217;s such a trove of information about Rails online. Indeed, googling and reading guides or StackOverflow threads / mail threads always ended up giving the answer; but maybe not as fast as IRC would&#8217;ve been.</p>
<p>Oh, and the design of the website is pretty terrible, but that wasn&#8217;t really the goal (and CSS has never been my forte).</p>
<h2>What now?</h2>
<p>Well, as far as Rails go, I&#8217;m ready to tackle bigger things. I hope to find a Rails job in Wellington (wink wink nudge nudge if you&#8217;re reading this from New Zealand ;) ) and put this freshly-acquired knowledge to good use!</p>
<p>I also hope these blog posts might help a newcomer to Rails, but they ended up being half ranting and half specific bug-finding, so I&#8217;m not sure of their value as a learning tool. Or as a read to anyone else than me actually. I&#8217;ll just post this on HN and let the crowd decide.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2011/rails-in-a-week-day-7/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Rails in a week — day 6</title>
		<link>http://www.uponmyshoulder.com/blog/2011/rails-in-a-week-day-6/</link>
		<comments>http://www.uponmyshoulder.com/blog/2011/rails-in-a-week-day-6/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 22:59:17 +0000</pubDate>
		<dc:creator>Florent Delannoy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[railsinaweek]]></category>

		<guid isPermaLink="false">http://www.uponmyshoulder.com/blog/?p=510</guid>
		<description><![CDATA[TL;DR: testing works, I learned i18n, and fixed a bug through TDD. &#160; After writing a simple little functional test and making it run through rake test, albeit slowly, I installed Spork and autotest. From what I gathered, Spork is an RSpec-only thing, so I wrote a few RSpec tests instead of functional tests. After [...]]]></description>
				<content:encoded><![CDATA[<p>TL;DR: testing works, I learned i18n, and fixed a bug through TDD.</p>
<p>&nbsp;</p>
<p>After writing a simple little <a href="https://github.com/Pluies/Antipodes/blob/master/test/functional/antipodes_controller_test.rb">functional test</a> and making it run through <span class="command">rake test</span>, albeit slowly, I installed Spork and autotest. From what I gathered, Spork is an RSpec-only thing, so I wrote a few RSpec tests instead of functional tests. After a bit of tweaking, everything was going smoothly between Spork and autotest, all running RSpec, but my file in <span class="filename">test/</span> was ignored. Moving on.</p>
<p>I fixed the bug causing a 500 error when an empty string was entered on the main screen, by writing the following spec:</p>
<pre class="code">it "should not render en empty request" do
  get :address, :q =&gt; ""
  response.should_not render_template("address")
  response.should redirect_to("/")
end</pre>
<p>Which was red, then green (yay!). I once read that TDD introduced a &#8220;strange smoothing feeling&#8221; (paraphrasing), which is definitely true: seeing automated tests pass contributes quite a lot to one&#8217;s peace of mind.</p>
<p>I also used the flash hash to flash a notification that the address was invalid. Actually making the notice &#8220;flash&#8221; (i.e. appear and disappear) took a bit of jQuery (<a href="https://github.com/Pluies/Antipodes/blob/master/app/views/shared/_notice_div.html.erb">nothing fancy</a>, but still!).</p>
<p>Moving on, my next goal was i18n (internationalisation), to make my website switch automatically from English to French depending on the browser&#8217;s HTTP Accept-language header. After a quick read of the official website&#8217;s own <a href="http://guides.rubyonrails.org/i18n.html">guide on i18n</a> (excellent as usual), I modified the files as needed (just noticed I didn&#8217;t split up my locale files, which maybe I should have done) and set out to find how to change the language depending on the HTTP header.</p>
<p>Apparently this isn&#8217;t &#8220;the Rails way&#8221;, because it isn&#8217;t RESTful; two request to the same URL won&#8217;t give back the same result depending on the browser&#8217;s configured language. Instead, Rails recommends either 1) an explicit <span class="filename">?locale=</span> parameter in the URL, 2) modifying the routing scheme from, say, <span class="filename">/page</span> to <span class="filename">/en/page</span> and <span class="filename">/fr/page</span>, or even 3) using two separate domains, e.g. <span class="filename">myapp.com</span> and <span class="filename">myapp.fr</span>. I&#8217;m not really fond of the first solution, the second one is okay but should be thought out from the start, and the third one is great but doesn&#8217;t fit my needs here. HTTP headers it is.</p>
<p>Thankfully, the <a href="https://github.com/iain/http_accept_language">http-accept-language gem</a> allows you to easily find the best match between which language(s) the browser demands and which language(s) you can provide. Changing the locale according to this data was <a href="https://github.com/Pluies/Antipodes/blob/master/app/controllers/application_controller.rb#L4-13">a simple before_filter away in the ApplicationController</a>. I used <a href="http://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-ruby-debug">the debugger</a> and <span class="command">curl</span> commands (the <span class="command">-H</span> option allows writing custom headers) to make sure everything was working correctly.</p>
<p>I went back to Spork after a while, not exactly understanding why autotest wasn&#8217;t doing the same thing as <span class="command">rake test</span>. If I understand correctly, it looks like autotest runs either rspec or test::unit (which doesn&#8217;t only cover unit tests, but also functional tests, etc, as long as they&#8217;re under <span class="filename">test/</span>), and that Spork being RSpec-only, autotest+Spork only worked with RSpec. After some more research, Spork actually supports test::unit&#8230; As a separate gem, spork-testunit. And those two are completely separated: they don&#8217;t listen on the same port and are called by completely different commands (respectively <span class="command">rspec spec</span> and <span class="command">testdrb</span>).</p>
<p>A quite hackish workaround is to start two different Spork servers with <span class="command">bin/spork TestUnit &amp; bin/spork RSpec</span>, and to add hooks to autotest to launch Unit::Tests too <a href="https://github.com/Pluies/Antipodes/blob/master/.autotest#L3-5">in the <span class="filename">.autotest</span> file</a>. It means Test::Unit is only launched after some specs needed re-testing instead of the traditional &#8220;a file change, it runs tests&#8221;, but it still works.</p>
<p>Now, I guess the problem is that a project won&#8217;t need to have testing done both in RSpec <strong>and</strong> in Test::Unit. I could be wrong about that, but it seems that the goals and end results of both framework are pretty similar.</p>
<p>&nbsp;</p>
<p>With all that testing, I didn&#8217;t have time to implement my country-wide search, which I hope to do tomorrow for this week&#8217;s final day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.uponmyshoulder.com/blog/2011/rails-in-a-week-day-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 5/13 queries in 0.052 seconds using disk: basic

 Served from: www.uponmyshoulder.com @ 2013-05-26 08:30:30 by W3 Total Cache -->