How can I measure the performance of my q code?

The simplest way to time something in q is to use the \t command:

q)f: {[] do[100000; 2 + 2]}
q)\t f[]
10
q)

\t executes its argument and returns the number of milliseconds that elapsed – i.e., wall clock time (except when you are using \t in its other sense, i.e., setting or getting the time between timer events).

When using kdb as a database server, you will want to use one or more of Simon’s logging scripts as a starting point. These tremendously useful scripts enable you to record every incoming query as well as measure the time taken by every incoming query. That way you can calculate summary statistics on actual database performance in order to find and fix intermittent problems.

How do I use the timer?

If you defined a function called .z.ts, and enable q’s timer with \t n then q will call .z.ts every n milliseconds:

q).z.ts: {[] show .z.T}
q)\t 500
q)07:41:15.147
07:41:15.647
07:41:16.147
07:41:16.647
07:41:17.147
\t 0
q)

As shown above, \t 0 stops the timer.

To set the timer interval, you must give \t a literal integer. What that means is that, if you want to change the timer frequency at runtime (e.g., when building a simple scheduler), the following will not work:

q)i: 500
q).z.ts: {[] show .z.T; system “t i”; i *: 2}
q)\t 500
q)07:46:47.106
07:46:47.606
07:46:48.106
07:46:48.606
\t 0
q)

Notice what happens when we enter \t i at the repl:

q)\t i
0
q)

What we’ve done is use \t to measure the execution time of the expression i. To get the effect we want is, fortunately, easy once you see what is going on:

q)i: 500
q).z.ts: {[] show .z.T; system “t “, string i; i *: 2}
q)\t 500
q)07:59:59.694
08:00:00.195
08:00:01.195
08:00:03.196
\t 0
q)

By passing “t “, string i instead of “t i” to system, the q interpreter sees a literal integer.

One last detail: if you decide you need to delete the definition of .z.ts, you’ll need to use \x.

I want to keep kdb rdb super fast and prevent users from introducing large latencies by using a timeout argument of 2 seconds. is this a good idea?

It is true that no single user query will block for more than the time specified in the -T command line argument. However, be sure to disable timeout via \T 0 prior to executing the end-of-day batch processing, or it will fail with a ‘stop error.


This work is licensed under a Creative Commons License.
The views and opinions expressed herein are those of the authors and do not necessarily reflect those of any other person or legal entity.
Kdb+ is the registered trademark of Kx Systems, Inc.