Harsh J

Memoirs of a QWERTY Keyboard

Erlang: Using the timer:tc function in escripts

one comment

Both escripts and timer:tc features of Erlang are very useful for writing simple test code to profile functions.

There is one tiny issue with using timer:tc/1 or timer:tc/2 from within escripts though: they will not work in the interpreted execution mode.

For example, you may have an escript such as below:

#!/usr/bin/env escript
-module(test).

-export([generate_list/1]).

generate_list(N) ->
  lists:seq(1, N).

main(_) ->
  io:format("time: ~p~n", [timer:tc(generate_list, [1000])]).

Running this with “escript test.erl” would yield an error:

escript: exception error: undefined function test:generate_list/1
  in function  timer:tc/2 (timer.erl, line 179)

The issue is that the default escript mode is “interpreted” and not “compiled”. This leads to it not finding the function at all.

To fix the execution, one has to add the “-mode(compile).” line to the script, and also use the timer:tc/3 form of execution instead of the timer:tc/1 or timer:tc/2.

A fixed script of the above example would thus be:

#!/usr/bin/env escript
-mode(compile).

-module(test).

-export([generate_list/1]).

generate_list(N) ->
  lists:seq(1, N).

main(_) ->
  io:format("time: ~p~n", [timer:tc(?MODULE, generate_list, [1000])]).

I think the same should hold for apply calls too, or any function that uses the Module-Function-Arguments style of dynamic calls.

Written by Harsh

June 2nd, 2013 at 10:55 am

And they said…

leave a comment

Looking at the awesome activity lately on my blog:

Sorry.

Sorry.

Written by Harsh

May 22nd, 2012 at 6:21 pm

Posted in Personal

Gotta post something

2 comments

So here it is.

Wishing whoever reads this a better time ahead, as you replace those old calendars. And continue doing what you do best.

Written by Harsh

January 1st, 2012 at 2:38 pm

Posted in Personal

Tagged with

Running a local CGIProxy web proxy server on OSX

3 comments

What is CGIProxy?

It is slightly ancient today, but CGIProxy still appears to be one of the good web proxies (non-transparent, CGI-based proxy script) out there.

Also, being essentially a PERL script, its easy to set it up with an existing Apache2 install. Thankfully enough, Mac comes with its own Apache2 server well integrated and the work required to edit and configure it to run PERL scripts is trivial.

Some reasons why you might want to run CGIProxy locally would be to test a local website for how it handles session data, or to even use it generally to filter content over your network, etc.. I had the former need while working with Hadoop‘s web applications as part of what I do on a daily basis at Cloudera.

 

Here’s how to setup CGIProxy locally on your Mac OSX (or earlier/later):

Setup Apache2 to allow Perl and CGI execution

Over Applications – System Preferences – Sharing, disable ‘Web Sharing’ if it is running. This stops the Apache2 web server that may already be running.

System Preferences - Sharing

System Preferences - Sharing

Sharing - Disable Web Sharing before we configure Apache2

Sharing - Disable Web Sharing before we configure Apache2

With that out of the door, we should be able to edit out our Apache2 configs without any issue. With your favorite text editor, I use vim below, edit the file /etc/apache2/httpd.conf and append the following lines to it somewhere along the end:


AddHandler cgi-script .cgi

This would let Apache be able to interpret files ending with .pl as CGI-scripts.

Configure /etc/apache2/httpd.conf with Vim

Configure /etc/apache2/httpd.conf with Vim

The next steps is to configure and allow execution of such files via your user account, which we can do by editing yet another file named /etc/apache2/users/<Your username>.conf. For instance, on my setup, the file is called /etc/apache2/users/harsh.conf. Modify the ‘Options’ configuration to allow CGI Execution:


Options Indexes MultiViews ExecCGI

Configure /etc/apache2/users/"Username".conf with Vim

Configure /etc/apache2/users/"Username".conf with Vim

With that done as well, we’re now ready to setup CGIProxy.

Setting up CGIProxy

Fetch the CGIProxy tarball, which is about 170 KB strong. Extracting this would provide you a script called nph-proxy.cgi. Place it in the ~/Sites directory of your Home.

Place the CGIProxy script into the ~/Sites directory

Place the CGIProxy script into the ~/Sites directory

We’re done with installing CGIProxy.

Running CGIProxy

Now that we’re all set, we’ll just launch the Apache2 web server again and get to using the CGIProxy.

Head back to Applications – System Preferences – Sharing and enable ‘Web Sharing’ to run the server. Once up, you’ll get the public links to your homepage.

Enable Web Sharing under System Preferences - Sharing

Enable Web Sharing under System Preferences - Sharing

Head to <Your URL>/nph-proxy.cgi (For example, http://localhost/~harsh/nph-proxy.cgi) and you should see a properly functional, local CGIProxy ready for consumption. Hit it with a URL of choice and set some options you want it to use, and you’re off. Also look into the script with a text editor to see further configuration options.

CGIProxy in Chrome

CGIProxy in Chrome

Written by Harsh

October 1st, 2011 at 11:49 am

Posted in Personal

Committer on Apache Hadoop!

one comment

Just rolled in today:

Accepted as a committer on Apache Hadoop!

Woohoo! :D

Interested in becoming one too? Come spend good days with me at Cloudera. We’re hiring!

Mail me if you are interested, or for any questions you might have! :)

Written by Harsh

September 21st, 2011 at 1:32 am

Posted in Personal