Using the VuGen JavaScript engine

Applies to: Web - HTTP/HTML and SAP - Web Vuser scripts written in C only.

What is the VuGen JavaScript engine?

Typically, Web Vuser scripts contain C code. The built-in JavaScript engine enables you to insert snippets of JavaScript code into the C code.

What can I do with JavaScript in a Vuser script?

You can insert JavaScript code into a Web - HTTP/HTML or SAP - Web Vuser script to manipulate text strings that are included in the request and response messages that are sent between the client and server. Manipulating strings is often useful for correlation and parameterization purposes. Typical string manipulations include converting decimal to hexadecimal, encoding and decoding Base64, URL encoding and decoding, and accessing object values inside JSON-formatted data.

Note: It is possible to perform many of these string manipulation procedures by using the built-in DFEs (Data Format Extensions). For details, see Data format extensions (DFEs) overview.

Inserting JavaScript code into a Vuser script may also be useful when client-side logic is implemented in JavaScript. Inserting snippets of the original client-side JavaScript code into the Vuser script removes the requirement of having to re-write the JavaScript logic into C code to be included in the Vuser script.

You can use JavaScript code in a Vuser script to execute an XMLHTTPRequest. This allows you to generate and send HTTP or HTTPS requests using standard Javascript APIs. Such APIs include, for example, sending asynchronous requests, assigning callbacks to handle responses, reading responses in XML format. An XMLHTTPRequest used this way may replace a call to an action step such as web_url or web_custom_request.

Back to top

Why use JavaScript snippets?

Although it may be possible to achieve the required functionality by using C code alone, including JavaScript in a Vuser script may be beneficial for the following reasons:

  • JavaScript often offers a more intuitive, easier to implement solution than C.
  • The JavaScript regular expression library simplifies the challenge of working with regular expressions.
  • There are numerous JavaScript libraries that assist with string manipulation.
  • Client-side logic is often implemented in JavaScript. Inserting snippets of the original JavaScript code removes the requirement of having to translate the JavaScript client logic into C code.

Back to top

Can I use the JavaScript engine in Vuser scripts of all protocols?

No, the JavaScript engine enables you to insert JavaScript into Web - HTTP/HTML and SAP - Web Vuser scripts only.

Back to top

What are some scenarios in which the JavaScript engine may be useful?

Including JavaScript code in a Vuser script may be useful in the following scenarios:

Back to top

What are the API functions that I can use in a Vuser script to execute Javascript code?

The following API functions are available for including JavaScript in a Vuser script:

  1. web_js_run: Runs the specified JavaScript code.

  2. web_js_reset: Clears the JavaScript context.

You use the web_js_run function to include JavaScript code in a Vuser script. Using the web_js_run function, you can either insert the required JavaScipt code into the Vuser script, or you can reference a file that contains the required JavaScript code.

For details on the above functions, and examples of how they can be used, see the Function Reference (select the relevant version).

Back to top

Can I use JavaScript to access any "internal" API functions?

JavaScript in a Vuser script gives you access to a number of "internal" API functions that can be called directly from a web_js_run function in the JavaScript code. These functions are used primarily for managing parameters, but also enable you to log specified messages, record data, and run XMLHTTPRequest.

Back to top

How do I enable the Javascript engine for Vusers?

To run JavaScript from within a Vuser script, you must enable the JavaScript engine for the Vuser script. To enable the JavaScript engine, open the Replay > Runtime Settings > Internet Protocol > Preferences view. Go to the JavaScript section and select the Enable running JavaScript code option.

Note: Enabling this option causes the creation of a JavaScript Runtime engine, even if there are no JavaScript steps in the script.

Back to top

How do I configure the JavaScript engine?

You use the Vuser script's runtime settings to configure the product's custom JavaScript engine.

To access the JavaScript engine (JSE) runtime settings, select Replay > Runtime Settings > Internet Protocol > Preferences, and expand the JavaScript section.

  • JavaScript Engine runtime size: Specifies the size of the allocated JavaScript engine Runtime memory, in kilobytes. This value may need to be increased when running a large number of Vusers.
  • JavaScript Engine stack size per-thread: Specifies the size of each Vuser thread in the JavaScript engine memory, in kilobytes. This value may need to be increased for large objects or deep stack calls.

For user interface details, see Preferences view - Internet protocol.

Back to top

What is the connection between the VuGen's JavaScript engine and VuGen's JavaScript protocol?

There is no connection between VuGen's JavaScript engine and VuGen's JavaScript Protocol.

Back to top

Troubleshooting

If you encounter difficulties when implementing JavaScript engine support, review the items below for possible solutions.

  1. Make sure that VuGen's JavaScript engine is enabled. For details, see How do I enable the JavaScript Engine?
  2. Javascript limits you to adding up to 9,000 operands in one function. For example, if you are combining strings, "str1"+"str2"+"str3"+..."str9000", you can only add up to 9,000 strings.
  3. Memory issues

    • If the Simulate a new user on each iteration > Clear cash on each iterationruntime setting is selected, web_js_reset is called automatically at the start of each iteration.
    • If Simulate a new user on each iteration > Clear cash on each iteration is not set, avoid excessive memory consumption by inserting web_js_reset calls in your Vuser script at points where you no longer need the saved context.

    For details on the runtime settings, see the hints below the option in the runtime settings view.

  4. Performance issues

    If you are experiencing performance issues, modify the JavaScript runtime settings. For details, see Preferences view - Internet protocol.

Back to top