Quantcast
Channel: Grafana k6 - Grafana Labs Community Forums
Viewing all articles
Browse latest Browse all 345

Capture Id from post request from one scenario and pass in GET request in another scenario

$
0
0

I’m trying to capture promiseID from post request in scenario1 and pass it in Get request in scenario2 since TPS of both Post and Get are different.

import { authenticate } from “…/services/auth-request”;
import {
postOutboundPromise,
getPromiseDetails,
} from “…/services/outboundPromise-request”;
import { logMetrics, printMetrics } from “…/services/custom-metric”;
import { sleep } from “k6”;

var accessToken: string | null = null;

export const options = {
scenarios: {
Post: {
executor: “constant-arrival-rate”,
rate: 2,
duration: “1m”,
preAllocatedVUs: 1,
maxVUs: 1,
exec: “testFlow1”,
},
Get: {
executor: “constant-arrival-rate”,
rate: 1,
duration: “1m”,
startTime: “10s”,
preAllocatedVUs: 1,
maxVUs: 1,
exec: “testFlow2”,
},
},
discardResponseBodies: false,
thresholds: {
http_req_duration: [“p(95)<500”],
http_req_failed: [“rate<0.1”],
},
};

let globalPromiseId: string;

export function setup() {
accessToken = authenticate();
if (!accessToken) {
throw new Error(“Failed to authenticate and get access token”);
}

return { accessToken };
}

export function testFlow1(data: { accessToken: any }) {
accessToken = data.accessToken;

if (!accessToken) {
throw new Error(“Authentication failed, no access token received”);
}

const { promiseId, response } = postOutboundPromise(accessToken);
if (!promiseId) {
throw new Error(“POST request failed, no promiseId received”);
}

// Update global variables
globalPromiseId = promiseId;

// Log metrics for the POST request

logMetrics(“POST /promises”, response);

return { promiseId };
}

export function testFlow2(data: { accessToken: any }) {
accessToken = data.accessToken;

if (!accessToken) {
throw new Error(“Authentication failed, no access token received”);
}

console.log(globalPromiseId + “this is ok ?”);

const promiseId = globalPromiseId;

// Wait to simulate the delay and ensure GET request happens after POST
sleep(1); // Adjust sleep duration as needed

const getResponse = getPromiseDetails(
promiseId,
accessToken
// globalPostResponse
);

// Log metrics for the GET request
logMetrics(“GET /promises/:promiseId}”, getResponse);

return { getResponse };
}

// Teardown function to print metrics
export function teardown() {
sleep(1);
printMetrics();
}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 345

Trending Articles