New ReflectionClass versus normal class
In the project I'm currently working on we attend great importance to testing. We looked for a way to replace our data models with mock objects (our current buzz word number one) for easier testing. The mock objects from simpletest are very cool, but we wanted an even simpler solution.
We decided to get our data models by a factory - without touching the existing models - to be able to replace them. A problem was that not all our constructors use the same footprint, which we solved by using ReflectionClass and newInstanceArgs. This is a quite expensive way but makes testing really easy.
Here is the code for my very simple profiling:
<?php
class magicfactory {
function __call($name, $params) {
$class = new ReflectionClass('reflection' . $name);
return $class->newInstanceArgs($params);
}
}
class reflectiontest {
function __construct($foo, $bar) {
}
}
$ref1 = microtime(true);
for ($x=0; $x < 1000000 ;$x++) {
$f = new magicfactory();
$r = $f->test('foo', 'bar');
}
$ref2 = microtime(true);$noref1 = microtime(true);
for ($x=0; $x < 1000000 ;$x++) {
$r = new reflectiontest('foo', 'bar');
}
$noref2 = microtime(true);
And that are the results, I clearly expected that the reflection calls will consume at least double the time since it has to instance two classes:
Reflection time: 16.925868034363s
No reflection time: 4.6512498855591s
The difference is about 0.012274618148804ms each run
Lock screen on osx with screensaver
This is mainly for myself because I search that snipped every time I set up a mac. I place it to /usr/local/bin/lock and then type lock in my shell (or LaunchBar) to fire the screensaver and lock the screen (locking needs to be turned on in the preferences).
#!/bin/bash SCRS=`ps xcopid,command|grep ScreenSaverEng|cut -b1-5` if [ -z "$SCRS" ]; then /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine fi
To safe some energy I don't use the screensaver but let blank the screen after a while.
Start memcached with launchd [Updated]
I let memcached automatically start with the OS X integrated launchd. Here is the XML I provided to start it with the following settings:
- Bound to localhost
- Runs as nobody
- Spawns only one thread (this can slow things down)
- Consume a maximum of 10MB memory (it will just throw old stuff out afterwards)
- It will start when you login
- It will send all output to /dev/null
Here is the updated script (I use macports now instead of fink just change the path to wherever your binary is). Just put the XML to ~/Library/LaunchAgents/memcached.plist and run launchctl load ~/Library/LaunchAgents/memcached.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>memcached</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/memcached</string>
<string>-l</string>
<string>127.0.0.1</string>
<string>-u</string>
<string>nobody</string>
<string>-t</string>
<string>1</string>
<string>-m</string>
<string>10</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/dev/null</string>
<key>StandardErrorPath</key>
<string>/dev/null</string>
</dict>
</plist>
View source in Safari with TextMate
Some days ago Lukas Smith asked me if it's possible to get source highlighting in Safari. For Firefox I used the ViewSourceWith extension for a long time to use TextMate. So now I wanted to get that in Safari too. Usually I use Safari to browse and Firefox to develop so it's not really necessary but a nice feature.
I found a cool plugin for Safari called SafariScript which ships a view source in TextMate script. But the script is pretty poor. It just fetches the URL via curl to a specified file, so you can only access public sites and only one page at time. The other scripts are very cool trough for example the "Open page in Firefox" script which I love when I opened the page in the wrong browser.
I came up with my own script which you can see bellow. It fetches the source directly from Safari instead of downloading the URL and it adds a timestamp and random number to the tempfile so it should be possible to open as much pages as you want. Just put it in place of the old script and add a cool keyboard shortcut (I use ⌘U as in Firefox).
property newline : ASCII character 10
set timestamp to do shell script ("date +%s") & (random number from 1 to 1024) as text
set tmpfile to POSIX path of ((path to "temp" as Unicode text) & "view_source" & (timestamp) & ".html ")
set tmpfileSpec to ((path to "temp" as Unicode text) & "view_source" & (timestamp) & ".html ") as file specification
tell application "Safari"
set htmlSource to source of document of front window as text
end tell
set fp to open for access tmpfileSpec with write permission
set eof of fp to 0
write (htmlSource) to fp starting at eof
close access fp
tell application "TextMate"
activate
open tmpfileSpec
end tell
do shell script "rm -f -- '" & tmpfile & "'"
Killeblatt Poker Tournament
Last week I participated in the Killerblatt Tourney. It was supposed to be a 20 table tournament but the organizers shortly decided to add more tables after they overbooked the pre-registration without extending the price pool. You had to win your first table which was 8-handed to advance to the finals (which started with three tables now).
The experience was fun because it was my first bigger live poker tournament. Most player were amateurs and didn't even know the rules - which was a bit annoying when they were up for dealing. The blind structure was very tough, because the big blind nearly doubled up ever 10 minutes, so it became pretty much gambling after a short time. At my table two people were knocked out in the first two hands, there was only one serious guy and one very agressive. The rest played too bad or tight to be really dangerous.
I advanced to the final table and finished in 10. place. Unfortunately only the top 3 ranks were really payed (iPhone, Poker Surface, Chips-Set) and place 4 to 9 became a free entry for the next tournament. I decided to not participate in further tournament but probably I will show up there again to rock some Sit & Go tables :) (Good payment structure there but very high rakes on the entry fee)
Here a picture before the beginning of the tournament (hey there was really air in the room at some point):

Enable tags in ecto3
Yesterday I wrote a little Patch to fix the tagging feature in ecto3. You will have to set the setting to the following:

I fixed the image upload too, but I didn't figure out how to use a customized image template to let FluxCMS generate the thumbnails instead of uploading them separately.
I got blogged! [Updated]
Beni took a picture of me while working hard.
Update: Yesterday it was very cold in our office. I was wearing my new St. Pauli hat from Hamburg but beni used another tactic of me:

