Posted by hank, Sun Jun 10 17:13:00 UTC 2007
Well, I was over at Debian Package of the Day when I noticed an article on KLone. It’s a little application framework that allows you to do XHTML templating in C! Then, you can compile it and send it off to any Linux machine (with a few dependencies of course) to run it. I got it, and it didn’t work for me at first. Then, for some reason, it started working. Here’s what I think did it:
In Ubuntu, I did this:
sudo apt-get install build-essential libssl-dev quilt klone klone-package
Then, I made a new test project:
make-klone-project create -p myhello
This creates a new project directory. Now, go into it and make it your new home:
cd myhello-0.1/
cd userdata/
mkdir www etc
vim etc/kloned.conf
I’m just going to assume you’re using vim because, well, you should be. Make the config file look something like this:
server_list my_http
allow_root yes
my_http
{
type http
addr.type IPv4
addr.port 8880
dir_root /www
}
Now lets give it something to work with (note we’re still in userdata):
vim www/index.klone
<html> <head><title>Hello Lady!</title></head> <body> <% io_printf(out, "Hey, Lady! You call him Dr. Jones!"); %> </body> </html>
Now add your precious files to the sauce:
cd ../../site/
klone -c import ../userdata/
# 2 dirs and 2 files imported
cd ..
Now, compile and run it:
kloned-build -o myapp userdata
./myapp -F # This runs it in non-daemonized mode
If you don’t get any errors, congratulations. That means I did something right.
Now, just hop over to here or wherever you specified it to run, and it will magically appear.
Now you can do this:
<html> <head><title>Hello World</title></head> <body> <% int i; for(i=0; i < 10; ++i) { io_printf(out, "Hello Lady! %d<br />", i); } %> </body> </html>
Then run this to rebuild and re-run the server:
kloned-build -o myapp userdata && ./myapp -F
Update
So, I ran some tests, and I have to say, the speed increase from C might be really awesome every now and again. Here’s the code:
clock_t curtime = clock(); #define SEED 35791246 int niter=10000000; double x,y; int count=0; /* # of points in the 1st quadrant of unit circle */ double z; double pi; /* initialize random numbers */ srand(SEED); count=0; for ( i=0; i<niter; i++) { x = (double)rand()/RAND_MAX; y = (double)rand()/RAND_MAX; z = x*x+y*y; if (z<=1) count++; } pi=(double)count/niter*4; io_printf(out, "# of trials= %d , estimate of pi is %g \n",niter,pi); io_printf(out, "%f", (double)(clock() - curtime)/(double)CLOCKS_PER_SEC);
All this is is a Monte Carlo method of calculating pi that I stole from here. It takes 0.56 seconds of CPU time on my Core 2 Duo @ 3.3Ghz. Now for the Ruby on Rails test:
# Controller @start = Time.now srand(35791246) iter = 10000000 count = 0 0.upto(iter) do |i| x = rand().to_f y = rand().to_f z = x*x+y*y count += 1 if z <= 1 end @pi = count.to_f / iter * 4 @end = Time.now
This finishes in 20.75 seconds (about 40x slower). C is great for things like this. I hope to use KLone in the future for these kinds of tasks.

Blog Posts