<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>/contrib/famzah &#187; Development</title>
	<atom:link href="http://blog.famzah.net/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.famzah.net</link>
	<description>Enthusiasm never stops</description>
	<lastBuildDate>Thu, 02 Feb 2012 15:36:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.famzah.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>/contrib/famzah &#187; Development</title>
		<link>http://blog.famzah.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.famzah.net/osd.xml" title="/contrib/famzah" />
	<atom:link rel='hub' href='http://blog.famzah.net/?pushpress=hub'/>
		<item>
		<title>DIY temperature and humidity wireless data logger</title>
		<link>http://blog.famzah.net/2011/12/26/diy-temperature-and-humidity-wireless-data-logger/</link>
		<comments>http://blog.famzah.net/2011/12/26/diy-temperature-and-humidity-wireless-data-logger/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 16:56:50 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[ATmega168]]></category>
		<category><![CDATA[atmel avr]]></category>
		<category><![CDATA[DIY]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[humidity]]></category>
		<category><![CDATA[PCB]]></category>
		<category><![CDATA[SHT11]]></category>
		<category><![CDATA[temperature]]></category>
		<category><![CDATA[wireless]]></category>
		<category><![CDATA[XBee]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=1078</guid>
		<description><![CDATA[Monitoring and controlling relative humidity is important for humans health. Too low or too high humidity feels uncomfortable, but most importantly high moisture is a factor for growing mold in your home, which could be health threatening (according to EPA and CDC). I will not go into details on how to control humidity. Instead I&#8217;ll [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1078&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Monitoring and controlling <a href="http://en.wikipedia.org/wiki/Relative_humidity">relative humidity</a> is important for humans health. Too low or too high humidity feels uncomfortable, but most importantly high moisture is a factor for growing mold in your home, which could be health threatening (according to <a href="http://www.epa.gov/mold/moldresources.html">EPA</a> and <a href="http://www.cdc.gov/mold/dampness_facts.htm">CDC</a>). I will not go into details on how to control humidity. Instead I&#8217;ll describe what motivated me to design and create my own temperature and humidity sensor which reports its readings every minute to a central Linux server.</p>
<p><a href="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg"><img src="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg?w=450&#038;h=337" alt="" title="Sensor with XBee (wireless)" width="450" height="337" class="aligncenter size-full wp-image-1089" /></a></p>

<a href='http://blog.famzah.net/2011/12/26/diy-temperature-and-humidity-wireless-data-logger/with-xbee/' title='Sensor with XBee (wireless)'><img data-attachment-id='1089' data-orig-size='1024,768' data-liked='0'width="150" height="112" src="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg?w=150&#038;h=112" class="attachment-thumbnail" alt="Sensor with XBee (wireless)" title="Sensor with XBee (wireless)" /></a>
<a href='http://blog.famzah.net/2011/12/26/diy-temperature-and-humidity-wireless-data-logger/only-usb/' title='Sensor without XBee (only USB)'><img data-attachment-id='1091' data-orig-size='1024,768' data-liked='0'width="150" height="112" src="http://famzah.files.wordpress.com/2011/12/only-usb.jpg?w=150&#038;h=112" class="attachment-thumbnail" alt="Sensor without XBee (only USB)" title="Sensor without XBee (only USB)" /></a>
<a href='http://blog.famzah.net/2011/12/26/diy-temperature-and-humidity-wireless-data-logger/schematics/' title='Schematics'><img data-attachment-id='1093' data-orig-size='1782,1270' data-liked='0'width="150" height="106" src="http://famzah.files.wordpress.com/2011/12/schematics.png?w=150&#038;h=106" class="attachment-thumbnail" alt="Schematics" title="Schematics" /></a>
<a href='http://blog.famzah.net/2011/12/26/diy-temperature-and-humidity-wireless-data-logger/board/' title='PCB board'><img data-attachment-id='1094' data-orig-size='447,193' data-liked='0'width="150" height="64" src="http://famzah.files.wordpress.com/2011/12/board.png?w=150&#038;h=64" class="attachment-thumbnail" alt="PCB board" title="PCB board" /></a>

<p>The main requirements for my design were the following:</p>
<ul>
<li>Affordable price, as I wanted to install four sensors.</li>
<li>Great accuracy both for temperature and humidity readings.</li>
<li>Over-the-air communication, as I wanted to be able to install a sensor even in my bathroom, where I can&#8217;t run data or power wires. Support for wired communication too, so that we can reduce the overall price by not installing the wireless module.</li>
<li>Data logging to a computer, because both temperature and humidity change with time, for example when you sleep in the room, and you can&#8217;t look at a mechanical temperature or humidity meter every minute, in order to write down the results.</li>
<li>Battery operated, in order to avoid any wiring.</li>
<li>Open-source hardware and software toolchain, so I chose Atmel AVR microcontrollers. I got sick of Microchip and their commercial C compilers.</li>
<li>To have fun with electronics but at the same time create the device as fast as possible, as free time turned out to be a pretty limited resource recently.</li>
</ul>
<p>I managed to accomplish most of the requirements I set with two exceptions: the device operates only a month on batteries, and cumulatively I spent almost a week to design, solder, develop the firmware, and test the device. Now all the sensors operate from a wall-plug power adapter, and my hunger for environmental control in my house is satisfied.</p>
<p>I&#8217;ll now try to describe the whole process and the reasons behind my engineer decisions. Note that I&#8217;m an amateur hobbyist.  </p>
<p><u><strong>Idea and requirements</strong></u><br />
I wrote down all my thoughts in a text editor. Then re-designed all the sticky notes into requirements, and did so a few more times, in order to finally decide what I want to design and not get distracted by new random ideas in my head.</p>
<p><u><strong>Power supply</strong></u><br />
I wanted the device to be able to operate both via USB, and thus be powered by 5V, as well as to be powered by an accumulator or a battery with an input voltage up to 12V, so that it could be used in a car too. I put a polarity protection diode D1 in series with the power line, so that an accidental polarity mismatch doesn&#8217;t burn out the power regulator. Such a protection diode must have very low voltage drop and thus low power loss, and the Schottky diode <a href="http://www.datasheetcatalog.com/datasheets_pdf/1/N/5/8/1N5819.shtml">1N5819</a> seemed like a good match. </p>
<p>Operating from a battery also means that the voltage regulator must be extremely efficient and with a low bias current consumption, which means that it should draw almost nothing while there is nothing connected to it at its output as a load. Most battery operated devices &#8220;sleep&#8221; during most of their life cycle, so their consumption is close to zero. I used the ultra low-dropout fixed voltage regulator <a href="http://www.national.com/pf/LP/LP2986.html">LP2986-33</a>, marked as U1 in the schematics. The whole circuit operates at 3.3V because of the XBee wireless modules, and also because operating at a lower voltage usually gives lower power consumption.</p>
<p>Since we can have two different power sources, there must be a way to choose which one is active. You can switch between the power sources using the PWR_SELECT jumpers.</p>
<p><u><strong>Wired communication via USB</strong></u><br />
I wanted to have the option to use the sensors by directly connecting them to a computer. This way we could save the money for an XBee wireless module. I used the classical USB-to-Serial solution <a href="http://www.ftdichip.com/Products/ICs/FT232R.htm">FT232R</a>, which is also quite inexpensive and requires almost no external components. You can see it in the schematics as U2. Note that the I/O lines of FT232R must be configured to operate at 3.3V too. This is done by connecting pin 17, which is the internal 3.3V regulator of FT232R, to pin 4. The internal 3.3V regulator is not used for anything else, and in theory I could have powered the I/O lines, pin 4, directly from the main voltage regulator U1.</p>
<p><a href="http://famzah.files.wordpress.com/2011/12/only-usb.jpg"><img src="http://famzah.files.wordpress.com/2011/12/only-usb.jpg?w=150&#038;h=112" alt="" title="Sensor without XBee (only USB)" width="150" height="112" class="aligncenter size-thumbnail wp-image-1091" /></a></p>
<p><u><strong>Wireless communication</strong></u><br />
The <a href="http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/zigbee-mesh-module/xbee-zb-module#overview">XBee modules</a> is something I wanted to play with for a long time. They seem very easy to work with and are packed with all kind of features. Though in my case I&#8217;m not using almost any of them, not even the AES encryption which could secure the data channel. I&#8217;m using the Series 1 XBee low-power embedded RF modules (XB24), which have a power of 1 mW and 30 m indoor range. There are many comments in Internet that the indoor range of the XBee modules is poor and I can confirm that. The range really depends on what the signal must travel through. Sometimes you lose the link even through one wall, sometimes it can go through a few walls. The <a href="http://ftp1.digi.com/support/images/XST-AN019a_XBeeAntennas.pdf">XBee &amp; XBee-PRO OEM RF Module Antenna Considerations</a> is a great article by the XBee manufacturers. After all, probably by using such a low-power module, we shouldn&#8217;t expect so great results. It works well in my apartment though &#8212; all rooms report to the central XBee module successfully. On the server&#8217;s side, the receiver, I first had an XBee with chip antenna, which I replaced with an XBee-PRO with whip antenna. This made no difference.</p>
<p>Wiring the XBee module is very easy. It requires no external components. If you read the PDF datasheet, you&#8217;ll see how many great features an XBee has. I&#8217;m using only three of them:</p>
<ul>
<li>Sleep mode &#8212; the microcontroller puts the XBee to sleep by controlling the SLEEP_RQ pin 9.</li>
<li>Networking addressing &#8212; each XBee is configured with a unique address, so that the receiver on the server side knows which reading belongs to which sensor probe.</li>
<li>API operation &#8212; the receiver XBee module operates in an API mode, which is a frame-based protocol that provides greater flexibility and more control. For example, besides the received data payload, an API frame gives information about the sender&#8217;s address and the signal quality.</li>
</ul>
<p><a href="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg"><img src="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg?w=150&#038;h=112" alt="" title="Sensor with XBee (wireless)" width="150" height="112" class="aligncenter size-thumbnail wp-image-1089" /></a></p>
<p><u><strong>Temperature and humidity sensor</strong></u><br />
I wanted to interface the sensor directly using a digital protocol, so that we can minimize the ADC stuff and errors. The <a href="http://www.sensirion.com/en/01_humidity_sensors/02_humidity_sensor_sht11.htm">SHT11</a> turned out to be the sensor I was looking for:</p>
<ul>
<li>Relative humidity accuracy: +/- 3% in the range 20% to 80% RH. The sensor comes fully calibrated.</li>
<li>Temperature accuracy: +/- 1.5 degrees Celsius in the range -15 to +65 degrees Celsius.</li>
<li>Digital two-wire interface.</li>
<li>Very low energy consumption: 80uW (at 12bit, 3V, 1 measurement/s).</li>
</ul>
<p>The SHT11 is a bit pricey but works very easily and accurately out of the box, so I decided to go with it. There is a very good alternative at <a href="http://www.sparkfun.com/">Sparkfun</a> &#8212; the <a href="http://www.sparkfun.com/products/10167">RHT03</a> humidity and temperature sensor (also known as &#8220;RHT-22&#8243;). There were some contradictive comments by Sparkfun users &#8212; some say it works very well, some doubt its accuracy. I haven&#8217;t tried it but have left space JP7 on the current board, so that at some later time I could solder one RHT03 and use it with the existing schematics.</p>
<p>One note about the SHT11 two-wire interface. Definitely use a pull-up resistor on the DATA wire, as advised in the PDF! I tried to do some magic by the microcontoller and failed. With the exception of the pull-up resistor, everything else worked with no other problems with the SHT11 sensor. The manufacturer Sensirion provides <a href="http://www.sensirion.com/en/pdf/product_information/Sample_Code_humidity_sensor_SHTxx.pdf">Sample code for the SHTxx sensors</a> which turned out to be very useful. I was able to re-code it for the AVR GNU C compiler in a couple of minutes.</p>
<p>The <a href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check">CRC</a> calculation got me a bit confused. There are multiple different ways to calculate a CRC checksum, and they all provide different results. Each CRC calculation depends on the selected CRC polynomial, which is something like a bit-mask that defines the algorithm for the CRC calculation. After lots of struggle, I finally found an excellent <a href="http://ghsi.de/CRC/">Online CRC Calculation</a> web wizard, which also includes a hardware implementation example, and sample C and VERILOG implementations, which you can copy-paste in your program. Thank you Kay Gorontzi!</p>
<p><u><strong>Microcontroller</strong></u><br />
Initially I worked with <a href="http://www.atmel.com/dyn/products/product_card.asp?part_id=2004">ATmega8</a>. Then I switched to <a href="http://www.atmel.com/dyn/products/product_card.asp?part_id=3303">ATmega168</a> because of the much lower power consumption. I could have used any other Atmel AVR microcontroller which has USART, internal oscillator, and sleep mode. Though ATmega8 or ATmega168 are always available in my local electronics shop, so I chose one of them. Besides the lower power consumption, ATmega168 has one other major advantage for my application &#8212; the watchdog timer can wake the chip from sleep mode and directly execute an interrupt, thus not re-starting the program from the very beginning.</p>
<p><u><strong>Firmware</strong></u><br />
I&#8217;m working on Windows 7 64-bit and used a <a href="http://www.fischl.de/usbasp/">USBasp programmer</a> to download the code into the microcontroller. The whole development toolchain is packaged into the <a href="http://winavr.sourceforge.net/">WinAVR</a> suite. It includes the <a href="http://www.nongnu.org/avr-libc/">AVR GCC</a> compiler and the <a href="http://www.nongnu.org/avrdude/">avrdude</a> programmer. I also downloaded a <a href="http://www.hookersandbooze.com/avr-gcc-makefile/">sample Makefile</a> which makes compilation and firmware download easy.</p>
<p>The main loop of the program does two tasks &#8212; measures and displays the readings over the serial port (which goes to the USB or over-the-air via XBee), and sleeps for about 60 seconds. As already mentioned, I use the new feature of ATmega168 which allows for the Watchdog timer to generate an interrupt, which wakes the chip from sleep mode. This is very handy as it allows you to continue the program at the point where you put it to sleep. The sleep mode was something new for me; there are some URLs in the source code which show what online articles helped me to master it. Note that the XBee RF transmitter is also put into sleep mode, in order to save battery.</p>
<p><u><strong>Data collector</strong></u><br />
All the sensor readings are collected to a Linux server over-the-air. I use an <a href="http://www.sparkfun.com/products/8687">XBee Explorer USB</a> by <a href="http://www.sparkfun.com/">Sparkfun</a> to connect the XBee receiver with the Linux server. The XBee is seen as a serial device on the Linux box. The frame protocol of the XBee API is easy to understand and I implemented a Perl script to parse it. Here is a sample reading which is received from one of my wireless sensors (0&#215;0001 is the address of the probe standing outside of my apartment):</p>
<blockquote><p>[Mon Dec 26 17:32:11 2011] RX_packet: source=0&#215;0001, rssi=-55dBm (opt=0&#215;00):  4.24;69.04</p></blockquote>
<p>As you can see, now it&#8217;s winter here &#8212; 4.24 degrees Celsius temperature; 69.04% relative humidity (RH).</p>
<p><u><strong>Board design</strong></u><br />
Both the schematics and PCB board were designed using <a href="http://www.cadsoftusa.com/">Eagle PCB</a> by CadSoft. This is a great piece of software. Most PCB factories accept Eagle board files directly. You&#8217;ll find my Eagle files in the Resources section at the end of this article.</p>
<p><u><strong>Lessons learned</strong></u><br />
There are a few things which I discovered only once I already built and tested the schematics:</p>
<ul>
<li>Battery-operated devices are hard to design &#8212; in theory my sensors were supposed to last for about 3 months with a 9V battery. In practice only one of them lasted for a month, the others &#8211; for a week.</li>
<li>Electronics components, boards and/or assembly could differ a lot &#8212; see above. Also one of the SHT11 sensors is <strong>sometimes</strong> giving CRC errors.</li>
<li>XBee indoor range is not excellent.</li>
<li>Research and development takes a lot of time, usually 2x or 3x the time you planned. Furthermore, building something with love takes even more time, but in the end it pays off with great results and satisfaction.</li>
<li>You can create an electronics device with a lower price than what is currently offered on the marked. But this has its price too &#8212; your time, and you get no guarantee whatsoever.</li>
</ul>
<p>I had different plans for this blog article but it got so lengthy that I wrote it in four different days (and it&#8217;s Christmas now). The main idea was to sketch the device and all its components, and to show that they can work together as a finished product. If there is any interest by other people, I&#8217;m happy to answer to any questions.</p>
<p>Happy holidays and best of luck in 2012!!! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Resources:</p>
<ul>
<li><a href="http://famzah.net/download/wireless-sensor/xbee-read-th-sensors.txt">Perl XBee API parser</a></li>
<li><a href="http://famzah.net/download/wireless-sensor/sht1x/">Firmware source code</a></li>
<li><a href="http://famzah.net/download/wireless-sensor/eagle/">Eagle schematics and PCB board files</a></li>
</ul>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a>, <a href='http://blog.famzah.net/category/hardware/'>Hardware</a>, <a href='http://blog.famzah.net/category/linux/'>Linux</a> Tagged: <a href='http://blog.famzah.net/tag/atmega168/'>ATmega168</a>, <a href='http://blog.famzah.net/tag/atmel-avr/'>atmel avr</a>, <a href='http://blog.famzah.net/tag/diy/'>DIY</a>, <a href='http://blog.famzah.net/tag/electronics/'>electronics</a>, <a href='http://blog.famzah.net/tag/humidity/'>humidity</a>, <a href='http://blog.famzah.net/tag/pcb/'>PCB</a>, <a href='http://blog.famzah.net/tag/sht11/'>SHT11</a>, <a href='http://blog.famzah.net/tag/temperature/'>temperature</a>, <a href='http://blog.famzah.net/tag/wireless/'>wireless</a>, <a href='http://blog.famzah.net/tag/xbee/'>XBee</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/1078/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/1078/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/1078/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/1078/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/1078/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/1078/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/1078/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/1078/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/1078/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/1078/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/1078/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/1078/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/1078/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/1078/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1078&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2011/12/26/diy-temperature-and-humidity-wireless-data-logger/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>

		<media:content url="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg" medium="image">
			<media:title type="html">Sensor with XBee (wireless)</media:title>
		</media:content>

		<media:content url="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg?w=150" medium="image">
			<media:title type="html">Sensor with XBee (wireless)</media:title>
		</media:content>

		<media:content url="http://famzah.files.wordpress.com/2011/12/only-usb.jpg?w=150" medium="image">
			<media:title type="html">Sensor without XBee (only USB)</media:title>
		</media:content>

		<media:content url="http://famzah.files.wordpress.com/2011/12/schematics.png?w=150" medium="image">
			<media:title type="html">Schematics</media:title>
		</media:content>

		<media:content url="http://famzah.files.wordpress.com/2011/12/board.png?w=150" medium="image">
			<media:title type="html">PCB board</media:title>
		</media:content>

		<media:content url="http://famzah.files.wordpress.com/2011/12/only-usb.jpg?w=150" medium="image">
			<media:title type="html">Sensor without XBee (only USB)</media:title>
		</media:content>

		<media:content url="http://famzah.files.wordpress.com/2011/12/with-xbee.jpg?w=150" medium="image">
			<media:title type="html">Sensor with XBee (wireless)</media:title>
		</media:content>
	</item>
		<item>
		<title>URL escape in Bash</title>
		<link>http://blog.famzah.net/2011/08/25/url-escape-in-bash/</link>
		<comments>http://blog.famzah.net/2011/08/25/url-escape-in-bash/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 09:49:22 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[url escape]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=1037</guid>
		<description><![CDATA[I recently needed to escape some user-supplied input for an URL address variable, in a Bash script. This is what the PHP urlencode(), and Perl URI::Escape::uri_escape() functions do, for example. My initial approach was to call Perl from the Bash script: Though I wanted to optimize the Bash script by not having to fork() a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1037&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently needed to escape some user-supplied input for an URL address variable, in a Bash script. This is what the <a href="http://www.php.net/manual/en/function.urlencode.php">PHP urlencode()</a>, and <a href="http://search.cpan.org/dist/URI/URI/Escape.pm">Perl URI::Escape::uri_escape()</a> functions do, for example. My initial approach was to call Perl from the Bash script:</p>
<div class="sourcecode"><pre class="brush: bash;">
#!/bin/bash
function urlencode() {
	echo -n &quot;$1&quot; | perl -MURI::Escape -ne 'print uri_escape($_)'
}
</pre></p>
</div>
<p>Though I wanted to optimize the Bash script by not having to fork() a Perl interpreter every time, which could be CPU intensive if you execute the Bash script often. So I ended up with the following solution, entirely coded in Bash, using <a href="http://tldp.org/LDP/abs/html/string-manipulation.html">Bash string manipulation</a> and <a href="http://wiki.bash-hackers.org/syntax/arrays">Bash hash arrays</a>:</p>
<div class="sourcecode">
<pre class="brush: bash;">
#!/bin/bash
set -u

declare -A ord_hash # associative hash; requires Bash version 4

function init_urlencode() {
	# this is the whole ASCII set, without the chr(0) and chr(255) characters
	ASCII='...!&quot;#$%&amp;'\''()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—™љ›њќћџ ЎўЈ¤Ґ¦§Ё©Є«¬­®Ї°±Ііґµ¶·ё№є»јЅѕїАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэю...'
	# download the script, don't copy paste it from the blog page!

	# chr(0) cannot be stored in a Bash variable

	local idx
	for idx in {0..253}; do # 0..253 = 254 elements = length($ASCII)
		local c=&quot;${ASCII:$idx:1}&quot; # VERY SLOW
		local store_idx=$(($idx+1))
		ord_hash[&quot;$c&quot;]=&quot;$store_idx&quot;
		# chr(255) cannot be used as a key
	done
}

function urlencode() {
	local inp=&quot;$1&quot;
	local len=&quot;${#inp}&quot;
	local n=0
	local val
	while [ &quot;$n&quot; -lt &quot;$len&quot; ]; do
		local c=&quot;${inp:$n:1}&quot; # VERY SLOW
		if [ &quot;$c&quot; == &quot;я&quot; ]; then # chr(255) cannot be used as a key
			val=255
		else
			val=&quot;${ord_hash[$c]}&quot;
		fi
		printf '%%%02X' &quot;$val&quot;
		n=$((n+1))
	done
}

init_urlencode # call only once
urlencode 'some^fancy#text'
</pre>
</div>
<p>The logic works pretty well, but the performance is terrible. It turned out that the Bash string manipulation methods are rather slow. So I finally ended up by using Perl, the same way I did it initially. For very small strings in the order of a few characters, you should be fine. But for anything else, this implementation is not recommended.</p>
<p>If you still want to use the Bash code, please download it directly from <a href="http://www.famzah.net/download/bash_urlencode/escape.sh.txt">here</a>, because the blog page messed up some of the special ASCII characters.</p>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/bash/'>bash</a>, <a href='http://blog.famzah.net/tag/url-escape/'>url escape</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/1037/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1037&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2011/08/25/url-escape-in-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
		<item>
		<title>Parse XML into a PHP array</title>
		<link>http://blog.famzah.net/2011/07/15/parse-xml-into-a-php-array/</link>
		<comments>http://blog.famzah.net/2011/07/15/parse-xml-into-a-php-array/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 08:21:45 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=1014</guid>
		<description><![CDATA[There are many different examples on how to parse an XML document into an array with PHP. What mine is different with is that it: is very memory efficient by using PHP references (similar to pointers in C) uses no recursion, thus there is no limit on the XML subtree levels is very strict and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1014&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are many different examples on how to parse an XML document into an array with PHP. What mine is different with is that it:</p>
<ul>
<li>is very memory efficient by using <a href="http://php.net/manual/en/language.references.php">PHP references</a> (similar to pointers in C)</li>
<li>uses no recursion, thus there is no limit on the XML subtree levels</li>
<li>is very strict and paranoid about correctness</li>
</ul>
<p>The parsing is done using <a href="http://bg2.php.net/manual/en/book.xml.php">XML Parser</a>.</p>
<p>An example input XML data follows:</p>
<div class="sourcecode">
<pre class="brush: xml; highlight: [5,6,8,9];">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;root&gt;
	&lt;first_item&gt;Test 1st item&lt;/first_item&gt;
	&lt;first_level_nested&gt;
		&lt;item idx=&quot;0&quot;&gt;value #1&lt;/item&gt;
		&lt;item idx=&quot;1&quot;&gt;value #2&lt;/item&gt;
		&lt;second_level_nested&gt;
			&lt;item idx=&quot;0&quot;&gt;value #3&lt;/item&gt;
			&lt;item idx=&quot;1&quot;&gt;value #4&lt;/item&gt;
		&lt;/second_level_nested&gt;
	&lt;/first_level_nested&gt;
	&lt;second_item&gt;Test 2nd item&lt;/second_item&gt;
&lt;/root&gt;
</pre>
</div>
<p>There is one specific hack here. Since XML allows it to have an element with the same name multiple times on the same subtree level (see <strong>&lt;item&gt;</strong> on lines #05, #06, #08, #09), and at the same time it does not allow to have an element with only numeric name, we need to make the following exception for arrays which have numeric indexes:</p>
<ul>
<li>If an <em>element</em> is named <strong>&lt;item&gt;</strong>, and it has an <em>attribute</em> named <strong>&#8220;idx&#8221;</strong>, then we will use this attribute as name, and respectively array key.</li>
</ul>
<p>This is handled in the XmlCallback() class, method startElement(), lines #44, #45, #46, which are also highlighted. You can see the sources at the end of the article.</p>
<p>XML also allows it that an element contains both DATA and sub-elements. This cannot be parsed into a PHP array, and will result in an Exception.</p>
<p>The parsed PHP array would look like as follows:</p>
<div class="sourcecode">
<pre class="brush: php;">
Array
(
	[root] =&gt; Array
	(
		[first_item] =&gt; Test 1st item
		[first_level_nested] =&gt; Array
		(
			[0] =&gt; value #1
			[1] =&gt; value #2
			[second_level_nested] =&gt; Array
			(
				[0] =&gt; value #3
				[1] =&gt; value #4
			)

		)

		[second_item] =&gt; Test 2nd item
	)

)
</pre>
</div>
<p>If you liked the results, you can download the sources which follow (click &#8220;show source&#8221; below):</p>
<div class="sourcecode">
<pre class="brush: php; collapse: true; highlight: [44,45,46]; light: false; toolbar: true;">
&lt;?php

function xml_decode($output) {
	$xml_parser = xml_parser_create();
	$xml_callback = new XmlCallback();
	
	if (!xml_set_element_handler(
		$xml_parser,
		array($xml_callback, 'startElement'),
		array($xml_callback, 'endElement')
	)) throw new Exception('xml_set_element_handler() failed');
	if (!xml_set_character_data_handler($xml_parser, array($xml_callback, 'data'))) {
		throw new Exception('xml_set_character_data_handler() failed');
	}
	if (!xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0)) {
		throw new Exception('xml_parser_set_option() failed');
	}
	
	if (!xml_parse($xml_parser, $output, TRUE)) {
		$xml_error = sprintf(
			&quot;%s at line %d&quot;,
			xml_error_string(xml_get_error_code($xml_parser)),
			xml_get_current_line_number($xml_parser)
		);
		throw new Exception(&quot;XML error: $xml_error\nXML data: $output&quot;);
	}
	
	xml_parser_free($xml_parser);
	
	return $xml_callback-&gt;getResult();
}

class XmlCallback {
	private $ret = null;
	/* assign and use references directly to the array, or else you'll be in trouble */
	private $ptr_stack = array();
	private $level = 0;

	public function __construct() {
		$this-&gt;ptr_stack[$this-&gt;level] =&amp; $this-&gt;ret;
	}

	public function startElement($parser, $name, $attrs) {
		if ($name == 'item' &amp;&amp; isset($attrs['idx'])) {
			$name = $attrs['idx']; /* reconstruct arrays with numeric indexes */
		}

		if (!isset($this-&gt;ptr_stack[$this-&gt;level])) {
			$this-&gt;ptr_stack[$this-&gt;level] = array();
			$this-&gt;ptr_stack[$this-&gt;level][$name] = null;
		} else {
			if (!is_array($this-&gt;ptr_stack[$this-&gt;level])) {
				if (!strlen(trim($this-&gt;ptr_stack[$this-&gt;level]))) {
					/* if until now we got only whitespace (thus scalar data),
					but now we start a nested elements structure, discard this
					whitespace, as it is most probably just space between the
					element tags */
					$this-&gt;ptr_stack[$this-&gt;level] = array();
				} else {
					throw new Exception('Mixed array and scalar data');
				}
			}
			if (isset($this-&gt;ptr_stack[$this-&gt;level][$name])) {
				/* isset() == (isset() &amp;&amp; !is_null()) */
				throw new Exception(&quot;Duplicate element name: $name&quot;);
			}
		}

		/* array_push() */
		++$this-&gt;level;
		$this-&gt;ptr_stack[$this-&gt;level] =&amp; $this-&gt;ptr_stack[$this-&gt;level-1 /* MINUS ONE! */][$name];
	}

	public function endElement($parser, $name) {
		if (!array_key_exists($this-&gt;level, $this-&gt;ptr_stack)) {
			throw new Exception('XML non-existing reference');
		}

		/* array_pop() */
		unset($this-&gt;ptr_stack[$this-&gt;level]);
		--$this-&gt;level;

		if ($this-&gt;level &lt; 0) throw new Exception('XML stack underflow');
	}

	public function data($parser, $data) {
		if (is_array($this-&gt;ptr_stack[$this-&gt;level])) {
			if (strlen(trim($data))) { # check if this is just whitespace
				throw new Exception('Mixed array and scalar data');
			} else {
				/* we tolerate AND skip whitespace, if we're already in
				a nested elements structure, as this whitespece is most
				probably just space between the element tags */
				return;
			}
		}
		if (is_null($this-&gt;ptr_stack[$this-&gt;level])) {
			$this-&gt;ptr_stack[$this-&gt;level] = ''; /* first data input */
		}
		$this-&gt;ptr_stack[$this-&gt;level] .= $data; /* we may be called several times, in chunks */
	}

	public function getResult() {
		return $this-&gt;ret;
	}
}
</pre>
</div>
<hr />
<p>Update, 20/Jul/2011: The source code was modified to handle white-space better, in order to fix the following tricky sample XML input: <strong>&lt;item6&gt; &amp;amp;  &amp;lt; &lt;/item6&gt;</strong></p>
<p>Update, 30/Jul/2011: Another bugfix which handles empty responses like: <strong>&lt;response/&gt;</strong></p>
<hr />
<p>References:</p>
<ul>
<li>
There are plenty of other (similar) solutions out there:</p>
<ul>
<li><a href="http://www.zend.com/code/codex.php?ozid=982&amp;single=1">XML @=@ Associative Array</a></li>
<li><a href="http://php.net/manual/en/book.simplexml.php">SimpleXML PHP examples</a></li>
<li><a href="http://www.bin-co.com/php/scripts/xml2array/">xml2array() &#8211; XML Parser for PHP</a></li>
</ul>
</li>
</ul>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/php/'>php</a>, <a href='http://blog.famzah.net/tag/xml/'>xml</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/1014/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/1014/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/1014/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/1014/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/1014/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/1014/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/1014/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/1014/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/1014/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/1014/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/1014/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/1014/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/1014/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/1014/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1014&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2011/07/15/parse-xml-into-a-php-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
		<item>
		<title>Testing exception message with PHPUnit</title>
		<link>http://blog.famzah.net/2011/07/07/testing-exception-message-with-phpunit/</link>
		<comments>http://blog.famzah.net/2011/07/07/testing-exception-message-with-phpunit/#comments</comments>
		<pubDate>Thu, 07 Jul 2011 06:09:01 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[exception message]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=1009</guid>
		<description><![CDATA[PHPUnit has a built-in method to test if an expected exception occurred during a test case: You cannot however test the message of the exception. There are cases where a program may throw the same exception type, but with different messages for different errors, and you want to differentiate between them. Here is my example [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1009&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.phpunit.de">PHPUnit</a> has a built-in method to test if an expected exception occurred during a test case:</p>
<div class="sourcecode">
<pre class="brush: php; gutter: false;">
$this-&gt;setExpectedException('Exception');
</pre>
</div>
<p>You cannot however test the message of the exception. There are cases where a program may throw the same exception type, but with different messages for different errors, and you want to differentiate between them.</p>
<p>Here is my example code on how to reliably test for the <strong>type</strong> and <strong>message</strong> of an <strong>exception</strong>:</p>
<div class="sourcecode">
<pre class="brush: php;">
class staticSessionTest extends PHPUnit_Framework_TestCase {
	...
	function test_bad_data() {
		$emess = null;
		try {
			$this-&gt;sess-&gt;start('must be array', FALSE, FALSE); # we expect an Exception here
		} catch (Exception $e) { $emess = $e-&gt;getMessage(); }
		$this-&gt;assertEquals($emess, 'Session data must be an array');
	}
	...
}
</pre>
</div>
<p>Putting the assertEquals() outside of the try&#8230;catch block ensures that you cannot forget to test for the <strong>message</strong>. The <strong>type</strong> of the exception is coded inside the catch(&#8230;) block.</p>
<hr />
<p><strong>UPDATE:</strong> I just re-read the latest <a href="http://www.phpunit.de/manual/current/en/appendixes.annotations.html">PHPUnit Annotations</a>, and this feature is already included in the standard PHPUnit suite. The difference between my custom code and the &#8220;<strong>@expectedExceptionMessage</strong>&#8221; annotation is that the annotation is valid for the whole test block of execution, while using try&#8230;catch you can specify precisely where you expect the exception to occur.</p>
<hr />
<p>References:</p>
<ul>
<li><a href="http://danbettles.net/2011/01/another-alternative-approach-to-testing-exceptions-with-phpunit/">An(other) Alternative Approach To Testing Exceptions With PHPUnit</a></li>
<li><a href="http://www.phpunit.de/manual/current/en/appendixes.annotations.html">PHPUnit Annotations</a></li>
</ul>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/exception-message/'>exception message</a>, <a href='http://blog.famzah.net/tag/php/'>php</a>, <a href='http://blog.famzah.net/tag/phpunit/'>phpunit</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/1009/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/1009/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/1009/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/1009/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/1009/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/1009/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/1009/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/1009/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/1009/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/1009/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/1009/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/1009/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/1009/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/1009/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=1009&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2011/07/07/testing-exception-message-with-phpunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
		<item>
		<title>Print to STDERR in PHP</title>
		<link>http://blog.famzah.net/2010/11/30/print-to-stderr-in-php/</link>
		<comments>http://blog.famzah.net/2010/11/30/print-to-stderr-in-php/#comments</comments>
		<pubDate>Tue, 30 Nov 2010 11:48:11 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=954</guid>
		<description><![CDATA[If you are writing a command line tool in PHP and want to write to STDERR, here is the command: file_put_contents('php://stderr', 'This text goes to STDERR'); The relevant documentation page is PHP input/output streams. P.S. I actually wanted to reply to this page, but there was no way to leave a comment there&#8230; Filed under: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=954&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are writing a command line tool in PHP and want to write to STDERR, here is the command:</p>
<blockquote><p><code>file_put_contents('php://stderr', 'This text goes to STDERR');</code></p></blockquote>
<p>The relevant documentation page is <a href="http://php.net/manual/en/wrappers.php.php">PHP input/output streams</a>.</p>
<p>P.S. I actually wanted to reply to <a href="http://dren.ch/php-print-to-stderr/">this</a> page, but there was no way to leave a comment there&#8230;</p>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/php/'>php</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/954/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/954/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/954/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/954/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/954/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/954/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/954/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/954/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/954/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/954/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/954/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/954/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/954/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/954/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=954&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2010/11/30/print-to-stderr-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
		<item>
		<title>Google App Engine Performance Profiling</title>
		<link>http://blog.famzah.net/2010/09/14/google-app-engine-performance-profiling/</link>
		<comments>http://blog.famzah.net/2010/09/14/google-app-engine-performance-profiling/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 09:53:47 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[appengine-profiler]]></category>
		<category><![CDATA[Appstats]]></category>
		<category><![CDATA[AppWrench]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiler]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=923</guid>
		<description><![CDATA[When developing under Google App Engine, developers need to pay attention to how fast their code works, and also how much resources their code uses. The first parameter is vital for user-experience, the second &#8211; for the hosting expenses, as resources usage costs money. It turns out that there are quite a few suitable profilers [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=923&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When developing under <a href="http://code.google.com/appengine/">Google App Engine</a>, developers need to pay attention to how fast their code works, and also how much resources their code uses. The first parameter is vital for user-experience, the second &#8211; for the hosting expenses, as resources usage costs money.</p>
<p>It turns out that there are quite a few suitable profilers for Google App Engine. At least these are the ones I could find:</p>
<ul>
<li><a href="http://sites.google.com/site/appengineappstats/">Appstats</a> &#8211; no doubt, the most advanced one, giving you information about the timing and costs of your RPC calls to the datastore, Memcache, etc.<br />
Works for both Python, and Java. Included in the official SDK as of version 1.3.1.</li>
<li><a href="http://code.google.com/p/appengine-profiler/">appengine-profiler</a> &#8211; besides being an RPC profiler like <a href="http://sites.google.com/site/appengineappstats/">Appstats</a>, <a href="http://code.google.com/p/appengine-profiler/">appengine-profiler</a> gives you the option to profile the CPU usage of your code, thus you can easily identify hot-spots where your code wastes a lot of CPU resources and wall-clock time. You can define &#8220;<a href="http://code.google.com/p/appengine-profiler/wiki/Screenshots#Tracepoints">tracepoints</a>&#8221; which surround part of your code blocks, and you&#8217;ll easily know the resources usage of these blocks for each page load.<br />
Works for Python.</li>
<li><a href="http://appwrench.onpositive.com/">AppWrench</a> &#8211; the Java profiler. I don&#8217;t code on Java, and I haven&#8217;t tested this profiler, but I&#8217;m including it here for all you Java gurus.</li>
</ul>
<hr />
<p>Resources:</p>
<ul>
<li><a href="http://code.google.com/p/appengine-profiler/wiki/Screenshots">appengine-profiler &#8211; Examples on what output you will actually see in your application logs</a>.</li>
<li><a href="http://googleappengine.blogspot.com/2010/03/easy-performance-profiling-with.html">Easy Performance Profiling with Appstats</a>.</li>
<li><a href="http://blog.dantup.com/2010/01/profiling-google-app-engine-with-appstats">Profiling Google App Engine with Appstats</a>.</li>
</ul>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/appengine-profiler/'>appengine-profiler</a>, <a href='http://blog.famzah.net/tag/appstats/'>Appstats</a>, <a href='http://blog.famzah.net/tag/appwrench/'>AppWrench</a>, <a href='http://blog.famzah.net/tag/google-app-engine/'>google app engine</a>, <a href='http://blog.famzah.net/tag/performance/'>performance</a>, <a href='http://blog.famzah.net/tag/profiler/'>profiler</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/923/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/923/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/923/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/923/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/923/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/923/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/923/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/923/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/923/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/923/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/923/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/923/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/923/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/923/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=923&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2010/09/14/google-app-engine-performance-profiling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
		<item>
		<title>PHP non-interactive usage in a cron job</title>
		<link>http://blog.famzah.net/2010/09/14/php-non-interactive-usage-in-a-cron-job/</link>
		<comments>http://blog.famzah.net/2010/09/14/php-non-interactive-usage-in-a-cron-job/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 08:33:24 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[cron job]]></category>
		<category><![CDATA[crontab]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sudo]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=901</guid>
		<description><![CDATA[Using a PHP script in a crontab is fairly easy, as stated in the &#8220;Using PHP from the command line&#8221; documentation&#8230; Until you start to get the following warning during the execution: No entry for terminal type &#8220;unknown&#8221;; using dumb terminal settings. The script works, but this nasty warning really bothers you. Here is a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=901&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Using a PHP script in a crontab is fairly easy, as stated in the &#8220;<a href="http://php.net/manual/en/features.commandline.php">Using PHP from the command line</a>&#8221; documentation&#8230; Until you start to get the following warning during the execution:</p>
<blockquote><p>No entry for terminal type &#8220;unknown&#8221;;<br />
using dumb terminal settings.</p></blockquote>
<p>The script works, but this nasty warning really bothers you.</p>
<p>Here is a sample crontab entry:</p>
<blockquote><p>* * * * * root sudo -u www-data php -r &#8216;echo &#8220;test&#8221;;&#8217;</p></blockquote>
<p>When executed, it prints the warning on STDERR.</p>
<p>Yes, I know I don&#8217;t need &#8220;<a href="http://en.wikipedia.org/wiki/Sudo">sudo</a>&#8221; here, but this was my initial usage pattern as I discovered the problem, and at the first time I suspected that &#8220;sudo&#8221; got crazy. Well, it wasn&#8217;t &#8220;sudo&#8221; to blame, but PHP.</p>
<p>Here is the <strong>fixed</strong> crontab entry:</p>
<blockquote><p>* * * * * root sudo -u www-data <strong>TERM=dumb</strong> php -r &#8216;echo &#8220;test&#8221;;&#8217;</p></blockquote>
<p>The issue was encountered on an Ubuntu 10.04 server. I though crond usually sets $TERM to something&#8230; Anyway, problem solved.</p>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a>, <a href='http://blog.famzah.net/category/linux/'>Linux</a> Tagged: <a href='http://blog.famzah.net/tag/cron-job/'>cron job</a>, <a href='http://blog.famzah.net/tag/crontab/'>crontab</a>, <a href='http://blog.famzah.net/tag/php/'>php</a>, <a href='http://blog.famzah.net/tag/sudo/'>sudo</a>, <a href='http://blog.famzah.net/tag/ubuntu/'>ubuntu</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/901/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/901/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/901/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/901/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/901/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/901/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/901/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/901/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/901/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/901/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/901/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/901/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/901/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/901/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=901&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2010/09/14/php-non-interactive-usage-in-a-cron-job/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
		<item>
		<title>Amazon Web Services Usage Report Visualizer</title>
		<link>http://blog.famzah.net/2010/09/13/amazon-web-services-usage-report-visualizer/</link>
		<comments>http://blog.famzah.net/2010/09/13/amazon-web-services-usage-report-visualizer/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 13:06:22 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[reporting]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=892</guid>
		<description><![CDATA[While Amazon Web Services provide a free visualizer tool for your EBS Volume usage in the EC2 &#8220;Monitoring&#8221; section, they missed to create image charts for the other things you get billed, like the count of running EC2 instances, In- and Out Data transfer, and so on. I wanted to try Google Chart Tools API [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=892&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While <a href="http://aws.amazon.com/">Amazon Web Services</a> provide a free visualizer tool for your EBS Volume usage in the EC2 &#8220;Monitoring&#8221; section, they missed to create image charts for the other things you get billed, like the count of running EC2 instances, In- and Out Data transfer, and so on.</p>
<p>I wanted to try <a href="http://code.google.com/apis/chart/">Google Chart Tools API</a> for a long time, and that&#8217;s why I coded a simple AWS Usage Report visualizer which creates graphics of your AWS usage, based on the CSV reports which you can download from the Amazon administration portal page.</p>
<p>Here it is &#8211; the <a target="_blank" href="http://famzah-teamuser.appspot.com/aws-chart-report/"><strong>Amazon Web Services Report Visualizer</strong></a>.<br />
There you&#8217;ll find some screenshots too, if you just to take a quick look at it.</p>
<p>As usual, I though it&#8217;ll take me like a few hours to code it, and in reality it took me more like 5-6 hours, duh&#8230; I hope you like the result.</p>
<p>If you have any comments, don&#8217;t hesitate to share them.</p>
<hr />
<p>A screenshot teaser:<br />
<div class="wp-caption aligncenter" style="width: 691px"><a href="http://famzah-teamuser.appspot.com/aws-chart-report/"><img alt="" src="http://famzah-teamuser.appspot.com/static/aws-chart-report/screenshots/AmazonEC2-RunInstances-DataTransfer-In-Bytes-chart.png" title="Sample chart screenshot (EC2 Data Transfer In Bytes)" width="681" height="509" /></a><p class="wp-caption-text">Sample chart screenshot (EC2 Data Transfer In Bytes)</p></div></p>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/aws/'>aws</a>, <a href='http://blog.famzah.net/tag/ec2/'>ec2</a>, <a href='http://blog.famzah.net/tag/reporting/'>reporting</a>, <a href='http://blog.famzah.net/tag/s3/'>s3</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/892/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/892/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/892/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/892/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/892/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/892/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/892/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/892/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/892/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/892/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/892/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/892/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/892/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/892/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=892&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2010/09/13/amazon-web-services-usage-report-visualizer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>

		<media:content url="http://famzah-teamuser.appspot.com/static/aws-chart-report/screenshots/AmazonEC2-RunInstances-DataTransfer-In-Bytes-chart.png" medium="image">
			<media:title type="html">Sample chart screenshot (EC2 Data Transfer In Bytes)</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting &#8220;500 Line too long (limit is 4096)&#8221; error in Perl</title>
		<link>http://blog.famzah.net/2010/09/11/getting-500-line-too-long-limit-is-4096-error-in-perl/</link>
		<comments>http://blog.famzah.net/2010/09/11/getting-500-line-too-long-limit-is-4096-error-in-perl/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 08:43:00 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[LWP]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[soap::lite]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=885</guid>
		<description><![CDATA[The error may also be &#8220;500 Line too long (limit is 8192)&#8221; but the problem is still the same &#8211; LWP or SOAP::Lite return this error when you try to POST or GET something very long. The one to blame is actually Net::HTTP::Methods, included somewhere by something. It took me a few hours to get [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=885&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The error may also be &#8220;500 Line too long (limit is 8192)&#8221; but the problem is still the same &#8211; <a href="http://search.cpan.org/dist/libwww-perl/lib/LWP.pm">LWP</a> or <a href="http://www.soaplite.com/">SOAP::Lite</a> return this error when you try to POST or GET something very long.</p>
<p>The one to blame is actually <a href="http://search.cpan.org/~gaas/libwww-perl-5.836/lib/Net/HTTP.pm">Net::HTTP::Methods</a>, included somewhere by something.</p>
<p>It took me a few hours to get this resolved:</p>
<div class="sourcecode">
<pre class="brush: perl; light: true;">
use LWP::Protocol::http; # to suppress the warning &quot;possible typo&quot; in the next statement
push(@LWP::Protocol::http::EXTRA_SOCK_OPTS, MaxLineLength =&gt; 0); # to remove the limit
</pre>
</div>
<p>Put the above code in your Perl HTTP client and you&#8217;re good to go!</p>
<p>References:</p>
<ul>
<li><a href="http://www.mail-archive.com/libwww@perl.org/msg03754.html">Re: Fw: LWP Problem: Error: 500 Line too long (limit is 4096)</a></li>
<li><a href="http://www.webmasterkb.com/Uwe/Forum.aspx/libwww/25/Bind-to-specific-IP">Bind to specific IP</a></li>
</ul>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/lwp/'>LWP</a>, <a href='http://blog.famzah.net/tag/perl/'>perl</a>, <a href='http://blog.famzah.net/tag/soaplite/'>soap::lite</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/885/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/885/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/885/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/885/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/885/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/885/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/885/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/885/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/885/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/885/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/885/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/885/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/885/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/885/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=885&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2010/09/11/getting-500-line-too-long-limit-is-4096-error-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
		<item>
		<title>Google App Engine &#8211; Datastore performance, and Memcache behavior</title>
		<link>http://blog.famzah.net/2010/08/31/google-app-engine-datastore-performance-and-memcache-behavior/</link>
		<comments>http://blog.famzah.net/2010/08/31/google-app-engine-datastore-performance-and-memcache-behavior/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 15:26:36 +0000</pubDate>
		<dc:creator>Ivan Zahariev</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[appengine datastore]]></category>
		<category><![CDATA[appengine memcache]]></category>
		<category><![CDATA[google appengine]]></category>

		<guid isPermaLink="false">http://blog.famzah.net/?p=862</guid>
		<description><![CDATA[Ever since I&#8217;ve been working with Google App Engine, there are two issues which bothered me a lot: Datastore performance &#8211; lots of people have already written about it (see links #1, #2, and #3). Currently, when working with small datasets, it&#8217;s far from being comparable even with a slow MySQL database, and you may [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=862&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ever since I&#8217;ve been working with <a href="http://code.google.com/intl/bg/appengine/">Google App Engine</a>, there are two issues which bothered me a lot:</p>
<ol>
<li><a href="http://code.google.com/intl/bg/appengine/docs/python/datastore/overview.html">Datastore</a> performance &#8211; lots of people have already written about it (see links <a href="http://3.14.by/en/read/why-google-appengine-sucks">#1</a>, <a href="http://stackoverflow.com/questions/1902944/benchmarks-for-various-google-app-engine-datastore-operations">#2</a>, and <a href="http://code.google.com/status/appengine/">#3</a>). Currently, when working with small datasets, it&#8217;s far from being comparable even with a slow MySQL database, and you may also occasionally get internal errors, as well as increased latencies. I contacted Google about this, and asked them if the <a href="http://code.google.com/intl/bg/appengine/business/">Business customers of GAE</a> who pay for it would get better Datastore performance. Here is what I got as an answer from <a href="http://blog.notdot.net/">Nick Johnson</a>, a GAE developer:<br />
<blockquote><p>Business customers will receive paid support, which is prioritized, as well as the extra features we announced at I/O. System latency is not any different, however, as we try and make the system as fast as possible for all our users.</p></blockquote>
<p>So the bad news is that you cannot make the Datastore run faster, even if you pay.<br />
The good news is that we are all getting the same service in terms of speed, which is a good thing &#8211; when everybody is having difficulties, then the community will eventually find a solution.
</li>
<li><a href="http://code.google.com/intl/bg/appengine/docs/python/memcache/">Memcache</a> fairness &#8211; what happens if another website (on the same server) uses the Memcache service extensively, thus making the Memcache entries of my website expire too quickly, due to the memory pressure. Here is what Nick Johnson from Google replied:<br />
<blockquote><p>Memcache is segmented by application. Although there is some variation (so that apps that don&#8217;t use any memcache don&#8217;t take up usable space), every app is guaranteed a fair share of memcache space.</p></blockquote>
<p>Excellent system design GAE engineers. Keep up the good work!
</li>
</ol>
<p><strong>Update</strong>: Google App Engine engineers continue to do a very good work indeed! You should take a look at the new features announced with the <a href="http://googleappengine.blogspot.com/2010/08/multi-tenancy-support-high-performance_17.html">1.3.6 release of GAE</a>.</p>
<br />Filed under: <a href='http://blog.famzah.net/category/development/'>Development</a> Tagged: <a href='http://blog.famzah.net/tag/appengine-datastore/'>appengine datastore</a>, <a href='http://blog.famzah.net/tag/appengine-memcache/'>appengine memcache</a>, <a href='http://blog.famzah.net/tag/google-appengine/'>google appengine</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/famzah.wordpress.com/862/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/famzah.wordpress.com/862/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/famzah.wordpress.com/862/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/famzah.wordpress.com/862/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/famzah.wordpress.com/862/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/famzah.wordpress.com/862/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/famzah.wordpress.com/862/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/famzah.wordpress.com/862/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/famzah.wordpress.com/862/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/famzah.wordpress.com/862/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/famzah.wordpress.com/862/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/famzah.wordpress.com/862/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/famzah.wordpress.com/862/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/famzah.wordpress.com/862/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.famzah.net&amp;blog=10109730&amp;post=862&amp;subd=famzah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.famzah.net/2010/08/31/google-app-engine-datastore-performance-and-memcache-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e354dbace6659e6cfc6ec6f01d7d962d?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">famzah</media:title>
		</media:content>
	</item>
	</channel>
</rss>
