<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>fifteen seconds of infamy</description><title>William Groppe</title><generator>Tumblr (3.0; @willgroppe)</generator><link>http://will.groppe.us/</link><item><title>Micro languages in Clojure - part 2</title><description>&lt;p&gt;In the previous article we constructed a tiny DSL for generating SQL statements using a more Clojure like syntax.  It works, but it has a couple of problems:  It doesn&amp;#8217;t parameterize SQL variables, and nesting doesn&amp;#8217;t work properly.  In this article we&amp;#8217;ll rectify those issues, and add a bit of addition functionality.&lt;/p&gt;
&lt;p&gt;So to parameterize the SQL statements we obviously need to extract the values from our generated SQL and replace them with question marks.&lt;/p&gt;
&lt;p&gt;This:&lt;/p&gt;
&lt;script src="http://gist.github.com/535532.js?file=gistfile1.txt"&gt;&lt;/script&gt;&lt;p&gt;Needs to become this:&lt;/p&gt;
&lt;script src="http://gist.github.com/535535.js?file=gistfile1.txt"&gt;&lt;/script&gt;&lt;p&gt;The library we are using expects parameterized SQL in the following form:&lt;/p&gt;
&lt;script src="http://gist.github.com/535713.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;That should be the eventual result of our SQL generator.&lt;/p&gt;
&lt;p&gt;For our simple DSL we need to accumulate the string portion of the statement, and the variables for binding.  Whenever I need to accumulate more then one thing in Clojure, I tend to lean on maps.  You may know them by different names, perhaps dictionary or hash table, etc.  They are a very useful data structure, and I tend to use them quite frequently.  Perhaps too much, but that&amp;#8217;s another story for another day.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s create a simple helper function for making maps of SQL fragments:&lt;/p&gt;
&lt;script src="http://gist.github.com/535540.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;This function takes multiple arguments and checks to see of the last one is a vector.  If the last one is a vector, it&amp;#8217;s assumed to be a variable to be bound to the SQL.  It returns a map with the &lt;span class="code"&gt;:stmt&lt;/span&gt; key bound to the SQL string, and the &lt;span class="code"&gt;:vars&lt;/span&gt; key bound to a list of the variables.&lt;/p&gt;
&lt;p&gt;Now we need another little helper function to selectively generate paramatized statements.&lt;/p&gt;
&lt;script src="http://gist.github.com/535543.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Let&amp;#8217;s modify the &lt;span class="code"&gt;sql-exp&lt;/span&gt; function to call &lt;span class="code"&gt;paramatize&lt;/span&gt;:&lt;/p&gt;
&lt;script src="http://gist.github.com/535545.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;A call to &lt;span class="code"&gt;sql-exp&lt;/span&gt; will now return a map resembling this:&lt;/p&gt;
&lt;script src="http://gist.github.com/535548.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Our little SQL DSL is now using bound variables.  The other issue to be dealt with is the nesting of SQL expressions.  To tackle that issue we&amp;#8217;ll create another helper function:&lt;/p&gt;
&lt;script src="http://gist.github.com/535554.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;All this function does is surround the supplied statement with parentheses.  So nesting should now work correctly.  Albeit at the cost of one superfluous pair of parentheses.&lt;/p&gt;
&lt;p&gt;All the maps get merged together using &lt;span class="code"&gt;merge-with concat&lt;/span&gt;.  Which does exactly what it says, merge the maps by concatenating the values with matching keys.  All that&amp;#8217;s left is to take the resulting map and convert it to a form that &lt;a href="http://richhickey.github.com/clojure-contrib/#sql"&gt;clojure.contrib.sql&lt;/a&gt; can digest.  So one more helper function:&lt;/p&gt;
&lt;script src="http://gist.github.com/535558.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Let&amp;#8217;s give it a test run:&lt;/p&gt;
&lt;script src="http://gist.github.com/535574.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Looks good, bound variables are working and nesting is handled correctly now.  Let&amp;#8217;s see how much work it would take to add sorting and result set paging.&lt;/p&gt;
&lt;p&gt;For sorting we&amp;#8217;ll use our macro/let binding trick again to create functions &lt;span class="code"&gt;asc&lt;/span&gt; and &lt;span class="code"&gt;desc&lt;/span&gt;.&lt;/p&gt;
&lt;script src="http://gist.github.com/535586.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Paging turns out to be dead simple:&lt;/p&gt;
&lt;script src="http://gist.github.com/535600.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Let&amp;#8217;s test and make sure it works as expected:&lt;/p&gt;
&lt;script src="http://gist.github.com/535626.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Looks good, though the nesting is getting a bit extreme.  As luck would have it, Clojure offers a great solution for over nestification.  Say hello to the &lt;span class="code"&gt;&lt;a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/-&amp;gt;&amp;gt;"&gt;-&amp;#187;&lt;/a&gt;&lt;/span&gt; macro.  With the &amp;#8220;threading&amp;#8221; macro, we can rewrite the above as:&lt;/p&gt;
&lt;script src="http://gist.github.com/535633.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Much more readable.  Now our tiny DSL meets almost all of its users&amp;#8217; (yours truly) needs.  In the future we might revisit how the SQL is generated to enhance composibility, and perhaps provide a more terse syntax for specifying table columns.&lt;/p&gt;
&lt;p&gt;As always the complete source is available on &lt;a href="http://github.com/wilig/net.groppe.sequel"&gt;Github&lt;/a&gt;.&lt;/p&gt;</description><link>http://will.groppe.us/post/973148216</link><guid>http://will.groppe.us/post/973148216</guid><pubDate>Wed, 18 Aug 2010 14:59:58 -0400</pubDate></item><item><title>Micro languages in Clojure via macros and let.</title><description>&lt;p&gt;While working on a couple of web projects using &lt;a href="http://github.com/cgrand/moustache"&gt;moustache&lt;/a&gt; I&amp;#8217;ve been slowly building up some abstractions for dealing with SQL for data durability.  The abstractions are built on top of the &lt;a href="http://richhickey.github.com/clojure-contrib/sql-api.html"&gt;clojure.contrib.sql&lt;/a&gt; library which works great.  However I was interested in being able to express my queries in a more &amp;#8220;Clojuresque&amp;#8221; way. The library that immediately popped to mind was the &lt;a href="http://gitorious.org/clojureql/"&gt;ClojureQL&lt;/a&gt; project.  Alas there were two issues with ClojureQL.  First and foremost, it seems abandoned or at least stagnant at the moment, hopefully that will change as it has lots of potential.  The second issue is that the abstraction it provides seems backwards.  Here&amp;#8217;s an example:&lt;/p&gt;
&lt;script src="http://gist.github.com/512962.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;In Clojure the collection generally comes last.  So perhaps something like this, would be more idiomatic:&lt;/p&gt;
&lt;script src="http://gist.github.com/512967.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Let&amp;#8217;s see if we can build a tiny SQL abstraction layer to provide the above interface.  Let&amp;#8217;s  agree before we begin that one of our goals should be to minimize namespace pollution, especially considering we&amp;#8217;ll be redefining some clojure.core functions.  Users of the library (aka me) shouldn&amp;#8217;t have to prefix every function or resort to the use of &lt;span class="code"&gt;(:use)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;We don&amp;#8217;t want this:&lt;/p&gt;
&lt;script src="http://gist.github.com/512972.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;A thought occurred to me the other day for working around this issue.  Perhaps we can locally rebind symbols using &lt;span class="code"&gt;let&lt;/span&gt; inside a macro to give us the interface we&amp;#8217;d like:&lt;/p&gt;
&lt;script src="http://gist.github.com/512975.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Much nicer in my opinion.  That thought in hand, let&amp;#8217;s get down to work.  Starting with collect macro we have:&lt;/p&gt;
&lt;script src="http://gist.github.com/513003.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;(The &lt;span class="code"&gt;sqlize&lt;/span&gt; function simply transforms :table into &amp;#8220;table&amp;#8221; and :table/col into &amp;#8220;table.col&amp;#8221;.)&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s our first use of &lt;span class="code"&gt;let&lt;/span&gt; to rebind a symbol.  &lt;span class="code"&gt;as&lt;/span&gt; is bound to an anonymous function but only within the context of collect.  Now a quick test of the new collect macro:&lt;/p&gt;
&lt;script src="http://gist.github.com/513011.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;So now we can generate select statements, next up is filtering those &amp;#8220;collections&amp;#8221;.  Here&amp;#8217;s the filter macro:&lt;/p&gt;
&lt;script src="http://gist.github.com/513438.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Again, we use &lt;span class="code"&gt;let&lt;/span&gt; to bind symbols local to only the macro.  Time to test filtering:&lt;/p&gt;
&lt;script src="http://gist.github.com/513486.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;It works!  Here&amp;#8217;s our (in)complete SQL DSL:
&lt;script src="http://gist.github.com/513498.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it for now, in the next article we&amp;#8217;ll fix our nesting issue, and implement bound variables in the generated SQL.&lt;/p&gt;
&lt;p&gt;If you want to play around, all the code is available on &lt;a href="http://github.com/wilig/net.groppe.sequel"&gt;Github&lt;/a&gt;.&lt;/p&gt;</description><link>http://will.groppe.us/post/943063292</link><guid>http://will.groppe.us/post/943063292</guid><pubDate>Thu, 12 Aug 2010 13:55:40 -0400</pubDate></item><item><title>Clojure simple threaded web server performance test.</title><description>&lt;p&gt;After reading &lt;a href="http://dosync.posterous.com/22516635"&gt;David Nolen&amp;#8217;s&lt;/a&gt; post about &lt;a href="http://github.com/ztellman/aleph"&gt;Aleph&lt;/a&gt; I was curious to see how scalable a simple Clojure threaded web application could be when paired with good old Postgres 8.4.  To make things just slightly more realistic, two queries will be generated per request, and a basic listing of the data will be produced.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m running this experiment on an iMac with a 2.8 Ghz Intel Core i7 with 8 gigs of memory.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s the code:&lt;/p&gt;
&lt;script src="http://gist.github.com/506984.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;And here&amp;#8217;s a screen capture of the output page produced:&lt;/p&gt;
&lt;p&gt;&lt;a title="scale-html-screen-grab by Will Groppe, on Flickr" href="http://www.flickr.com/photos/willgroppe/4855245293/"&gt;&lt;img alt="scale-html-screen-grab" height="284" width="317" src="http://farm5.static.flickr.com/4122/4855245293_b4d01f3136.jpg"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The tests will be run with apache bench set to make one thousand requests with a concurrency of fifty.  The tests will be run ten times and the results averaged.&lt;/p&gt;
&lt;pre&gt;ab -n 1000 -c 50 http://localhost:8080/&lt;/pre&gt;
&lt;p&gt;The first pass will use straight non-pooled connections.  &lt;/p&gt;
&lt;script src="http://gist.github.com/506970.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Results: &lt;strong&gt;668 requests a second.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Not exactly speedy, but that&amp;#8217;s expected due to the high cost of making a connection to the database for every request.&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s will switch to connection pooling.  Eliminating the connection overhead should increase performance significantly.&lt;/p&gt;
&lt;script src="http://gist.github.com/506974.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;Result: &lt;strong&gt;901 requests a second.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Better, but not nearly as large a difference as I was expecting.  Perhaps changing the maximum connections to the database will improve the performance.  Let&amp;#8217;s bump the maximum connections to 3 and try again.&lt;/p&gt;
&lt;p&gt;Result: &lt;strong&gt;2203 requests a second.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A large improvement.  Setting the maximum connections to 3 increased performance rather dramatically.  Now let&amp;#8217;s try setting the maximum connections to 50 to match the concurrency we are using in apache bench.&lt;/p&gt;
&lt;p&gt;Result: &lt;strong&gt;3138 requests a second.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Again a large improvement.  Not a surprise as the optimal connection level for the work load at hand was readily apparent.  Time for one more experiment:  Setting the maximum connections to 1000.  &lt;em&gt;(postgres.conf must be adjusted to allow one thousand connections.)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Result: &lt;strong&gt;3065 requests a second.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Basically the same as the previous run.  So blindly setting the maximum connections higher does not pay off in improved performance in this circumstance.&lt;/p&gt;
&lt;p&gt;The simple experiment illustrated here is merely the tip of a very large optimization ice-burg.  I know very little about actually tuning Postgres.  There are other connection pool libraries out there that could potentially be faster.  Operations folks experienced in these matters may be able to squeeze out even more performance for very little complexity cost.&lt;/p&gt;
&lt;p&gt;My personal conclusion is that it&amp;#8217;s extremely unlikely that any project I&amp;#8217;m building will ever need the additional raw speed offered by event based servers.  The complexity is, quite simply, not worth the cost.  Every situation is different but trading complexity for raw speed, unless it&amp;#8217;s your only competitive advantage, seems like a fools errand.&lt;/p&gt;
&lt;p&gt;If you want to play around yourself, the code is available on &lt;a href="http://github.com/wilig/scale"&gt;Github&lt;/a&gt;.&lt;/p&gt;</description><link>http://will.groppe.us/post/899492559</link><guid>http://will.groppe.us/post/899492559</guid><pubDate>Tue, 03 Aug 2010 16:37:37 -0400</pubDate></item><item><title>Named routes for Compojure</title><description>&lt;p&gt;I&amp;#8217;ve been itching for named routes in &lt;a title="Compojure" target="_blank" href="http://github.com/weavejester/compojure"&gt;Compojure&lt;/a&gt; for a few weeks now.  I tend to change my site structure often in the beginning of a project, and string literals for links was making my life difficult.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s my, probably naive, solution:&lt;/p&gt;
&lt;script src="http://gist.github.com/322462.js?file=gistfile1.clj"&gt;&lt;/script&gt;</description><link>http://will.groppe.us/post/429522579</link><guid>http://will.groppe.us/post/429522579</guid><pubDate>Fri, 05 Mar 2010 23:11:00 -0500</pubDate></item><item><title>Sending email from Clojure.</title><description>&lt;p&gt;I&amp;#8217;m working on several small projects in Clojure, and for one of them I needed to be able to send email.  Quick googling revealed &lt;a href="http://nakkaya.com/2009/11/10/using-java-mail-api-from-clojure/"&gt;this&lt;/a&gt;.  Java Mail API?  Just what I wanted.  However since I&amp;#8217;m using leiningen, I needed to find a maven repository with javamail.  In my search for javamail I happened across Apache &lt;a href="http://commons.apache.org/email/"&gt;commons-email&lt;/a&gt;, the description is as follows: &amp;#8220;&lt;b&gt;Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.&lt;/b&gt;&amp;#8220;  Simplify you say?  Well that is worth a look&amp;#8230;  Long story short, if you are using leiningen.  In project.clj add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
:repositories [["central-proxy" "http://repository.sonatype.org/content/repositories/central/"]]
:dependencies [[org.apache.commons/commons-email "1.2"]]
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;Now you can simply do the following:&lt;/p&gt;
&lt;script src="http://gist.github.com/322481.js?file=gistfile1.clj"&gt;&lt;/script&gt;&lt;p&gt;This does add one extra dependency but, considering it can easily send HTML email and do attachments, I think it&amp;#8217;s worth it.&lt;/p&gt;</description><link>http://will.groppe.us/post/406065542</link><guid>http://will.groppe.us/post/406065542</guid><pubDate>Mon, 22 Feb 2010 21:19:00 -0500</pubDate></item></channel></rss>

