LoadRunner Syntax Highlighting (for your blog or forum)

I like posting LoadRunner code snippets on My Load Test and, judging by the emails and comments that I get, a lot of people find them really useful. To make my code more readable, I have added syntax highlighting that uses the same colours you see in VuGen. If you would like to download a copy of the syntax highlighting code to use on your own blog or forum, or you would like to learn more about how I did it, read on…

There is a neat parser/highlighter called GeSHi, which stands for “Generic Syntax Highlighter”. This was originally developed for phpBB, but is now available for many other blogs and forums. If you are using WordPress, you will find that the WP-Syntax plugin uses GeSHi.

Even though LoadRunner supports a variety of languages (depending on the vuser type), just covering C (and maybe Java) should be enough for most of the code that I plan to share.

LoadRunner C

Here is a simple LoadRunner script as an example of how VuGen does syntax highlighting.

VuGen syntax highlighting example

As you can see (if you click on the image to get the larger size), the following elements are coloured differently…

  • Comments – Grey (RGB: 155,155,155). LoadRunner supports traditional C-sytle comments “/* */” and C++ style line comments “//”.
  • Strings – Green (RGB: 0,128,128). Any text that is in double quote (or single quotes – I guess VuGen’s parser isn’t trying to verify correctness).
  • Primitive Types – Blue (RGB: 0,0,255). Primitive types and their modifiers (short, int, long, void, char, float, double, const, enum, struct, union, typedef, etc)
  • LoadRunner Parameters – Pink (RGB: 192,0,192). Any part of a string that is in {CurlyBraces}. It has always annoyed me that VuGen does not highlight parameters created with web_reg_save_param or lr_save_param. This is my opportunity to fix this.
  • LoadRunner Functions – Brown (RBG: 138,0,0). The common LoadRunner functions that all start with “lr_”, and the protocol-specific functions that start with “web_”, “sapgui_”, etc.
  • LoadRunner Constants – Blue. LoadRunner has a huge number of constants (LAST, EXTRARES, LR_AUTO, LR_PASS, LR_FAIL, etc)
  • Control Structures – Blue. Looping and conditional statements (if, else, for, do, while, return, continue, break, switch, case, default, goto)
  • Preprocessor Directives – Blue. C preprocessor macro definitions (#define, #include, etc)
  • Operators, Functions, Variables – Black (RBG: 0,0,0). Basically, any language elements that are left over are coloured black.

Here is the code from the screenshot above in all its syntax-highlighted glory…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Action()
{
    int i;
 
    // Google search for 10 different search terms.
    for (i=0; i<10; i++) {
 
        lr_start_transaction("Search");
 
        web_reg_find("Text=Your search - <b>{SearchTerm}</b> - did not match any documents.", "SaveCount=DidNotMatch", LAST);
        web_reg_find("Text={SearchTerm} - Google Search", LAST);
        web_url("search", 
            "URL=http://www.google.com.au/search?&q={SearchTerm}", 
            "Resource=0", 
            "RecContentType=text/html", 
            "Referer=http://www.google.com.au/", 
            "Snapshot=t2.inf", 
            "Mode=HTTP", 
            LAST);
 
        // If no results are returned for the search, fail the transaction.
        if (atoi(lr_eval_string("{DidNotMatch}")) != 0) {
            lr_error_message("No results found for %s", lr_eval_string("{SearchTerm}")); // Should have "Fail open transactions on lr_error_message" enabled in runtime settings.
        }
 
        lr_advance_param("SearchTerm");
 
        lr_end_transaction("Search", LR_AUTO);
 
        lr_think_time(5); // Limit search rate or get banned by Google.
    }
 
    return 0;
}

If you would like to add this feature to your own blog, install GeSHi (or a plugin which uses it), and drop the c_loadrunner.php file I created into the ./geshi directory that contains the files that support all the other languages. Enjoy.

6 Responses to “LoadRunner Syntax Highlighting (for your blog or forum)”

  1. Stuart Moncrieff says:

    Note for people who want to use my code with WP-Syntax; you need to follow these steps…
    1. Install WP-Syntax (obviously your WordPress blog is already up and running), and enable this plugin.
    2. Download my c_loadrunner.php file (remove the “.txt” extension I gave it), and copy it to ./wp-content/plugins/wp-syntax/geshi/geshi
    3. Add some code to a blog post inside pre tags like <pre lang="c_loadrunner">lr_output_message("Hello World")</pre>
    4. Rejoice, because your code looks just like it does in VuGen.

    BUGS
    * If your code is within <pre> tags, then the browser will handle tab size for you. To guarantee that your visitors will see tabs that are 4 spaces wide, you can replace your tabs with…four spaces.
    * LoadRunner parameters are not highlighted. I can’t get this working. I have sent a question about this to the GeSHi developers mailing list. Fixed.

    • Stuart Moncrieff says:

      Benny Baumann (a GeSHi developer) pointed me in the right direction to fix my bug with highlighting of LoadRunner parameters.

      I have updated the LoadRunner Language File with the bugfix.

  2. Hi Stuart,

    I saw your blog post and thought I’d use the code, but this link doesn’t work.
    http://www.myloadtest.com/files/c_loadrunner.php.txt

    Is there any chance that you can repost the code?

    All the best
    Richard

    P.S. Great site, thanks for sharing..

    • Stuart Moncrieff says:

      Sorry Richard. I had removed the file temporarily due to the bugs mentioned above but, even with the outstanding bugs, I guess it is still usable. :)

      I have put the GeSHI language file for LoadRunner back up.

      Please send me an email (or leave a comment) if you find more bugs or have any feature requests.

  3. Hi Stuart,

    Thanks for posting this. I’ve been looking for an easy way to get LoadRunner syntax highlighting into my documents and reports for some time now. I followed your advice and installed GeSHi together with your php file on my website.

    I’ve published the resulting GeSHi tool so that I can use it wherever I have an Internet connection so that I can use it regardless of the client site where I’m working. http://bish.co.uk/geshi/example.php

    I’ve written an article about this for my website and linked to your site. I hope that you don’t mind.
    http://www.bish.co.uk/index.php?option=com_content&view=article&id=104:loadrunner-syntax-highlighting-with-geshi&catid=34:recent&Itemid=1

    Thanks once again for your efforts for the wider testing community.

    All the best
    Richard

    • Stuart Moncrieff says:

      Nice one Richard, I will have to use your tool next time I need to cut and paste syntax highlighted LoadRunner code into a Word Document or email.

      Cheers,
      Stu.

Leave a Reply