<?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>Krues8dr.com</title>
	<atom:link href="http://krues8dr.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://krues8dr.com</link>
	<description>Personal website and programming notes of Bill Hunt</description>
	<lastBuildDate>Mon, 25 Mar 2013 13:17:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>New Year, New Theme</title>
		<link>http://krues8dr.com/blog/2013/01/21/new-year-new-theme/</link>
		<comments>http://krues8dr.com/blog/2013/01/21/new-year-new-theme/#comments</comments>
		<pubDate>Mon, 21 Jan 2013 15:22:56 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://krues8dr.com/?p=150</guid>
		<description><![CDATA[I pushed up a new theme design here on the site, using all the fun bells and whistles of SASS with Compass, some nice typography in Raleway courtesy of Google Web Fonts, and much more.  And if you&#8217;re wondering how I did &#8230; <a href="http://krues8dr.com/blog/2013/01/21/new-year-new-theme/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I pushed up a new theme design here on the site, using all the fun bells and whistles of <a title="SASS" href="http://sass-lang.com/"><strong>SASS</strong></a> with <a href="http://compass-style.org/" title="Compass"><strong>Compass</strong></a>, some nice typography in Raleway courtesy of <strong><a title="Raleway Font at Google Web Fonts" href="http://www.google.com/webfonts/specimen/Raleway">Google Web Fonts</a></strong>, and much more.  And if you&#8217;re wondering how I did that crazy pure-CSS zigzag drop on the top there, you can check out the source from <strong><a title="Krues8dr's GitHub Repo" href="https://github.com/krues8dr/krues8drdotcom-theme">my GitHub repo</a></strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2013/01/21/new-year-new-theme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache JMeter: Part 3 &#8211; Hacking JMeter to Customize the Listening Port on the Client</title>
		<link>http://krues8dr.com/blog/2011/03/22/apache-jmeter-part-3-hacking-jmeter-to-customize-the-listening-port-on-the-client/</link>
		<comments>http://krues8dr.com/blog/2011/03/22/apache-jmeter-part-3-hacking-jmeter-to-customize-the-listening-port-on-the-client/#comments</comments>
		<pubDate>Tue, 22 Mar 2011 17:45:11 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://krues8dr.com/?p=95</guid>
		<description><![CDATA[By default, there&#8217;s no way to set which port that the Apache JMeter client (master) listens on for the response of a test &#8211; and it always uses a random port instead! That means you have to open up your &#8230; <a href="http://krues8dr.com/blog/2011/03/22/apache-jmeter-part-3-hacking-jmeter-to-customize-the-listening-port-on-the-client/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>By default, there&#8217;s no way to set which port that the <strong>Apache JMeter client (master) listens on for the response of a test</strong> &#8211; and it <strong>always uses a random port</strong> instead!  That means you have to open up your firewall on ports <strong>45000-70000</strong> to run it, which sucks. It&#8217;s easy enough to patch, though!  Below, you can download my patched source to build your own copy:</p>
<p><a href="/files/jakarta-jmeter-2.4.patched.zip">JMeter 2.4 Patched Source</a>  </p>
<p>If you are using a newer version of JMeter than 2.4 (the latest, as of the time of writing this), directions follow for which files probably need to be updated.  If you&#8217;ve never built JMeter or a Java application from source, you are also in luck, as all the gory details are here, too!  <span id="more-95"></span></p>
<h3>Updating the necessary files</h3>
<p>In the code above, I&#8217;ve only changed a couple of small things, <a href="http://developersblog.espris.sk/2009/10/how-to-jmeter-ssh-tunnel.html">based on this useful post</a>.</p>
<ol>
<li>
<h4>Tell the listeners about the new port</h4>
<p>In <code>src/core/org/apache/jmeter/engine/ConvertListeners.java</code>, we need to let the listener know to respond on a given port.  We use the <code>JMeterUtils.getPropDefault</code> method to get the port from the configuration, so we need to tell the code to use the JMeterUtils library.  Go to the list of <code>import</code> statements at the top, and add this one to the list:</p>
<p><code><br />
import org.apache.jmeter.util.JMeterUtils<br />
</code></p>
<p>Then we just need to add the port as an argument to the Listener that&#8217;s created.  Replace this:</p>
<p><code><br />
RemoteSampleListener rtl = new RemoteSampleListenerImpl(item);<br />
</code><br />
with this:<br />
<code><br />
int callbackPort = JMeterUtils.getPropDefault("client.rmi.callbackport", 0);<br />
RemoteSampleListener rtl = new RemoteSampleListenerImpl(callbackPort, item);<br />
</code></p>
<li>
<h4>Change the Listener implementation to take the new port</h4>
<p>Now, all we have to do is update <code>RemoteSampleListenerImpl</code> to accept the new port.  In <code>src/core/org/apache/jmeter/samplers/RemoteSampleListenerImpl.java</code>, find the <code>RemoteSampleListenerImpl</code> definition and replace:</p>
<p><code><br />
public RemoteSampleListenerImpl(Object listener) throws RemoteException {<br />
        super();<br />
</code><br />
with:<br />
<code><br />
public RemoteSampleListenerImpl(int port, Object listener) throws RemoteException {<br />
         super(port);<br />
</code></li>
</ol>
<p>Once both of those are complete, you&#8217;re ready to compile!  Once you have the finished product, you can specify the custom listening port with the brand new, super shiny <code>client.rmi.callbackport</code> option in <code>jmeter.properties</code> file (it&#8217;s not in there by default, so you&#8217;ll have to add it by hand) or from the command line:</p>
<p><code><br />
jmeter -Jclient.rmi.callbackport=12345<br />
</code></p>
<h3>Building JMeter from source</h3>
<p>If you&#8217;re from the non-Java world, building JMeter may seem a bit weird.  It&#8217;s not just as simple as the traditional <code>apt-get</code> or <code>configure/make/make install</code>.  You&#8217;ll be doing all of the building with <code>ant</code>, with a heavy dose of manual labor along the way.</p>
<p>First thing first &#8211; you&#8217;re gonna need to install ant if it&#8217;s missing.  If you&#8217;re using a Fedora/Red Hat/CentOS box (including Amazon Linux!) you can do:<br />
<code><br />
yum install ant<br />
</code></p>
<p>You&#8217;ll also need some additional libraries for that, to add support for ReplaceRegExp:<br />
<code><br />
yum install ant-apache-oro.noarch<br />
</code></p>
<p>We&#8217;re going to assume you have java jvm and hopefully the java development kit installed, otherwise go hunt down the latest version from the internet &#8211; use yum or go digging <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">here</a>.</p>
<p>Now, by default, you should just be able to go into the base directory for the package and run <code>ant install</code>, but for me, a bunch of things failed immediately.  Here&#8217;s what I had to fix:</p>
<h4>xstream 1.3.1 has wrong source location</h4>
<p>I don&#8217;t know why it was released with a broken dependency location (and no forwarding), but I finally found xstream at <code>nexus.codehaus.org</code></p>
<p>in <code>build.properties</code> replace<br />
<code><br />
xstream.loc = http://repository.codehaus.org/com/thoughtworks/xstream/xstream/1.3.1<br />
</code><br />
with<br />
<code><br />
<nobr>xstream.loc = https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/xstream/1.3.1</nobr><br />
</code></p>
<h4>Missing dependencies</h4>
<p>I also found that you have to manually tell <code>ant</code> to go get any missing packages, which you do with this command:<br />
<code><br />
ant download_jars<br />
</code></p>
<h4>Can&#8217;t find javac</h4>
<p>While building, <code>ant</code> died because it couldn&#8217;t find a suitable compiler.  &#8220;But <code>javac</code> is installed!&#8221;, I said.  I had to check where java thought it&#8217;s home was:</p>
<p><code><br />
echo $JAVA_HOME<br />
&gt; /usr/lib/jvm/jre<br />
</code></p>
<p>Well, that&#8217;s not right!</p>
<p><code><br />
locate javac<br />
> /usr/lib/jvm/java-1.6.0-openjdk.x86_64/bin/<br />
</code></p>
<p>Ah ha!  We&#8217;ll just re-set the environment variable!  We use the <strong>directory above the <code>bin/</code>.</strong></p>
<p><code><br />
export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk.x86_64/<br />
</code></p>
<h4>Can&#8217;t find junit</h4>
<p>Next, ant determined that it needed <code>junit</code> for unit testing and it wasn&#8217;t installed.  This wasn&#8217;t handled by the previous download_jars installation, for reasons I don&#8217;t completely understand.</p>
<p><code><br />
BUILD FAILED<br />
/home/ec2-user/jakarta-jmeter-2.4/build.xml:911: Problem creating jar: /home/ec2-user/jakarta-jmeter-2.4/lib/junit/test.jar (No such file or directory) (and the archive is probably corrupt but I could not delete it)<br />
</code></p>
<p>So we go looking for where to get junit:</p>
<p><code><br />
yum provides junit<br />
> junit-3.8.2-6.5.3.amzn1.noarch : Java regression test package<br />
</code></p>
<p>It looks like amazon has built their own, but your system may tell you something different.  We can still do:<br />
<code><br />
yum install junit<br />
</code></p>
<h4>Still can&#8217;t build (can&#8217;t create junit dir?)</h4>
<p>Oh for eff&#8217;s sake.  <code>ant</code> can&#8217;t even create the necessary directory for the <code>junit</code> tests.</p>
<p><code><br />
mkdir ./lib/junit<br />
</code></p>
<p>Once all of that was done, I could finally run the command:<br />
<code><br />
ant install<br />
</code></p>
<p>And all was well.  Once you&#8217;ve had a successful build, you&#8217;ll need to <code>chmod</code> your newly-created binaries so that they can actually be run:</p>
<p><code><br />
chmod a+x ./bin/jmeter<br />
chmod a+x ./bin/jmeter-server<br />
</code></p>
<p>And then you should be ready to go!  run <code>./bin/jmeter</code> and the GUI should popup, letting you know that all is well!</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2011/03/22/apache-jmeter-part-3-hacking-jmeter-to-customize-the-listening-port-on-the-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache JMeter: Part 2 &#8211; Remote Testing Configuration</title>
		<link>http://krues8dr.com/blog/2011/03/20/apache-jmeter-part-2-remote-testing-configuration/</link>
		<comments>http://krues8dr.com/blog/2011/03/20/apache-jmeter-part-2-remote-testing-configuration/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 18:39:49 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://krues8dr.com/?p=100</guid>
		<description><![CDATA[Let&#8217;s say you&#8217;ve already gotten through the basics of JMeter and you&#8217;re ready to start setting up your testing. If you&#8217;re doing any sort of remote testing, you&#8217;ll inevitably need to know how to setup your client/server relationships. The vast &#8230; <a href="http://krues8dr.com/blog/2011/03/20/apache-jmeter-part-2-remote-testing-configuration/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say you&#8217;ve <a href="/2011/03/19/apache-jmeter-part-1-the-basics/">already gotten through the basics of JMeter</a> and you&#8217;re ready to start setting up your testing.  If you&#8217;re doing any sort of remote testing, you&#8217;ll inevitably need to know how to setup your client/server relationships. <span id="more-100"></span></p>
<p>The vast majority of JMeter&#8217;s configuration is done through a single file, the <code>jmeter.properties</code> file (which lives inside of the JMeter <code>bin/</code> directory).  Any of the properties in this file can be overridden by options on the command line &#8211; but since this is Java we&#8217;re talking about, the method is ridiculous (code for Linux/Mac):</p>
<p><code><br />
jmeter -J<em>propertyname1=value1</em> -J<em>propertyname1=value1</em><br />
</code></p>
<p>Of course, that doesn&#8217;t work for every option.  For instance, to tell the server that you want it to <strong>listen for the initial request on a port that&#8217;s not random</strong>, you have to do so as an environment variable set on the same line before you make the call to the server.  Truly and utterly bizarre:</p>
<p><code><br />
SERVER_PORT=1660 jmeter-server<br />
</code></p>
<p><strong>As noted below, the server_port only sets the port used for the initial request from the client to the server to begin testing, the response from the server to the client will be sent on a totally random port.  I also found that the setting above did not work for setting the listening port at all!</strong></p>
<h3>Server (Slave) Configuration</h3>
<p>The server configuration is ridiculously easy.  The server just does what it&#8217;s told, so you only have to tell it what port to listen on.  In the <code>jmeter.properties</code> file, you only need to set the <code>server.rmi.localport</code> value to whatever port you want.  (You can also set this from the command line using <code>-J</code>, see the example above.)  <strong>Note that you MUST set this option to keep the server from listening on a random port!</strong>  The jquery &#8220;default&#8221; is 1099.</p>
<p><strong>Also note that if you&#8217;re having trouble with the Client not being able to talk to the Server, you need to restart the Client after you&#8217;ve made any changes to the Server.  This is very counter-intuitive!</strong></p>
<p>Now, you may encounter a case where the server is trying to respond to the client (master) but looking up the <strong>wrong IP address</strong> for the hostname.  The fix is easy, simply add a record in your <a href="http://en.wikipedia.org/wiki/Hosts_(file)">hosts file</a> (often /etc/hosts) for the correct address.</p>
<p>Last, you should be aware that the server responds to the client&#8217;s request on a different port than the server_port specified &#8211; it will return test results on a <strong>random high-numbered port</strong> (45000-70000?).  If you haven&#8217;t opened up your firewall to account for this, it may cause the server to throw a nasty connection error.  <strong>Note that there is no way to set the response port by default in JMeter.</strong>  However, if you feel like getting your hands dirty, you can <strong><a href="/2011/03/22/apache-jmeter-part-3-hacking-jmeter-to-customize-the-listening-port-on-the-client/">hack the source to add this option</a></strong></p>
<h3>Client (Master) Configuration</h3>
<p>The client configuration is only a little more difficult.  You&#8217;ll need to set the <code>server_port</code> to the same value you used for the server, again in the <code>jmeter.properties</code> file.  You&#8217;ll also need to set the <code>remote_hosts</code> &#8211; this should be a comma-separated list of all of the servers&#8217; hostnames or IP addresses.  For example:</p>
<p><code><br />
remote_hosts=ec2-50-17-92-85.compute-1.amazonaws.com,ec2-50-17-94-90.compute-1.amazonaws.com<br />
</code></p>
<p>That&#8217;s it!  Once you&#8217;ve got that all setup, you should be able to start jmeter and have the list of servers appear under the <code>Run &gt; Remote Start</code> menu. Note that this does not start up the servers &#8211; you&#8217;ll still need to start them manually on each machine &#8211; it merely runs the current test on the already-running servers.  You can also use <code>Run &gt; Remote Start All</code> to run the current test on all servers at once.</p>
<p>As a final note, you might need to take a look at <code>jmeter.log</code>, also under the <code>JMeter bin/ directory</code> to debug anything that&#8217;s not working correctly.  There are often a few useful messages in there to help you along the way.</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2011/03/20/apache-jmeter-part-2-remote-testing-configuration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache JMeter: Part 1 &#8211; The Basics</title>
		<link>http://krues8dr.com/blog/2011/03/19/apache-jmeter-part-1-the-basics/</link>
		<comments>http://krues8dr.com/blog/2011/03/19/apache-jmeter-part-1-the-basics/#comments</comments>
		<pubDate>Sat, 19 Mar 2011 17:22:42 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Load Testing]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://krues8dr.com/?p=86</guid>
		<description><![CDATA[Recently, I&#8217;ve been doing a bit of load testing on Amazon AWS to determine how much abuse our web application can take without killing the server. I&#8217;ve been attempting to use Apache JMeter to do the hard part, but came &#8230; <a href="http://krues8dr.com/blog/2011/03/19/apache-jmeter-part-1-the-basics/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Recently, I&#8217;ve been doing a bit of <strong>load testing</strong> on <strong>Amazon AWS</strong> to determine how much abuse our web application can take without killing the server.  I&#8217;ve been attempting to use <a href="http://jakarta.apache.org/jmeter/" title="Apache JMeter">Apache JMeter</a> to do the hard part, but came up against a slew of problems.  The documentation provided seems targetted at dyed-in-the-wool Java developers (that &#8220;J&#8221; at the beginning is clearly a warning shot), and makes pretty big assumptions about the knowledge of the audience.  Here are the basic concepts of how to get started using it, targeted for us LAMP developers.<br />
<span id="more-86"></span></p>
<p>The first thing to understand is that there are two main to go about using Jmeter.  By default, Jmeter runs as a free-standing (GUI) application on which you run Tests directly from the machine it&#8217;s running on.  You do this with the <code>Run &gt; Start</code> option.</p>
<p>You can also, however set it up to run on other machines, reporting the results back to the original GUI.  In JMeter terms, the Master from which you send the tests is called the Client, and the Slaves that run the tests are the Hosts.  You have to configure which hosts to run on &#8211; afterward you can use <code>Run &gt; Remote Start &gt; <em>slave address</em></code> to run the test on a single machine, or <code>Run &gt; Remote Start All</code> to run on all slaves.</p>
<p>To get started, try running JMeter on your local machine, and writing a basic test.  If you&#8217;re working locally, just running the bin/jmeter (or bin/jmeter.sh on Mac, bin/jmeter.bat on Windows) script should start up a java session and run the program on your machine.  If you&#8217;re working remotely, Jmeter runs in an X-Windows environment &#8211; so if you&#8217;re on a Mac you&#8217;ll need to have <a href="http://www.simplehelp.net/2006/10/22/how-to-install-x11-in-os-x/">X11</a> installed and running.</p>
<h3>Writing Tests</h3>
<p>There&#8217;s actually very good <a href="http://jakarta.apache.org/jmeter/usermanual/jmeter_proxy_step_by_step.pdf">documentation on setting up a test by recording your actions clicking through a site</a>, but here&#8217;s the short version.  To create a new test, you really only need a few elements.  </p>
<p><strong>Note:</strong> adding elements (Nodes) to a test is tricky because the elements are all <em>context-sensitive</em>.  If you haven&#8217;t selected the right parent in the list, you won&#8217;t be able to add certain children.  I&#8217;ve listed the correct element to click on as the parent below.</p>
<ul>
<li>A <strong>Thread Group</strong>.  Used to set the number of virtual users (Threads) and number of Loops (iterations, obv) for *each* slave to perform (if you&#8217;re using the local machine) Click on the <strong>Test Plan</strong> and then chose <code>Add &gt; Threads (Users) &gt; Thread Group</code> from the <code>Edit</code> menu or the right-click contextual menu.  For the trial run, you might just give it 1 <code>User</code>, 1 for <code>Ramp Up Period</code> and 1 <code>Loop</code></li>
<li>A <strong>Listener</strong> of some type.  This is the what shows you the results of the test, either by a chart, table, or other medium. The simplest one is probably the <strong>Summary Report</strong> &#8211; to add it, click on the <strong>Thread Group</strong> and choose <code>Add &gt; Listener &gt; Summary Report</code> from the menu. No additional configuration is necessary for this type of <strong>Listener</strong>.</li>
<li>A <strong>Sampler</strong> &#8211; an actual test element.  For instance, if you want to just grab one page off of a site to see if it&#8217;s working, you&#8217;ll add an <strong>HTTP Request</strong> Sampler.  Click on the <strong>Thread Group</strong> again, then choose <code>Add &gt; Sampler &gt; HTTP Request</code>.  We&#8217;ll want to test with a site we know is working first, so enter <em>google.com</em> for the <code>Server Name or IP</code>.</li>
</ul>
<p>Once all that is entered, you can choose to Start your test locally through the <code>Run &gt; Start</code> option.  If you have not saved your test, JMeter will prompt you to do so.  After that, if you click on the <strong>Summary Report</strong> you should see that there has been 1 Sample, with (hopefully) an Error % of 0.  If you&#8217;re getting an Error % greater than 0, you should probably check that you&#8217;re properly connected to the internet and you&#8217;ve followed all the steps correctly.</p>
<p>Now that they system is working, you can try entering your own domain in, and maybe enter a <strong>Path</strong> of a particular page that you want to test.  <strong>Note:</strong> be <strong>very careful</strong> when testing against your live site.  Increasing your thread or loops too high can cause the server to stop responding. (Which is what we want to test in the first place!)  It&#8217;s best to perform your load testing against a non-production machine.  Like, say, one set up on Amazon AWS.</p>
<p>From here, this would be a good time to read that above <a href="http://jakarta.apache.org/jmeter/usermanual/jmeter_proxy_step_by_step.pdf">article on setting up a test by recording your actions with a proxy server</a>, to create more complicated an thorough tests.</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2011/03/19/apache-jmeter-part-1-the-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Defensive Programming</title>
		<link>http://krues8dr.com/blog/2010/04/09/defensive-programming/</link>
		<comments>http://krues8dr.com/blog/2010/04/09/defensive-programming/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 19:51:42 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.krues8dr.com/?p=79</guid>
		<description><![CDATA[As a web developer, the greater part of my job is not creating new apps, but hacking together disparate software packages into Frankensteinian amalgamations that (supposedly) work together seamlessly.  This is universally a headache, as the original authors tend to &#8230; <a href="http://krues8dr.com/blog/2010/04/09/defensive-programming/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>As a web developer, the greater part of my job is not creating new apps, but hacking together disparate software packages into Frankensteinian amalgamations that (supposedly) work together seamlessly.  This is universally a headache, as the original authors tend to write code thinking that their app is the only one that will be installed.  <a href="http://wordpress.org">WordPress</a>, <a href="http://vanillaforums.org/">Vanilla</a>, and <a href="http://www.interspire.com/emailmarketer/">Interspire&#8217;s Email Marketer</a> are some of the worst offenders that I  struggle with regularly.</p>
<p>When coding your own brilliant application, there are a few simple things you can do to avoid potential collisions and headaches later, especially if anyone else will be using your code.  Here are a few areas to pay attention to.<span id="more-79"></span></p>
<h3>Namespace</h3>
<p>First and foremost, you need to watch out for collisions.  If you&#8217;re not using a language with built-in namespacing (e.g. PHP &lt;5.3), you&#8217;ll need to manage this manually. Some areas that you need to watch out for are:</p>
<ul>
<li>Class Names</li>
<li>Session Variables</li>
<li>Local Variables</li>
<li>Constants &amp; Globals</li>
<li>Database Tables</li>
</ul>
<p>Most databases already have a &#8220;users&#8221; table somewhere, and an app of any  size is likely to have a variable named &#8220;args&#8221; or &#8220;params&#8221; (or two, or  ten&#8230;). For most cases, it&#8217;s usually enough to prefix your names with your application name.  Keeping your names verbose helps, too.</p>
<h3>Program Flow</h3>
<p>When writing code, it&#8217;s always a good idea to keep everything to small, reusable functions.  This is especially true of published apps, because your users are very likely to be using your code in ways that your original app is not.  Try to break things down to the smallest possible chunks, even if it looks pedantic in your application.  For instance, break up your createUser() function into separate functions to add the user to the user table, subscribing the user to an email list, adding the user to the default group, etc.</p>
<p>Assume that your code will be executing inside of someone else&#8217;s code.  Try not to use print and echo statements when you can simply pass returned values &#8211; only print as the final step.  (An easy way to fake this is to use output buffering.)  You never know where your output is going anyway &#8211; so don&#8217;t assume that it will be a particular format &#8211; it may end up as HTML, an email body, or in the error log depending on how it&#8217;s implemented.</p>
<p>Pay special attention to any implicit defaults or rules that your code expects.  Don&#8217;t force the code to expect complicated series of objects or parameters that any one else wouldn&#8217;t immediately understand.  Don&#8217;t rely on database restrictions to impose your business rules.</p>
<p>Assume that your code will be glued into an existing user system at some point &#8211; make your user system as user-friendly as possible.  Create big wide hooks that anyone can use later to interact with your user system.  Actually, do this for everything.  (Ok, WordPress, you got that one right at least.)</p>
<p>Keep things wrapped up in nice containers.  Don&#8217;t just leave large procedural chunks lying around for others to trip over.  Don&#8217;t forget to give some attention to your configuration files on this front.</p>
<p><strong>Don&#8217;t use globals.  Don&#8217;t use globals.  For the love of God, don&#8217;t use globals.  I don&#8217;t care how clever you think you are (I&#8217;m looking at you, <a href="http://wordpress.org">WordPress</a>), you&#8217;re going to screw everyone else up if you use globals.</strong></p>
<h3>Don&#8217;t Step On Toes</h3>
<p>When you&#8217;re managing your resources, it&#8217;s a good idea to be courteous of others.  If you&#8217;re using a database connection or local file, keep a copy of the handle around instead of relying on the implicit &#8220;last opened&#8221; scheme many languages offer.  And since you&#8217;re all such brilliant developers, I don&#8217;t have to remind you to make sure to clean up after yourself &#8211; closing any connections that you opened, deleting any temporary files you&#8217;ve created, and cleaning up any objects you&#8217;re done using.  Even better, write error handling into all of your code so these things are done automatically even if something fails!</p>
<p>One particularly abused area is Session/Cookie management.  I cannot begin to list the number of applications that hijack the session and fill/clear it wantonly.  In general, you should never be destroying a session, or blanking out the entire cookie.  Always sandbox off your content into a hash (using namespaces again), so that you can remove only the content you added.  Also, don&#8217;t ever set the session name directly &#8211; just use the default.  (At least, in PHP you can&#8217;t use two different sessions simultaneously &#8211; setting the session name removes the ability to use any other session).</p>
<h3>In conclusion&#8230;</h3>
<p>Do be a considerate programmer.  Do keep good fences (as they make for good neighbors).  Don&#8217;t build giant monoliths (as they attract groups of violent monkeys).  Stick to these rules and you&#8217;ll save everyone a lot of trouble in the long run.</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2010/04/09/defensive-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scriptaculous / Prototype IE 8 Autocomplete disappearing problem</title>
		<link>http://krues8dr.com/blog/2009/10/30/scriptaculous-prototype-ie-8-autocomplete-disappearing-problem/</link>
		<comments>http://krues8dr.com/blog/2009/10/30/scriptaculous-prototype-ie-8-autocomplete-disappearing-problem/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 19:29:55 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.krues8dr.com/?p=68</guid>
		<description><![CDATA[In Internet Explorer 8, it seems that Scriptaculous / Prototype sometimes miscalculate exactly where to place the autocomplete box. As a result, it will usually not show up at all, since it&#8217;s off the screen. The trick here is that &#8230; <a href="http://krues8dr.com/blog/2009/10/30/scriptaculous-prototype-ie-8-autocomplete-disappearing-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In <strong>Internet Explorer 8</strong>, it seems that <strong>Scriptaculous</strong> / <strong>Prototype</strong> sometimes miscalculate exactly where to place the <strong>autocomplete box</strong>.  As a result, it will usually <strong>not show up at all</strong>, since it&#8217;s off the screen.  The trick here is that it&#8217;s calculating a left and top absolute position that are wrong, and then writing them directly to the element as inline styles.  </p>
<p>The solution, though a bit of a hack, is to write styles for the div container that use the <strong>!important rule</strong>, which will override any inline styles.  Here&#8217;s what the fix should look like:</p>
<p><code><br />
#my_auto_complete {<br />
    position: relative !important;<br />
    top: -10px !important;<br />
    left: 0px !important;<br />
}<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2009/10/30/scriptaculous-prototype-ie-8-autocomplete-disappearing-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS: Spriting Doors</title>
		<link>http://krues8dr.com/blog/2009/08/11/css-spriting-doors/</link>
		<comments>http://krues8dr.com/blog/2009/08/11/css-spriting-doors/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 13:38:49 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.krues8dr.com/?p=57</guid>
		<description><![CDATA[When we redesigned hotelicopter, I had to spend a lot of time cutting up images for a bunch of buttons and boxes. I was using the now-standard Sliding Doors technique, so that we could have flexible boxes. Today, I realized &#8230; <a href="http://krues8dr.com/blog/2009/08/11/css-spriting-doors/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>When we redesigned <a href="http://hotelicopter.com">hotelicopter</a>, I had to spend a lot of time cutting up images for a bunch of buttons and boxes.  I was using the now-standard <a href="http://www.alistapart.com/articles/slidingdoors/">Sliding Doors</a> technique, so that we could have flexible boxes.  Today, I realized that I could combine this technique with another <a href="http://www.alistapart.com/">A List Apart</a> favorite, <a href="http://www.alistapart.com/articles/sprites">image sprites</a>, so that you can have sliding doors <strong>using only one image</strong>.  <span id="more-57"></span></p>
<p>Effectively, you create your a background image the maximum width that you expect the box to be, as you would do with <strong>sliding doors</strong>.  However, instead of cutting off the left side (or right side, as you prefer), you keep the image intact. Instead, you simply align the image to the left on the outside and the right on the inside, leaving enough padding for the image edges to show through.</p>
<p><img src="http://www.krues8dr.com/wp-content/uploads/2009/08/spritingdoors1.png" alt="spritingdoors1" title="spritingdoors1" width="170" height="71" /></p>
<p><a href="http://www.krues8dr.com/files/spriting_doors/">Here&#8217;s an example of the technique in action.</a><br />
Update: Example now works in FF3, FF3.5, IE7, IE8 and Safari 3 and 4!</p>
<p>The only caveat here is that this won&#8217;t work for images with transparency, and only saves you one download (ostensibly).  This technique could be expanded, however, to have the hover states of the button in the image as well (as in the sprites technique), or for all four pieces of the image, used in <strong>rounded corners</strong> and <strong>custom border</strong> styles.  I wouldn&#8217;t recommend either of those for particularly large boxes, though, as you&#8217;d need a really large image for those, which doesn&#8217;t optimize well.</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2009/08/11/css-spriting-doors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Symfony + Doctrine on the command line using the wrong database</title>
		<link>http://krues8dr.com/blog/2009/06/16/symfony-doctrine-on-the-command-line-using-the-wrong-database/</link>
		<comments>http://krues8dr.com/blog/2009/06/16/symfony-doctrine-on-the-command-line-using-the-wrong-database/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 14:16:34 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.krues8dr.com/?p=52</guid>
		<description><![CDATA[So, one us pilots was trying to use Doctrine migrations to update a database on one of our servers. However, Doctrine was sternly refusing to use the correct database, as configured in the database.yml file. As it turns out, using &#8230; <a href="http://krues8dr.com/blog/2009/06/16/symfony-doctrine-on-the-command-line-using-the-wrong-database/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>So, one us <a href="http://www.hotelicopter.com/team">pilots</a> was trying to use <strong>Doctrine migrations</strong> to update a database on one of our servers.  However, Doctrine was sternly refusing to use the correct database, as configured in the <strong><code>database.yml</code></strong> file.  As it turns out, using <strong>Symfony</strong> from the command line skips the usual route through the <code>/web/yourapplication.php</code> file (e.g. <code>backend.php</code> or <code>frontend.php</code>).  As a result, the environment is not properly set when reading the <code>database.yml</code> file, and instead the last database connection specified is used.  Lame.  The trick is to specify the environment from the command line, so this file (and the other config files) do what they&#8217;re supposed to:</p>
<p><code><br />
symfony doctrine:migrate --env=staging frontend 119<br />
</code></p>
<p>where &#8220;staging&#8221; is whatever the environment is you want to use (to match the name in the <code>database.yml</code> file).</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2009/06/16/symfony-doctrine-on-the-command-line-using-the-wrong-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Symfony / Doctrine &#8211; update a record using models</title>
		<link>http://krues8dr.com/blog/2009/06/09/symfony-doctrine-update-a-record-using-models/</link>
		<comments>http://krues8dr.com/blog/2009/06/09/symfony-doctrine-update-a-record-using-models/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 18:20:16 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://www.krues8dr.com/?p=50</guid>
		<description><![CDATA[The Doctrine manual is really, really confusing in places. If you want to do something as simple as updating a record, the examples suggest that you use Doctrine_Query::create(). This doesn&#8217;t make a lot of sense, because we only want to &#8230; <a href="http://krues8dr.com/blog/2009/06/09/symfony-doctrine-update-a-record-using-models/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The <a href="http://www.doctrine-project.org/documentation/manual/1_1/en" title="Doctrine ORM">Doctrine manual</a> is really, really confusing in places.  If you want to do something as simple as updating a record, the examples suggest that you use <code>Doctrine_Query::create()</code>.  This doesn&#8217;t make a lot of sense, because we only want to manipulate the model, we shouldn&#8217;t have to even look at a query.<span id="more-50"></span>  Assuming you have the primary id of the record in question, you can do the following to easily modify a record:</p>
<p><code><br />
$myobj = new Doctrine::getTable('MyObj')->find($request->getParameter('my_id'));<br />
$myobj->property = 'New Value';<br />
$myobj->save();<br />
</code></p>
<p>This is really straightforward, but if you don&#8217;t include the <code>find()</code> inline with the <code>getTable()</code> call, you&#8217;ll get an obscure <strong>&#8220;Unknown method saveTableProxy&#8221;</strong> error, which there seems to be no documentation for anywhere on the internet.  It only took a couple of hours of trial and error to get this figured out.  </p>
<p>In general, I&#8217;d still recommend Doctrine as an ORM, but the lack of consistent and clear documentation, with dreadfully obscure object methods, make for a very steep learning curve.</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2009/06/09/symfony-doctrine-update-a-record-using-models/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP SimpleTest Unit Testing &#8211; Expecting Exceptions and Errors</title>
		<link>http://krues8dr.com/blog/2009/06/01/php-simpletest-unit-testing-expecting-exceptions-and-errors/</link>
		<comments>http://krues8dr.com/blog/2009/06/01/php-simpletest-unit-testing-expecting-exceptions-and-errors/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 21:12:03 +0000</pubDate>
		<dc:creator>Krues8dr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.krues8dr.com/?p=46</guid>
		<description><![CDATA[Like a good programmer, I try to be good about unit testing. And also as a good programmer, I throw errors in my PHP where appropriate. I just learned today after a bit of digging through the codebase, that SimpleTest &#8230; <a href="http://krues8dr.com/blog/2009/06/01/php-simpletest-unit-testing-expecting-exceptions-and-errors/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Like a good programmer, I try to be good about unit testing.  And also as a good programmer, I throw errors in my PHP where appropriate.  I just learned today after a bit of digging through the codebase, that <a title="SimpleTest from lastcraft" href="http://www.lastcraft.com/simple_test.php">SimpleTest</a> can be told to expect an Exception (or error) to be thrown in the test. <span id="more-46"></span></p>
<p>When using <code>trigger_error()</code>, you can use the <code>expectError()</code> as follows:<br />
<code><br />
	$this->expectError( $errorMessageToExpect, 'My message about this test case' );<br />
	my_code_that_causes_an_error();<br />
</code></p>
<p>Note that there is an internal <strong>queue of errors</strong> in SimpleTest, so it will expect precisely one error for each call to <code>expectError()</code>.  </p>
<p>If, however, you&#8217;re using <code>throw</code> you use <code>expectException</code> instead:<br />
<code><br />
	$this->expectException( $exceptionclass, 'My message about this test case' );<br />
	my_code_that_throws_an_exception_of_type_exceptionclass();<br />
</code></p>
<p>You can specify the class of exception to expect in the first parameter &#8211; the usual type would be <code>Exception</code>, of course.  If you set the first parameter to <code>false</code>, it defaults to this type.</p>
]]></content:encoded>
			<wfw:commentRss>http://krues8dr.com/blog/2009/06/01/php-simpletest-unit-testing-expecting-exceptions-and-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
