Share this page

# Record a Kafka script

You can record a Kafka protocol script in VuGen to monitor the health of your Kafka deployment.

In this topic:

## Record a Kafka script

Configure the necessary settings and record a Kafka protocol script in VuGen.

To record a Kafka script:

1. Prerequisites:

   - Before working with Kafka scripts, make sure your Java environment is set up. For details, see Set up the Java environment.
   - You must have a working Kafka broker.
   - You must have a configuration file in VuGen for the Kafka consumer, containing the mandatory configuration properties. If you have not created or imported a configuration file in VuGen, see Consumer configuration.

2. In VuGen, create a new Kafka protocol script. The Kafka script template opens in the VuGen editor.

3. (Optional) Set the code generation options for the script, using the Kafka > Code Generation node of the recording options:

   1. Choose where the producer configuration code is saved. By default, the producer configuration code is saved in the script file.

      Note: After you record the script, you need to locate the producer configuration code and modify it in order to replay the script.

   2. Enable VuGen to insert a consumer instance at the end of the script, if relevant. This consumer's purpose is to poll for all records sent by the producer during replay. This consumer can also be set to commit these records. If the records are committed, they are not consumed again by any subsequent consumers that poll for records.

4. Click the Record button in the VuGen toolbar. The Start Recording dialog box opens.

5. In the Configuration dropdown, select the configuration file for the consumer.

6. In the Topics field, add the Kafka topics to which the consumer subscribes.

7. Click Start Recording and record the script.

8. After you finish recording, modify the producer configuration. For details, see Producer configuration.

## Producer and consumer configuration

VuGen's Kafka protocol provides an encapsulated producer and consumer per Vuser. The producer and consumer configurations are each set at different stages of recording the script.

### Consumer configuration

The consumer is configured based on a configuration file that you create and select before you begin recording. You must include certain properties in the configuration file to record a script. Consumer configuration files created or imported in VuGen are saved, and can be reused for later Kafka recordings.

To create a configuration file:

1. Open a Kafka script.
2. In the VuGen toolbar, select Record > Recording Options.
3. Use the Kafka > Configuration Files node of the recording options to create a configuration file. For details, see Kafka > Configuration Files.

Example :

### Producer configuration

When you record a script, the producer is configured automatically based on shared properties copied from the consumer configuration. After you finish recording, you modify the producer configuration to define certain mandatory properties. You must define these properties for the producer in order to replay the script. You can also add or modify other properties as needed.

You can modify the producer configuration in two ways:

- Insert the LrKafkaProducer.reconfigureProducer API into the recorded script. For details, see Kafka Functions in the Function Reference.

- Manually edit the code that defines the producer configuration. By default, this code is located in the recorded Kafka script. However, depending on the code generation settings used when the script was recorded, the code may be located in a .properties file saved in the same folder as the script.

For details on the mandatory properties, shared properties, and other properties available for producer and consumer configuration, see Kafka consumer and producer properties.

## Code generation

Up to version 26.1, code generation in Kafka scripts was based on XML raw deserialization, using the lr.deserialize Java function. The script output using XML representation looks like this:

Copy code

    _producerrecord1 = (ProducerRecord)lr.deserialize(
            "<org.apache.kafka.clients.producer.ProducerRecord>" +
            " <topic>" + _topic1 + "</topic>" +
            " <headers
    class=\"org.apache.kafka.common.header.internals.RecordHeaders\">" +
            " <headers/>" +
            " <isReadOnly>false</isReadOnly>" +
            " </headers>" +
            " <key class=\"long\">0</key>" +
            " <value class=\"string\">This is record 0</value>" +
            "</org.apache.kafka.clients.producer.ProducerRecord>");
    _recordmetadata1 = LrKafkaProducer.send(_producerrecord1);

From version 26.3, Kafka scripts automatically use a fluent Builder API, LrKafkaProducer.newRecord, which produces code that is more readable and easier to maintain. The script output using Builder pattern looks like this:

Copy code

    _producerrecord1 = LrKafkaProducer.newRecord(_topic1)
            .withKey(0L)
            .withValue("This is record 0")
            .build();
    _recordmetadata1 = LrKafkaProducer.send(_producerrecord1);

Note:

- Kafka scripts recorded in 26.1 or earlier will continue to work.

- If your script contains complex cases of classes and objects, or custom classes, all code in the script may be generated using the XML deserialization method. For details, see Kafka serializers/deserializers.

## See also

- Kafka protocol
- Kafka recording options
- Record a Vuser script
- Start Recording dialog box
- Kafka functions

https://admhelp.microfocus.com VuGen,Professional Performance Engineering,Performance Engineering for Developers 8942
Tips to improve your search results... OpenText™ Virtual User Generator Help Center OpenText™ VuGen Help 26.1-26.3 All View Local Help Center View Online Help Center Version: