---
title: Sharing variables between Virtual Users
url: https://blog.octoperf.com/sharing-variables-between-virtual-users/
date: '2025-10-17'
authors:
- onedil
categories:
- Tutorials
tags:
- Tutorial
- Variable
- Performance Testing
description: How to use the AMQP technology to share variables between VUsers during your tests with Octoperf
---

# Sharing variables between Virtual Users

## Summary 

**Sharing dynamic values across Virtual Users** is often required in realistic load-testing scenarios.
AMQP provides a simple and reliable way to exchange data between scripts by pushing values into a queue and retrieving them later.
The tutorial demonstrates how one Virtual User can publish generated IDs to RabbitMQ, while another consumes them on demand.

CloudAMQP is used as the example broker, but **the method works with any AMQP-compatible system**.
OctoPerf integrates easily with these HTTP endpoints, allowing both insertion and retrieval of queued values.
Combined with JSON extractors, this approach makes cross-VU coordination straightforward and scalable.

## Table of Contents
- [Introduction](#introduction)
- [What is AMQP ?](#what-is-amqp)
- [Sharing variables through MQ between Virtual Users](#sharing-variables-through-mq-between-virtual-users)
- [Resources](#resources)


## Introduction

[Variables](https://api.octoperf.com/doc/design/edit-virtual-user/configuration/variables/), may they be constant or dynamic, are an essential part of load testing. 

We [already](https://www.iorad.com/player/2033662/Regexp?iframeHash=trysteps-1&lang=en) learned how to manipulate these values by extracting and re-injecting them inside one Virtual User.

But what if we need to share these values between several [Virtual Users](https://api.octoperf.com/doc/design/) ? 

In this blog post, I will show you just how to do that, using the [MQ technology](https://en.wikipedia.org/wiki/Message_queue).

## What is AMQP ?

[Advanced Message Queuing Protocol](https://www.amqp.org/) (AMPQ) is an open standard protocol that allows messages exchanges between different systems.

In practice, it lets you store values in queues, values that can be retrieved later by simple calls.

Here are the basic terms used while using AMQP:

### The queue

A queue stores messages/values, until they are consumed, in the manner of a buffer.

### Queue message

The value that you want to store in the queue, in order to retrieve it at a later time

### Exchanges

Several type of exchanges are available, they determine the way the message will be transferred to the queue(s)

### Binding

A binding is a rule that links a type of exchange to your queue(s)



## Sharing variables through MQ between Virtual Users 

In this exemple, we are going to use [CloudAMQP](https://www.cloudamqp.com/), a [RabbitMQ](https://www.rabbitmq.com/) powered [SaaS](https://en.wikipedia.org/wiki/Software_as_a_service).



Let's say we have 2 kinds of Virtual Users:

**VirtualUser1** is creating  customers in the application, which dynamically generates a *contactID*.

**VirtualUser2** is updating customers informations, by using several *contactID* generated by **VirtualUser1**



First, you will need to fill your CloudAMQP authentication information in Octoperf :

![Authentication](https://blog.octoperf.com/img/blog/variables-mq/auth.png)

Then, **VirtualUser1** you will need to insert a contactID everytime it is generated.

### Insert Value

The call, using CURL, should look like this :

```bash
curl -XPOST -d'{"properties":{},"routing_key":"my_key","payload":"my body","payload_encoding":"string"}' https://username:password@hostname/api/exchanges/vhost/amq.default/publish
```

``routing_key`` lets you use bindings if necessary, in our case we will put the name of our *destination queue* so that it will be sent directly to it. You will have to create that *destination queue* on your AMQP Admin Page. In the following exemple, the *destination queue* was named "**MQ**"

``Payload`` should contain the value injected.

In Octoperf, the request will be translated as the following :

![Insert Message](https://blog.octoperf.com/img/blog/variables-mq/insertmessage.png)

**VirtualUser2** will have to get one of these contactID by querying the queue.

### Query Value

The call, using CURL, should look like this :

```bash
curl -XPOST -d'{"vhost":"username","name":"your-queue","ackmode":"ack_requeue_false","encoding":"auto","count":"1"} https://username:password@hostname@whale.rmq.cloudamqp.com/api/queues/your_vhost/your_queue/get
```

`ackmode:ack_requeue_false` ensures that your value will be erased after being extracted. 

`count` lets you decide how many values you need to extract

In Octoperf, the request will be translated as the following :

![Query Value](https://blog.octoperf.com/img/blog/variables-mq/queryvalue.png)

Finally, you can extract the *contactID* from the response of that call using, for example, the [Octoperf JSON extractor](https://api.octoperf.com/doc/design/edit-virtual-user/action-types/post-processors/jsonpath-variable-extractor/) :

![Json Processor](https://blog.octoperf.com/img/blog/variables-mq/processor.png)

## Resources

If you would like to try it out yourself, you can use the following materials:

- Create a [Free AMQP account](https://customer.cloudamqp.com/signup),
- [Producer](https://blog.octoperf.com/img/blog/variables-mq/rabbitmqpost.jmx) virtual user,
- [Consumer](https://blog.octoperf.com/img/blog/variables-mq/rabbitmqretrieve.jmx) virtual user.

After importing, make sure to replace the credentials from the [servers menu](https://api.octoperf.com/doc/design/edit-virtual-user/configuration/servers/) and you should be good to go.
