End-to-end testing with fake data

I try to create a script, where each user sends a request with a fake first name in the body. For this activity, I used a faker. In my script are 2 vus, which send requests, but each request has the same name, which was generated using faker. Which elements I should fix when I want to send a different name on each request, not the same?

My script with a test case:

My function, which generates the fake names:

Hi @Mada

I was testing and example based on GitHub - grafana/k6-example-data-generation: Example repository showing how to utilise k6 and faker to load test using generated data, and what I find is that each VU iteration the faker data changes for me:

import { sleep } from 'k6';
import http from 'k6/http';
import { Rate } from 'k6/metrics';
import { vu } from 'k6/execution';
import faker from 'https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js'

export const options = {
    discardResponseBodies: true,
    scenarios: {
        form: {
            executor: 'per-vu-iterations',
            vus: 2,
            iterations: 30,
            maxDuration: '100s',
        },
    },
    thresholds: {
        'failed form submits': ['rate<0.1'],
        'failed form fetches': ['rate<0.1'],
        'http_req_duration': ['p(95)<400']
    }
};

export const generateSubscriber = () => ({
    name: `${faker.name.firstName()} ${faker.name.lastName()}`,
    title: faker.name.jobTitle(),
    company: faker.company.companyName(),
    email: faker.internet.email(),
    country: faker.address.country()
});

const baseUrl = 'https://httpbin.test.k6.io/anything';
const urls = {
    form: `${baseUrl}/form`,
    submit: `${baseUrl}/form/subscribe`,
};

const formFailRate = new Rate('failed form fetches');
const submitFailRate = new Rate('failed form submits');

const getForm = () => {
    const formResult = http.get(urls.form);
    formFailRate.add(formResult.status !== 200);
}

const submitForm = () => {
    const person = generateSubscriber();
    console.log('vu: ', vu.idInTest, 'iteration: ', vu.iterationInScenario, 'person.name: ', person.name);
    const payload = JSON.stringify(person);
    const submitResult = http.post(urls.submit, payload);
    submitFailRate.add(submitResult.status !== 200);
}

export default function () {
    getForm();
    submitForm();
    sleep(1);
}

The output:


          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: test.js
     output: -

  scenarios: (100.00%) 1 scenario, 2 max VUs, 2m10s max duration (incl. graceful stop):
           * form: 30 iterations for each of 2 VUs (maxDuration: 1m40s, gracefulStop: 30s)

