Qurbit Blog
Slaying My Laplacean Demon
Slaying My Laplacean Demon
Nov 3rd
Recently I have been working on improving the performance in one of my clients Rails applications, trying to get page load times down. Looking at the query log it was obvious that there was way too much activity going on for what was being rendered on the page–there were N+1 queries all over the place. This was nothing a few joins (or eager loads as the rails folks like to say) wouldn’t fix. So after changing the queries to make sure that all the necessary data was gathered in just one pass, I wondered how many other places this was cropping up in the application. That is when I stumbled upon flyerhzm’s Bullet Gem.
In short the gem monitors the queries that your server is making and notifies you as to whether or not you are encountering any N+1 scenarios. It also notifies you as to whether or not you have unused eager loads. It can be configured to notify you via javascript alerts, growl notifications, firebug notifications, or just log the instances.
Thus far it has been invaluable in finding instances where an eager load would help. It can (and should) be configured to run only in Development mode and will sit in the background until it has something to notify you about, so you can just turn it on, keep coding, and as you browse around your local instance, get notified as to potential areas for optimization.
While it is definitely worth installing a few things to note:
If you have your development.rb file in source control and shared among developers, one slight modification to the code on the github site that removes the gem dependency from your fellow developers is as follows:
config.after_initialize do begin require 'bullet' Bullet.enable = false Bullet.alert = false Bullet.bullet_logger = true Bullet.console = true Bullet.rails_logger = true Bullet.disable_browser_cache = true begin require 'ruby-growl' Bullet.growl = true rescue MissingSourceFile end rescue MissingSourceFile end end
Apr 14th
I have been doing development in PHP for a long time and have yet to find a really satisfactory solution on Windows. When I recently flattened my home PC and had to restart I thought I would try a new setup and here is what I’m using. Its pretty good to me so far, and I think I’m going to run with it for the time being:
For an IDE I am using EasyEclipse for LAMP which is a nice little package. It uses Eclipse as its base IDE but integrates plugins for PHP, Python, Ruby on Rails (not that good), Subclipse and others. Just download the latest build and install it.
To run locally you can install WAMP (I used to use this, it was good) or XAMPP. I am trying XAMPP for now because it is integrated nicely into Eclipse. Out of the box you will get MySQL, Apache, FileZilla and Mercury up and running if you install this.
At this point you can import directly from you subversion repository into the xampp\htdocs directory and you should have a working enlistment deployed (make sure Apache is started) to http://localhost/your_enlistment
There are a few more things that you might want to install, including PEAR and Memcached.
To install PEAR for XAMPP, open a command prompt (cmd.exe) as an Administrator (Vista only).
Then navigate the your xampp\php\PEAR\PEAR and execute the go-pear.phar script (its PHP) via:
php go-pear.phar
You should be able to use the default settings (unless you’ve tweaked your XAMPP installation). When prompted as to whether you would like to update your php.ini file, say Yes.
At this point you should be done and can now reference PEAR libraries in your PHP scripts.
If you need memcached you can get this up and running in a variety of ways. If you want to build from source you can find it here. Otherwise search for a pre-compiled windows32 version. Copy the binaries to some place on your development machine and then you will need to start and install the service.
To install the service open a command prompt as an administrator and navigate to the directory with memcached.exe. Install the service by typing: memcached -d install
To start the service type: memcached -d start
Note that the default reserved cache size for memcached is only 64MB, so if you would like it to be larger, launch the service and append a -m followed by the number of megabytes as an integer. So to launch and reserve a half gig you would launch it as: memcached -d start -m 512
Now that the service is running, you will need to let php know that it is. Make sure that you have the correct extension (php_memcache.dll) in the php\ext folder in your xampp directory. After you have verified that this is there, you should update the php.ini file and uncomment (remove the preceding “;”) the following line:
extension=php_memcache.dll
Make sure that this is the php.ini file associated with apache if you want to test this on your local pc (i.e. the one in your apache folder), otherwise you will just have access to this via the CLI version of PHP. Restart Apache and you should be good to go. If you are having issues make sure that there is a Memcache section in your phpinfo() output to verify that the extension is being loaded.
Hope this helps, leave any questions in the comments and I’ll try to help.
Jun 20th
Recently I have been wrestling with writing some code that will manipulate text on a user’s clipboard in a web browser. I thought I would share my findings here for others who might be interested in the same topic.
First lets start by looking at the Windows Clipboard in general. The clipboard, as we all know, stores blobs that the user has copied or cut from windows applications for future use. It stores this data in a variety of formats (if writing a windows application you can use the formats specified by the DataFormats class.) It is a convenient way to transport data between applications in windows. A simple example of writing to the clipboard in c# would then look like:
// For this example, the data to be placed on the clipboard is a simple // string. string textData = "I want to put this string on the clipboard."; // After this call, the data (string) is placed on the clipboard and tagged // with a data format of "Text". Clipboard.SetData(DataFormats.Text, (Object)textData);
Doing this on in a windows app is great, and easy, because that is really what the api’s were designed for… but what if we want to do this in a web app? Why would we do this you ask, well one argument is that as web apps and client apps converge, users are starting to expect more and more of the familiar windows UI concepts to be available to them on the web. Yeah, we’ll say that is the reason.
In a web app, there are primarily 3 ways that we are going to run code that will interact with our user: javascript, flash, and silverlight. We’ll take a look at each of these and see what is possible.
So what can we do with javascript and the clipboard? It depends on the browser and on the interaction that you want. Lets start with the simplest, and potentially cross browser action: execCommand. The execCommand method can perform a variety of operations, but the ones we care about are ‘copy’, ‘cut’, and ‘paste’. Because execCommand was built with enabling javascript based wysiwyg editors in mind, it operates on the selected areas of a web page. That is, text that has been highlighted or where there is an active cursor (e.g. in a text box). So how can we use execCommand? If you want to simply copy the selection to the clipboard, then it is trivial and you can just:
document.execCommand("Copy");
This copies the user’s selection to the clipboard.
What is good about the execCommand copy approach? It is potentially cross browser (not with default security settings in FF) and simple to implement. So long as they have javascript enabled you can use it. What is bad? In modern browsers the user will be prompted the first time that you attempt to do this and warned that you are trying to access their clipboard, which is a bad UX but necessary for security reasons.
Furthermore, you may only copy text that the user has selected in the browser.
For more examples of execCommand clipboard stuff, see this Geekpedia tutorial or this excellent JS WYSIWYG Editor tutorial.
An option, if for some strange reason you would like to build an application that only works in IE, is to take advantage of the clipboardData object. Using this, you can write and read directly from the clipboard and the user prompt is ignored when using the default security settings. Using this you can only take advantage of two ‘types’ on the clipboard: ‘text’ and ‘url’
To write text to the clipboard, you could then do the following:
var textToCopy = "This is some text for the clipboard"; window.clipboardData.setData("Text", textToCopy);
One nice thing about this, is that you can write arbitrary text to the clipboard (no user selection needed). But remember, this is Internet Explorer only. Mozilla does have a solution, but it is FF only. So if you want to use the clipboard in all browsers and not change security settings what can we do?
The best answer so far is flash. The idea (originally credited to Mark O’Sullivan) is to use the Flash setClipboard method to write to the clipboard. Why is this better than the javascript solutions? Because, the good people at Adobe did all the cross browser work for us. Now the only requirement is that the browser has a flash plugin, which given the penetration of flash is a fair bet. What we can then do is, when we want to copy something to the clipboard, we pass (via javascript) the the text to our flash object and let it do the clipboard dirty work.
Jeffothy Keyings has a good example of how to do this, and links to the open source flash object (_clipboard.swf) by O’Sullivan.
I looked and looked, and to the best of my knowledge Silverlight does not yet expose a method for accessing the clipboard. Alas.
So where does that leave us?
One more thing that is worth noting is that you can only write to and read from the clipboard from within the browser using the TEXT format. Why is that important? Because if you want to put images, or rich text on the clipboard you can’t. If you go into your browser and highlight some text with styles and then paste it into Microsoft Word, you see that the styles are preserved. That is because this is written to the Clipboard and read from it as RTF. Not only that, but if you paste it into Microsoft OneNote, you’ll find that you can see the original source of the clip. That is because as rich client apps, IE (or FF), Word and OneNote are not sandboxed in the same way that flash and javascript are. To see this more clearly try downloading Clipboard Inspector and looking at the format of items that you copy to the clipboard.
When clipping from a web browser you get the HTML option and there is header information showing source, etc:

When you write via javascript or flash you are forced to write out text, so when applications access the clipboard, even if you have valid HTML on there they will html encode the characters thinking they they are text, and thus not render them correctly. There are numerous reasons why this makes sense from a security perspective, but it is a pain and something that web developers need to be aware of nonetheless.