Knowledgebase

Generating UUIDs


Background

Scripting example on how to generate UUIDs in your load test.


UUID

Generate v1 and v4 UUIDs

Universally unique identifier are handy in many scenarios, as k6 doesn’t have built-in support for UUIDs we’ll have to resort to using a Node.js library called uuid and Browserify (to make it work in k6).

There are a few of steps to make this work:

  1. Make sure you have the necessary prerequisites installed:
  2. Install the uuid library:

    npm install uuid

  3. Run it through browserify:

    browserify node_modules/uuid/index.js -s uuid > uuid.js

  4. Move the uuid.js file to the same folder as your script file and you’ll be able to import it into your test script:

    import uuid from "./uuid.js"

Here’s an example generating a v1 and v4 UUID:

1
2
3
4
5
6
7
8
9
10
11
import uuid from "./uuid.js";

export default function() {
    // Generate a UUID v1
    let uuid1 = uuid.v1();
    console.log(uuid1);

    // Generate a UUID v4
    let uuid4 = uuid.v4();
    console.log(uuid4);
}