All posts by David

PHP array_shift() not a function for general use

While looking at metrics an monitoring the processing of a CBMHDHS*, I noticed that the active job queue would become quiescent for minutes at a time. Most markedly with the one specific tasklist. My gut told me it was an issue with the way PHP was handling my simple list (700,000 items give or take). So some performance testing was required. What I found was astonishing. PHP’s array_shift is horribly inefficient.

Here is an example to demonstrate this.

When shifting 2500 items (one at a time) off the array (list), it can take 30 or more seconds. Keep in mind this is all in memory! This test is with only 108,000 items:


2013-08-16 18:21:06 # STARTING ARRAY_SHIFT TEST: Q:108581
2013-08-16 18:21:43 # DONE removed 2500 Q:106081

It took 37 seconds, to be exact. To me, that seemed like a long time. Doing a little research I found that when you rewind an array to it’s beginning (using PHP), one of it’s side effects is that it returns the element at that pointer. Reset looks like a scary operation. It does not ‘reset’ the array in the sense of flushing it out, it just resets the pointer to the top. At any rate. the code looked like this:


$element = reset($array);

So, in theory one could use this to get the first element off an array automatically, without removing it. OK, but shift_array does this ANY removes the element. So this is not really the same action. However… (and you know there is a point there), there is another PHP function with a useful side effect. The ‘key() function‘ that returns the key value of at the current pointer (which in this case is the same location we returned the element above). It looks like this:


$key = key($array);

Now, with those two lines of code I have the element (value) and the key at the top of the array. So this is 2 lines of code where array_shift() is just one.. but we’re not even done yet.. we have to perform the most important part (for this exercise) and remove the element. So.. that’s a 3rd line of code like this, right?


unset($array[$key]);

OK.. so taking a line of code right out of the processor, there is a direct comparison. array_shift on the left, reset, key, unset on the right.