INFO[0000] vu:  2 iteration:  0 person.name:  Georgette Cole  source=console
INFO[0000] vu:  1 iteration:  0 person.name:  Georgette Cole  source=console
INFO[0002] vu:  2 iteration:  1 person.name:  Rory Reichel  source=console
INFO[0002] vu:  1 iteration:  1 person.name:  Rory Reichel  source=console
INFO[0003] vu:  2 iteration:  2 person.name:  Raymond Parker  source=console
INFO[0003] vu:  1 iteration:  2 person.name:  Raymond Parker  source=console
INFO[0004] vu:  2 iteration:  3 person.name:  Palma Mills  source=console
INFO[0004] vu:  1 iteration:  3 person.name:  Palma Mills  source=console
INFO[0005] vu:  2 iteration:  4 person.name:  Henry Koelpin  source=console
INFO[0005] vu:  1 iteration:  4 person.name:  Henry Koelpin  source=console
INFO[0006] vu:  2 iteration:  5 person.name:  Lauretta Schulist  source=console
INFO[0006] vu:  1 iteration:  5 person.name:  Lauretta Schulist  source=console
INFO[0008] vu:  2 iteration:  6 person.name:  Russ Carter  source=console
INFO[0008] vu:  1 iteration:  6 person.name:  Russ Carter  source=console
INFO[0009] vu:  2 iteration:  7 person.name:  Brenna Gislason  source=console
INFO[0009] vu:  1 iteration:  7 person.name:  Brenna Gislason  source=console
INFO[0010] vu:  2 iteration:  8 person.name:  Eulah Stark  source=console
INFO[0010] vu:  1 iteration:  8 person.name:  Eulah Stark  source=console
INFO[0011] vu:  1 iteration:  9 person.name:  Maxie Bartoletti  source=console
INFO[0011] vu:  2 iteration:  9 person.name:  Maxie Bartoletti  source=console
INFO[0013] vu:  2 iteration:  10 person.name:  Foster Stroman  source=console
INFO[0013] vu:  1 iteration:  10 person.name:  Foster Stroman  source=console
INFO[0014] vu:  2 iteration:  11 person.name:  Jerel Howe  source=console
INFO[0014] vu:  1 iteration:  11 person.name:  Jerel Howe  source=console
INFO[0015] vu:  2 iteration:  12 person.name:  Noah Christiansen  source=console
INFO[0015] vu:  1 iteration:  12 person.name:  Noah Christiansen  source=console
INFO[0016] vu:  2 iteration:  13 person.name:  Drake Ankunding  source=console
INFO[0016] vu:  1 iteration:  13 person.name:  Drake Ankunding  source=console
INFO[0018] vu:  2 iteration:  14 person.name:  Furman Boyer  source=console
INFO[0018] vu:  1 iteration:  14 person.name:  Furman Boyer  source=console
INFO[0019] vu:  2 iteration:  15 person.name:  Darien Murphy  source=console
INFO[0019] vu:  1 iteration:  15 person.name:  Darien Murphy  source=console
INFO[0020] vu:  2 iteration:  16 person.name:  Kristina Hyatt  source=console
INFO[0020] vu:  1 iteration:  16 person.name:  Kristina Hyatt  source=console
INFO[0021] vu:  2 iteration:  17 person.name:  Dimitri Krajcik  source=console
INFO[0021] vu:  1 iteration:  17 person.name:  Dimitri Krajcik  source=console
INFO[0022] vu:  2 iteration:  18 person.name:  Else Leffler  source=console
INFO[0022] vu:  1 iteration:  18 person.name:  Else Leffler  source=console
INFO[0024] vu:  2 iteration:  19 person.name:  Isaac Leuschke  source=console
INFO[0024] vu:  1 iteration:  19 person.name:  Isaac Leuschke  source=console
INFO[0025] vu:  2 iteration:  20 person.name:  Ashlee Bradtke  source=console
INFO[0025] vu:  1 iteration:  20 person.name:  Ashlee Bradtke  source=console
INFO[0026] vu:  2 iteration:  21 person.name:  Keenan Weber  source=console
INFO[0026] vu:  1 iteration:  21 person.name:  Keenan Weber  source=console
INFO[0027] vu:  1 iteration:  22 person.name:  Norris Mraz  source=console
INFO[0027] vu:  2 iteration:  22 person.name:  Norris Mraz  source=console
INFO[0029] vu:  2 iteration:  23 person.name:  Torrey Moore  source=console
INFO[0029] vu:  1 iteration:  23 person.name:  Torrey Moore  source=console
INFO[0030] vu:  2 iteration:  24 person.name:  Lucas Schuppe  source=console
INFO[0030] vu:  1 iteration:  24 person.name:  Lucas Schuppe  source=console
INFO[0031] vu:  2 iteration:  25 person.name:  Verna Heathcote  source=console
INFO[0031] vu:  1 iteration:  25 person.name:  Verna Heathcote  source=console
INFO[0032] vu:  2 iteration:  26 person.name:  Damaris Jacobi  source=console
INFO[0032] vu:  1 iteration:  26 person.name:  Damaris Jacobi  source=console
INFO[0033] vu:  2 iteration:  27 person.name:  Keyon Wehner  source=console
INFO[0033] vu:  1 iteration:  27 person.name:  Keyon Wehner  source=console
INFO[0035] vu:  2 iteration:  28 person.name:  Thalia Cormier  source=console
INFO[0035] vu:  1 iteration:  28 person.name:  Thalia Cormier  source=console
INFO[0036] vu:  2 iteration:  29 person.name:  Chadd Nader  source=console
INFO[0036] vu:  1 iteration:  29 person.name:  Chadd Nader  source=console

