Thursday, November 10, 2011

jQuery's Ajax POST Parameters

Most of the AJAX requests on our site utilize jQuery's $.post() method, building up a datastring of key/value pairs and sending that string right on through. Because of this, I had to manually replace all & characters with their URL equivalent, %26. But in one case, I discovered that the % was being converted to %25, resulting in a string of %2526...not at all what I wanted.

It turns out that it matters whether you send a datastring or a map. This is taken from jQuery's API referencing the $.ajax() method:


The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false.

Unfortunately, it does not seem possible to set processData from $.post(), which is simply a wrapper for $.ajax(). So, to be consistent, all of our AJAX requests should either utilize maps (thereby automatically escaping URL values), or the individual variables will have to be escaped manually. The first option seems to be a lot cleaner, but most of our requests use the latter form. What to do, what to do...

Tuesday, April 5, 2011

Using Performance Dashboard to Increase Performance on SQL Server 2005

There is a script that runs on one of our servers that scrapes the output of a log page and emails the result to a set of admins.  Lately, however, the log databases have grown so large that some of the SQL queries run by this page were timing out.

Luckily, I found out about SQL Server Performance Dashboard Reports, a set of reports you can install to find out, among other things, where all the CPU is going, which queries are the most resource consuming, and how to add indexes to increase performance.

Installation

To get started, simply download the installer from the link above and install it on the machine running SQL Server.  My default installation directory looked like this:

C:\Program Files\Microsoft SQL Server\90\Tools\PerformanceDashboard

Inside this directory are all the files needed to run the reports, a setup script, and a helpful CHM.  In addition to running the installer, a system administrator needs to run the setup.sql script located in this directory on every instance of SQL Server that you want profile.

Accessing Custom Reports

Then, to access the report in SQL Server Management Studio, right click on your server instance, select "Reports" then "Custom Reports" and open the performance_dashboard_main.rdl report in the installation directory.

Browse Expensive Queries and Create Missing Indexes

You can now browse the most expensive queries by CPU, Duration, Logical Reads/Writes, Physical Reads, or CLR Time.  Under "Miscellaneous Information", I found a link for "Missing Indexes".  This brought up a list of 4 indexes that it recommended that I create to improve database performance.  Suffice it to say that after creating the suggested indexes, CPU time was reduced an order of magnitude for the script.

For more information, I recommend this article:

http://www.sql-server-performance.com/articles/per/bm_performance_dashboard_2005_p2.aspx

Monday, April 4, 2011

Copy Text to Clipboard in Ubuntu from the Command Line

I'm writing a small Perl script that reads in some files and does some calculations and spits the text to STDOUT. All I ever do with this text is copy it to my clipboard and paste it in an Excel spreadsheet.  But I wondered...is there some way I can copy the text to my clipboard without having to select exactly what I want and hitting Ctrl-Shift-C?  The answer is surprisingly easy!

The following was done on Ubuntu:

$ sudo apt-get install xsel
$ echo -n "Put me in the clipboard." | xsel -ib
$
[hit Shift-Insert]
$ Put me in the clipboard.

Voila! The -i switch tells xsel to work in input mode (as opposed to append or follow mode) and the -b switch tells it to work with the clipboard.  Check out the man page for more cool stuff you can do with it.

Tuesday, January 18, 2011

SQL Server 2008 Job "Syntax Error"

I was attempting to create a job in SQL Server 2008 Management Studio that simply performs a SELECT on a table and emails the results.  It worked fine in Pre-Production, but in the Production environment, I got this strange error in the Job History Log:

The job failed.  The Job was invoked by User ....  The last step to run was step 1 (...).
  Unable to start execution of step 1 (reason: line(1): Syntax error). The step failed.

This was NOT a syntax error with my T-SQL statement, but rather this refers to the SQL statement that invokes the job.  These are the steps I had to take to fix the problem:


  1. Navigate to SQL Server Agent -> Jobs -> [Job Name] -> Properties -> Steps -> [Step that is causing troubles] -> Edit -> General -> General -> Package
  2. Add a backslash (\) to the front of the package.  (For example, change "Maintenance Plans\GroupRequestQueue" to "\Maintenance Plans\GroupRequestQueue".)
  3. Hit "Ok" twice, and try running the Maintenance Plan again.