Array Shift method Array Reset, Key and Unset method
$key = array_shift($this->QUEUE)) {
$payload = $this->DATA[$key];
$appkey  = reset($this->QUEUE);
$payload = $this->DATA[$appkey];
$key     =  key($this->QUEUE);
unset($this->QUEUE[$key]);

What I didn’t expect to find is how dramatically FASTER the more complex (looking) code is. In The numbers don’t lie.. see for yourself.

Array Shift method.

Total time for 2500 items removed is 37 seconds:


2013-08-16 18:21:06 # STARTING ARRAY_SHIFT TEST: Q:108581
2013-08-16 18:21:43 # DONE removed 2500 Q:106081

Array Reset, Key + Unset method.

Total time for 2500 items removed is <1 second:


2013-08-16 18:21:46 # STARTING ARRAY_RESET TEST: Q:108581
2013-08-16 18:21:46 # DONE removed 2500 Q:106081

Those numbers include getting the payload data out of the 2nd array.

Mind blowing in my opinion. It’s not even a contest. Writing your own is at least 15 times faster than PHP’s native operation, that for all intents, accomplishes the same objective.

The point of all this?

If you are seeing an inexplicable slowdown somewhere in PHP.. and you can narrow it down to 1-2 operations… it’s probably worth your while to try to do it another way, even if that way looks more complex!

I could not believe this when I tried it. When I implement this in the processor, I should be able to remove hours of useless waiting to ‘shift’ items off the list. At some point I’d hope Zend would fix this. Keep in mind I ran this test on PHP 5.5.3 (the latest stable release I could get my mitts on, at the time).

*Cloud Based Massivly Horizontal Distributed Harvesting System

U.S. Red Bull Grand Prix – Laguna Seca “DUCATI ISLAND”

IMG_3237WOO! Another year on Ducati Island, and being able to take advantage of the fact I own a few Italian bikes (including 2 Ducati) was awesome. My son was with me for the first time this year and he got to experience the awesomeness that is Ducati Island first hand. It was a great weekend!!

The first few pics were of us wandering around there on Thursday, before things really kicked off. It was nice that nobody harassed us, and my son was able to test sit a few bikes before the crowds descended the next day.


U.S. Red Bull MotoGP 2013 — Day 1

Friday morning arrived early, and we set about stocking the trailer and getting ready to tow back to Laguna Seca.

Having secured an overflow camping site (one of the last handful as it turned out), the evening before, we embarked on the first campout at Laguna Seca for us; despite attending motorcycle racing there since the 1980’s.

It did not disappoint! Everyone has seen a trailer, so there are not many pictures of the expedition equipment itself, but Friday’s small crowd allowed for a leisurely day of exploring the vendors and taking some more snap-shots.

U.S. Red Bull MotoGP 2013 — Day -1

Day -1 at the U.S. Red Bull MotoGP in Monterey CA was quiet, but attended more heavily than expected. The day before the official start of the event is always interesting.

I meet the vendors while they are setting up, and for those that are already selling, I start relationship building with them. This always pays off in many ways, not the least of which is simply meeting new people, and often old “friends” from events past.

Here are some photos of the wanderings around we did on Day -1.

Amazon AWS – spinning up a micro for Gearman Client

Walkthough for spinning up a micro EC2 for use as a GearMan worker node.

This is a preliminary effort, and the steps may very well change over the next few weeks as these are tested out. The documentation I’m presenting here is based on my previous work [HERE] Install Gearman + Gearman-PHP on AWS ec2.

Getting Started

This assumes you already have an AWS account setup. If you don’t you need to go do that now. You can get started [HERE].

Once logged in, go to your dashboard. From here you’ll be selecting the EC2 area.
Screen Shot 2013-06-17 at 10.26.23 AM

Under resources you’ll find a button “Launch Instance”. This is the button you want to click. This is where the fun begins.
Screen Shot 2013-06-17 at 10.27.57 AM

In this case I’m going to use the ‘Classic Wizard’. It’s really not that magical but Wizard is such a super-awesome-cool-name (ala Windoze ’95) you’ll see it used here. Anyhow, I’m going classic:
Screen Shot 2013-06-17 at 10.30.04 AM

I want to keep things EVERY simple here so I’m going use the default Amazon AWS distribution/AMI.
Screen Shot 2013-06-17 at 10.31.52 AM

For this exercise, I’m using an ‘On Demand’ Micro instance:
Screen Shot 2013-06-17 at 10.34.01 AM

No Advanced features should be required, so I’ve left everything on this page set to it’s default settings:
Screen Shot 2013-06-17 at 10.36.20 AM

Next the storage requirements are defined. Since this is a worker that should be able to be spun up on need, and shouldn’t require much in the way of local storage, I’m going to opt out of defining and EBS volume and rely upon Ephemeral storage.
Screen Shot 2013-06-17 at 10.40.43 AM

At this point, I don’t see the need to define any keys for EC2 management, so I’m leaving this area blank:
Screen Shot 2013-06-17 at 10.41.30 AM

Next, select the .ssh key file (it’s stored in a .pem file) that you want to use to access this system. If you don’t have a set of these keys setup already, you’ll want to define them. I’m using one specific to this node class already defined.. you’ll need to handle this step as your policy/needs dictate. It’s not that complex, but word to you, DOWNLOAD THAT KEY, once you create it, there is no known way (according to all places I’ve checked) to download it again. BE WARNED.
Screen Shot 2013-06-17 at 10.47.16 AM

Next step will be do select a security profile for your node. I’ve found that the default one is sufficient for these purposes. You may want to further restrict the number of ports open, as the default opens a few extra things you might not want. This is another area where you’re own needs an policy will need to be carefully considered. Otherwise, start with default and iterate to the optimal configuration.
Screen Shot 2013-06-17 at 10.52.02 AM

Check your settings on this review page, and if everything is to your preference, then you can spin it up!!
Screen Shot 2013-06-17 at 10.55.38 AM

One you click launch, it will take a little while for the instance to go live.
Screen Shot 2013-06-17 at 10.57.20 AM

Once launched, you’ll be able to see your instance in the dashboard! Sorry bout all the greyd out information, but the instance I’m talking about is slightly highlighted in blue.
Screen Shot 2013-06-17 at 11.00.22 AM

NOW, YOU CAN START TO INSTALL YOUR GEARMAN COMPONENTS

Nor Cal Ducati – Monterey CA meetup

Last night we had the 2nd meeting up of the Nor Cal Ducati Club this evening in at Cibo, in Monterey CA.. It was a nice evening, warm, sunny and the food was quite enjoyable, but the company made it all worth the ride. And a bit of a challenging ride it was!

I met up with Dimitri on his cool Monster900 (a deal may be in the works soon!), at the Vista Point on Highway 1, for the ride to Monterey.

View Larger Map

We waited a little while to try and catch a couple of riders coming down from the North Bay, but by 5:30 PM we decided it was time to head south. We didn’t make it far before we were caught in a massive traffic jam on Hwy 1. It was a virtual parking lot, we were going nowhere.

Fortunately we both knew of an alternate route and turned around on Hwy1. By the time we reached Castroville, the traffic heading north was also backed up for miles. A check of the local news sources didn’t reveal the cause of all this chaos, but I heard that at least one person was life-flighted from and accident scene on the highway.

We arrived in Monterey a little later than planned but it was no matter, there were two parking spots for us in front of Cibo, so we pulled in, popped off the helmets and met up with the southern contingent of the Nor Cal Ducati Club. It was nice little array of fine Italian machines on display:
IMG_3008

IMG_3009

IMG_3011

Thanks to The Bear for setting this all up. Looking forward to the next Ducati Bike Night.

Memories of VME

vme.seattle.aug.2005.05
While enjoying a meeting up of the Nor Cal Ducati Club this evening in at Cibo, in Monterey CA., the subject of the Seattle VME (Vintage Motorcycle Enthusiasts) came up, specifically the wonderful and wacky “Isle of Vashon” event. The coolest aspect of the bikes you’ll see in these videos, is that they are ALL ridden to and around the island.

What others have posted about the TT

Sample videos shot by others:
VME Isle of Vashon 2011

VME Isle of Vashon 2008

Here are some articles about the event itself:


VME Isle of Vashon
The Northwest Classic Old Bike Event

http://www.soundrider.com

What, you missed it again? VME’s Isle of Vashon? Nooooo? Why? Didn’t know when the date was? Where was it listed?[…]


Motorcycle club appreciates the Island’s support
http://www.vashonbeachcomber.com
The Vintage Motorcycle Enthusiasts (VME) would like to thank the Vashon-Maury Island community for supporting our 29th annual old bike rally, known as the Isle of Vashon TT.

Thanks to the event’s con[…]


Now this is really cool, and geeky.. but a big post about VME on a Gamers forum!!

Vintage Motorcyle Rally (Pic intensive)

Where is this, “Vashon Island” place?

Vashon island is located in lower ventricle of Puget Sound:
Screen Shot 2013-06-16 at 10.46.29 PM

Situated above Tacoma, south-west of Seattle and south-east of Bremerton, Vashon Island is one of the few Puget Sound island that is serviced only by ferry. No conventional roads exist to get to this island, unlike the rest within Puget Sound that are connected via some sort of bridge or another.
Screen Shot 2013-06-16 at 10.58.30 PM

VME events, and the Isle of Vashon in particular, you can find not only a nice variety of vintage bikes, but a number of strange and often amusing customs.

I certainly wouldn’t mind attending another one of these unusual events some time in the near future!

Tale of two Italians

Awaking this morning, I decided to put together this little rundown of my two liter-class Italians. For a pair of bikes with remarkably similar style, their characters are vastly different.

Here is the 411:


2008 Ducati 1098 Superbike

2007 MV Agusta F4 1000R
bike_banner
Dimensions
Wheel Base:
56.3 in. Wheel Base:
55.4 inches
Seat Height:
32.2 in. Seat Height:
31.9 inches
Fuel Capacity:
4.1 gal. Fuel Capacity:
5.5 Gal
Dry Weight:
381 lbs. Dry Weight:
423.3 pounds
Suspension / Wheels
Front Tire:
120/70 ZR17 Front Tire:
120/70 ZR17
Rear Tire:
190/55 ZR17 Rear Tire:
190/55 ZR17
Front Forks:
45mm Showa Inverted, adjustable preload, adjustable compression & rebound damping Front Forks:
50mm Marzocchi Inverted, adjustable preload, adjustable compression & rebound damping
Engine and transmission
Displacement:
1099 cc Displacement:
998 cc
Bore and Stroke:
104 x 64.7mm Bore and Stroke:
76 mm x 55 mm
Compression:
12.5:1 Compression:
13.1:1
Fuel System:
Marelli EFI w/ elliptical throttle bodies Fuel System:
Fuel Injected
Horsepower (bhp):
160 bhp Horsepower (bhp):
174 hp
Torque (ft./lb.):
90.4 Torque (ft./lb.):
81.9
Transition:
6 speed Transition:
6 speed cassette
Fleet Status
Acquired:
Feb. 2008 (new) Acquired:
May 2013 (used)
Mileage:
14,581 Mileage:
11,203
Duty:
Occasional track days, special riding excursions, photography prop Duty:
Special riding excursions, photography prop
Modifications:
* CRC forged levers
* Woodcraft billet clutch cover
* Ducati Performance spider pressure plate
* AVVI Billet Rear Sets
* Custom painted brake, clutch and slave reservoir caps
* Ducati Performance exhaust and 1098R ECU
* H11 HID low-beam lamp
* Reflector delete
Modifications:
none
Riding Impressions
  • Wicked fast.
  • Scalpel sharp handling.
  • Loud and Proud
  • Incredible performance
  • Scalpel sharp handling.
  • Head turning design (motorcycle art)
  • Closest thing to an F1 engine on the street

Install Gearman + Gearman-PHP on AWS ec2

The fun and games continue!! As with every Gearman implementation I’ve done, there are trick for each environment. Here are the Cliff Notes (originally sourced from [Planet MySQL page] with my own twist) for getting Gearman setup on an Amazon Web Services (AWS) EC2 (Elastic Computing 2) node running the default AWS distribution. As always, your experience may vary.

Install required libraries

First, get all the required libraries installed using yum:

[ec2-user@]$ sudo yum install -y gcc
[ec2-user@]$ sudo yum install -y gcc-c++
[ec2-user@]$ sudo yum install -y gperf
[ec2-user@]$ sudo yum install -y boost
[ec2-user@]$ sudo yum install -y boost-devel
[ec2-user@]$ sudo yum install -y memcached
[ec2-user@]$ sudo yum install -y libuuid
[ec2-user@]$ sudo yum install -y libuuid-devel
[ec2-user@]$ sudo yum install -y libevent-devel
[ec2-user@]$ sudo yum install -y php-devel
[ec2-user@]$ sudo yum install -y php-xml

Compile Gearmand from Source

Very straight forward config and build.

[ec2-user@]$ cd gearmand-1.1.9
[ec2-user@]$ sudo ./configure --with-boost=/usr/include --prefix=/usr
[ec2-user@]$ sudo make
[ec2-user@]$ sudo make install

Compile Gearman PHP Library from Source

Fairly simple build, but you must first phpize.

[ec2-user@]$ cd gearman-1.1.1
[ec2-user@]$ sudo phpize
[ec2-user@]$ sudo ./configure --prefix=/usr
[ec2-user@]$ sudo make
[ec2-user@]$ sudo make install

Run ldconfig to Reload Dynmaic Library Cache

If you don’t run ldconfig, you’re going to get errors when you edit the php.ini file (last step).


bad:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/gearman.so' - libgearman.so.8: cannot open shared object file: No such file or directory in Unknown on line 0

[ec2-user@]$ sudo ldconfig

Edit the PHP ini file

This is the last step.

finding location of your php.ini file
[ec2-user@]$ php -i | grep php.ini
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

Edit your config file, adding these lines:

[ec2-user@]$ sudo vi /etc/php.ini
[Gearman]
; Add Gearman shared object to config
extension="gearman.so"

Now your install is complete!!

Installing HomeBrew for OSX

Installing Homebrew

This process appeared to be fairly trivial. Run the following command. The script will tell you want it wants to do. I stuck with the default responses and let it do it’s worst. You will probably want to read the HomeBrew page [HERE] first, before just trusting me.


david$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Next step was to run the Brew Doctor and address any major issues raised there, of which I had a few. The most pressing of which was this:

Warning: Xcode is installed to a directory with a space in the name.
This will cause some formulae to fail to build.

I deleted XCode 4.3.3 (my latest installed version) and re-installed which gave me version 4.6.2. Next I had to install the ‘Command Line Tools’ (no longer embeded in XCode). That can be found in the XCode Preferences -> Downloads.

I had a few other issues, stray libraries from botched builds past. I worked through my issued and then tried to installing components.

Since Boost seems to be very commonly referenced, I went ahead and installed it with Brew as it’s first task:

Installing Boost using Brew

Then went about installing boost with HomeBrew, since it did not seem to get all the components I needed when installed via MacPorts/Fink.


david$ brew install boost

This appears to have installed boost-1.53.0

* DONE *

Now, you may Brew!