Register now and start sharing your code snippets.

How to debug memory allocation problems in PHP with Xdebug

PHP posted 18 days ago by christian

Xdebug is a good tool for finding the root cause of memory allocation problems such as the one shown here:

   1  Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes)

First install Xdebug by following the Xdebug installation instructions.

Next surround the code you suspect is causing the problem with the following function calls:

   1  xdebug_start_trace('/tmp/mytrace');
   2  ...
   3  Bad bad PHP code
   4  ...
   5  xdebug_stop_trace();

Read this blog entry by splitbrain.org for more details

Tagged xdebug, php, memory, error, performance

How to benchmark your Ruby code

Ruby posted 11 months ago by christian

You can easily benchmark your Ruby code like this:

   1  require 'benchmark'
   2  seconds = Benchmark.realtime do
   3  	sleep 1
   4  end
   5  print "#{seconds} elapsed..."

The output should be close to 1 second.

Tagged benchmark, performance, ruby

Find the amount of CPU time and total time runtime for Oracle SQL queries

SQL posted about 1 year ago by christian

There’s no equivalent to the MySQL slow query log in Oracle but these queries and the statspack reports are helpful when trying to find slow queries:

   1  -- Order queries by how many cpu seconds they consume
   2  SELECT   hash_value, executions, ROUND (elapsed_time / 1000000, 2) total_time,
   3           ROUND (cpu_time / 1000000, 2) cpu_seconds
   4      FROM (SELECT   *
   5                FROM v$sql
   6            ORDER BY elapsed_time DESC)
   7  ORDER BY cpu_seconds DESC;
   8  
   9  -- Find the total cpu seconds consumed
  10  SELECT SUM (cpu_seconds)
  11    FROM (SELECT   hash_value, executions,
  12                   ROUND (elapsed_time / 1000000, 2) total_time,
  13                   ROUND (cpu_time / 1000000, 2) cpu_seconds
  14              FROM (SELECT   *
  15                        FROM v$sql
  16                    ORDER BY elapsed_time DESC)
  17          ORDER BY cpu_seconds DESC);
  18  
  19  
  20  -- Find the total time queries have taken
  21  SELECT SUM (total_time)
  22    FROM (SELECT   hash_value, executions,
  23                   ROUND (elapsed_time / 1000000, 2) total_time,
  24                   ROUND (cpu_time / 1000000, 2) cpu_seconds
  25              FROM (SELECT   *
  26                        FROM v$sql
  27                    ORDER BY elapsed_time DESC)
  28          ORDER BY cpu_seconds DESC);
  29  
  30  -- Find execution plan for the damned query you should fix
  31  SELECT *
  32    FROM v$sql_plan
  33   WHERE hash_value = 2967942512;

Tagged oracle, performance

Log file analysis with AWK - Calculating the sum and average

Shell Script (Bash) posted about 1 year ago by christian

This AWK script is useful when you want to calculate the average and sum for a set of values found in a log file.

   1  awk '{ s += $1 } END { print "sum: ", s, " average: ", s/NR, " samples: ", NR }' rails_production_log_or_whatever.log

Note that $1 means that column one contains the values you want to use. NR is the total number of rows in the file. As an example, let’s say you have this log file:

   1  1
   2  2
   3  3
   4  4
   5  5

The output would then be:

   1  sum:  15  average:  3  samples:  5

Combine it with grep or sed to do more advanced log file analysis —you can for example calculate the average time it took to render action xyz in Rails on the 21th of July at 21:00 PM.

Note that by default the column values should be space separated—use the following switch to parse CSV (comma separated) files: -F,

Tagged awk, shell, performance, average, sum