Tuesday, January 11, 2011

For Fun: Unicode Text Obfuscator

ƞŐ÷ řĘâƚƚ¥ ŚŮŗƎ ƕòŴ ŲƨĚƭůľ şòmÊƬĦÏñǥ ŀĪǩÉ ƫĥÎƩ Ɯöúłď ƂĘ, þÙƬ ıŢ'ƨ ƑŬÑ ŢƠ Þ£Åÿ ÄřÖÙÑƊ ƜıƮħ. ţŘÿ ƈƟñvƐŕŦĮƝĝ ŷòƲŕ ëmÅïĺ§ ǂÕ ŨʼnįĊŎđƐ 1ƺƺ7 ÅňĎ ŚēÈ ŵƕåŢ ÝòÜŔ ƂÕƩ§ ŠÅ¥§...

Translation: Not really sure how useful something like this would be, but it's fun to play around with. Try converting your emails to Unicode 1337 and see what your boss says...

http://www.unicodeobfuscator.com/

Friday, January 7, 2011

Repeatedly Calling A Shell Program In Ruby

I'm writing a GA in Ruby just for fun, but as I ran it for the first time, I realized that my fitness function (a game simulation) was far too slow.  So I rewrote my fitness function in C and tried unsuccessfully to call it inline in Ruby using the RubyInline gem.  Instead, I compiled it as a separate program and called it from Ruby, using the backtick operator.

Of course, I have to call the program once for every individual in the population, and initializing a program in a tight loop is very CPU intensive.  What makes more sense is to move the loop to the C program, open a connection once in Ruby, and send a message to the C program telling it to run a simulation.  The C program runs, outputs the fitness, and waits for more input.  This way, initialization is only done once, reducing the overhead to create the address space for a new process every single time through the loop.

Here is how I accomplished this in Ruby:

#!/usr/bin/ruby
class Simulator
  def initialize
    @fh = IO.popen("./c_simulator", "w+")
  end


  def simulate(parameters)
    @fh.puts parameters
    @fh.gets.to_i
  end
end


...


sim = Simulator.new
population.each{|p|
  p.fitness = sim.simulate(p.getParameters)
}


IO.popen(prog, mode) opens a persistent connection to another process.  The mode "w+" means an IO stream is opened for reading and writing to/from the process.  You can then puts and gets to the handle just like you would from a file.  And then on the C side:

int main(int argc, char** argv) {
  char line[4096];
  char *cptr;
  do {
    cptr = fgets(line, 4096, stdin);
    if (cptr) {
      // Do processing, assign fitness to fitness
      printf("%d\n", fitness);
      fflush(stdout);
    }
  } while (cptr != NULL);
  return 0;
}


Fairly efficient way to call an external program from a Ruby loop, provided you are writing the external program yourself.

Tuesday, December 7, 2010

Setting up a RAM disk to speed up WMC7

After setting up my media center PC and playing around with Windows Media Center and Media Browser, I was less than enthused with browsing performance.  Figuring it was probably a disk problem with C:\Program Data\Media Browser, I looked into putting it on a RAM Disk.  I found a free program over at Dataram's web site called RAMDisk.  (The free version lets you create a disk up to 4GB. Higher than that costs $10.)  Just to try things out, here's a benchmark showing side-by-side comparisons of my hard drive's speed to that of a 100MB RAM disk using CrystalDiskMark.


On my HTPC, I created a small RAM disk (200MB should be enough for me...I hope!) and moved C:\ProgramData\MediaBrowser to it.  Bring up the command console and create a hard link: mklink /J C:\ProgramData\MediaBrowser F:\MediaBrowser.  Then boot up WMC and try it out.  Loading MediaBrowser seemed to be a bit faster, but the big speed increase came when I accessed my DVD collection.  Normally, it takes a long time to load all of the genre icons (that are stored in the MediaBrowser\Images By Name\Genre directory), but now that they are stored on the RAM disk, they loaded instantly!

You can configure RAMDisk to load from and save to a disk image, so the information should be persistent.  You can even have it save to disk at a regular interval.  I've only been using this setup for a day, so I'm not sure how reliable this method is, but it seems to be working fairly well so far.