running (0m37.0s), 0/2 VUs, 60 complete and 0 interrupted iterations
form ✓ [======================================] 2 VUs  0m37.0s/1m40s  60/60 iters, 30 per VU

     data_received..................: 105 kB 2.8 kB/s
     data_sent......................: 26 kB  704 B/s
   ✓ failed form fetches............: 0.00%  ✓ 0        ✗ 60 
   ✓ failed form submits............: 0.00%  ✓ 0        ✗ 60 
     http_req_blocked...............: avg=3.72ms   min=2µs      med=7µs      max=224.96ms p(90)=16.1µs   p(95)=18µs    
     http_req_connecting............: avg=1.77ms   min=0s       med=0s       max=107.73ms p(90)=0s       p(95)=0s      
   ✓ http_req_duration..............: avg=112.05ms min=106.66ms med=109.68ms max=141.92ms p(90)=117.96ms p(95)=129.6ms 
       { expected_response:true }...: avg=112.05ms min=106.66ms med=109.68ms max=141.92ms p(90)=117.96ms p(95)=129.6ms 
     http_req_failed................: 0.00%  ✓ 0        ✗ 120
     http_req_receiving.............: avg=89.82µs  min=19µs     med=76.5µs   max=301µs    p(90)=152.19µs p(95)=171.74µs
     http_req_sending...............: avg=34.21µs  min=7µs      med=32µs     max=103µs    p(90)=55µs     p(95)=60µs    
     http_req_tls_handshaking.......: avg=1.91ms   min=0s       med=0s       max=115.83ms p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=111.93ms min=106.54ms med=109.55ms max=141.8ms  p(90)=117.77ms p(95)=129.52ms
     http_reqs......................: 120    3.239677/s
     iteration_duration.............: avg=1.23s    min=1.21s    med=1.22s    max=1.47s    p(90)=1.24s    p(95)=1.25s   
     iterations.....................: 60     1.619838/s
     vus............................: 2      min=2      max=2
     vus_max........................: 2      min=2      max=2

I do not use your local faker, is it based on https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js as well? I also don’t have the fakeData.js you are importing. If you can share more details on those we can have a look.

Let me know if my example works on your side as well.

I hope this helps.

Cheers!

Ok, thx for your help. But VU 1 and VU 2 have the same person name. How to achieve the result that each user on each interaction has the different name?

Hi @Mada

If you need to change the values each VU is taking you might try initializing faker with a different seed. Similar to:

import { sleep } from 'k6';
import http from 'k6/http';
import { Rate } from 'k6/metrics';
import faker from 'https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js'
import { vu } from 'k6/execution';
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

export const options = {
    discardResponseBodies: true,
    scenarios: {
        form: {
            executor: 'per-vu-iterations',
            vus: 2,
            iterations: 30,
            maxDuration: '100s',
        },
    },
    thresholds: {
        'failed form submits': ['rate<0.1'],
        'failed form fetches': ['rate<0.1'],
        'http_req_duration': ['p(95)<400']
    }
};

export const generateSubscriber = () => ({
    name: `${faker.name.firstName()} ${faker.name.lastName()}`,
    title: faker.name.jobTitle(),
    company: faker.company.companyName(),
    email: faker.internet.email(),
    country: faker.address.country()
});

const baseUrl = 'https://httpbin.test.k6.io/anything';
const urls = {
    form: `${baseUrl}/form`,
    submit: `${baseUrl}/form/subscribe`,
};

const formFailRate = new Rate('failed form fetches');
const submitFailRate = new Rate('failed form submits');

const getForm = () => {
    const formResult = http.get(urls.form);
    formFailRate.add(formResult.status !== 200);
}

const submitForm = () => {
    const person = generateSubscriber();
    console.log('vu: ', vu.idInTest, 'iteration: ', vu.iterationInScenario, 'person.name: ', person.name);
    const payload = JSON.stringify(person);
    const submitResult = http.post(urls.submit, payload);
    submitFailRate.add(submitResult.status !== 200);
}

export default function () {
    faker.seed(randomIntBetween(1, 1000));
    getForm();
    submitForm();
    sleep(1);
}

Then I get:

 scenarios: (100.00%) 1 scenario, 2 max VUs, 2m10s max duration (incl. graceful stop):
           * form: 10 iterations for each of 2 VUs (maxDuration: 1m40s, gracefulStop: 30s)

INFO[0001] vu:  2 iteration:  0 person.name:  Kyler Boyer  source=console
INFO[0001] vu:  1 iteration:  0 person.name:  Monserrat Bogisich  source=console
INFO[0002] vu:  2 iteration:  1 person.name:  Garrett Mayer  source=console
INFO[0002] vu:  1 iteration:  1 person.name:  Freda Farrell  source=console
INFO[0003] vu:  1 iteration:  2 person.name:  Diana Funk  source=console
INFO[0003] vu:  2 iteration:  2 person.name:  Leopoldo Cartwright  source=console
INFO[0005] vu:  1 iteration:  3 person.name:  Elise Ryan  source=console
INFO[0005] vu:  2 iteration:  3 person.name:  Carissa Daniel  source=console
INFO[0006] vu:  2 iteration:  4 person.name:  Damien Connelly  source=console
INFO[0006] vu:  1 iteration:  4 person.name:  Jamaal Hayes  source=console
...

I hope this helps.

Cheers!

thank you very much :slight_smile:

1 Like