Skip to main content
Version: 4.5.2.5-onprem

k6 script — Section Control Segment

This page explains how the Section Control k6 script works and what each part does, including Alive Notice cycles.

Full script (copy & paste)

Save it as a filename of your choice (e.g., section-control-script.js).

import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';
import moment from 'https://momentjs.com/downloads/moment.min.js';
import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js';
import { SharedArray } from 'k6/data';
import http from "k6/http";
import { group, sleep, check } from "k6";
import { Trend } from 'k6/metrics';
import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.1.0/index.js';

const nginxTrend = new Trend('z0_NGINX');
const nf5101Trend = new Trend('z1_NF_5101');
const nf5002Trend = new Trend('z2_NF_5002');
const nf5003Trend = new Trend('z3_NF_5003');
const nf5004Trend = new Trend('z4_NF_5004');
const eumTrend = new Trend('z5_EUM');

export const options = {
vus: 30, // number of virtual users
duration: "600s", // test duration
// iterations: 3000, // optional cap on total iterations; if omitted, loops for duration
};

export function setup() {
const vars = {};

vars["apiurl"] = "https://nf4-onprem-test.stclab.com"; // NetFUNNEL base URL

vars['sid'] = "service_1"; // Project ID
vars['aid'] = "section_control"; // Segment ID

vars['btn_click_delay'] = "2.5"; // simulated service duration (seconds)
vars['section_count'] = "10"; // number of Alive Notice cycles

return vars;
}

export default function (vars) {
group("NGINX", function () {
const resources = [
{ url: `${vars.apiurl}/assets/nf-setting/1/nf-setting.json`, type: "json" }
];

let response;
// request integration asset
resources.forEach((resource) => {
const response = http.get(resource.url.toString(), {
tags: { name: "NGINX" }
})

check(response, {
"NGINX status 200": r => r.status === 200
});

nginxTrend.add(response.timings.blocked + response.timings.connecting + response.timings.tls_handshaking + response.timings.duration);
});
});

group("NF", function () {
let response;

const nf4URL = new URL(vars.apiurl + "/ts.wseq")

nf4URL.searchParams.append('opcode', '5101')
nf4URL.searchParams.append('sid', vars.sid)
nf4URL.searchParams.append('aid', vars.aid)

// request entry key (issue token)
response = http.get(nf4URL.toString(), {
tags: { name: "NF_5101" }
})

check(response, {
"NF_5101 status 200": r => r.status === 200
});
nf5101Trend.add(response.timings.blocked + response.timings.connecting + response.timings.tls_handshaking + response.timings.duration);

let [retval, other] = response.body.split(":")
let data = {}
other.split('&').forEach(item => {
const [k, v] = item.split("=")
data[k] = v
});

let ttl;

// when entry is not allowed, perform waiting communication
while (parseInt(retval) === 201) {
ttl = parseInt(data.ttl);
sleep(ttl);

const nf4RetryActionURL = new URL(`${vars.apiurl}:${data.port || 443}/ts.wseq`)
nf4RetryActionURL.searchParams.append('opcode', '5002')
nf4RetryActionURL.searchParams.append('key', data.key)
nf4RetryActionURL.searchParams.append('sticky', data.sticky)
response = http.get(nf4RetryActionURL.toString(), {
tags: { name: "NF_5002" }
})
check(response, {
"NF_5002 status 200": r => r.status === 200
});
nf5002Trend.add(response.timings.blocked + response.timings.connecting + response.timings.tls_handshaking + response.timings.duration);

[retval, other] = response.body.split(":")
data = {}
other.split('&').forEach(item => {
const [k, v] = item.split("=")
data[k] = v
});
}

// send Alive Notice cycles
if (parseInt(retval) === 200) {
retval = 201;
let count = 0;

while (parseInt(retval) === 201 && count < parseInt(vars.section_count)) {
ttl = parseInt(data.ttl);
sleep(ttl);

const nf4AliveActionURL = new URL(`${vars.apiurl}:${data.port || 443}/ts.wseq`);
nf4AliveActionURL.searchParams.append('opcode', '5003');
nf4AliveActionURL.searchParams.append('key', data.key);
nf4AliveActionURL.searchParams.append('sticky', data.sticky);

response = http.get(nf4AliveActionURL.toString(), {
tags: { name: "NF_5003" }
});

check(response, {
"NF_5003 status 200": r => r.status === 200
});

nf5003Trend.add(response.timings.blocked + response.timings.connecting + response.timings.tls_handshaking + response.timings.duration
);

[retval, other] = response.body.split(":");
data = {};
other.split('&').forEach(item => {
const [k, v] = item.split("=");
data[k] = v;
});

count++;
}
}

// return key (complete)
if (parseInt(retval) === 201) {
// place your business logic here
sleep(vars.btn_click_delay)

// after work, return key to NetFUNNEL
const nf4CompleteURL = new URL(`${vars.apiurl}:${data.port || 443}/ts.wseq`)
nf4CompleteURL.searchParams.append('opcode', '5004')
nf4CompleteURL.searchParams.append('key', data.key)
nf4CompleteURL.searchParams.append('sticky', data.sticky)
response = http.get(nf4CompleteURL.toString(), {
tags: { name: "NF_5004" }
})
check(response, {
"NF_5004 status 200": r => r.status === 200
});
nf5004Trend.add(response.timings.blocked + response.timings.connecting + response.timings.tls_handshaking + response.timings.duration);
}
});
}

How to run

# Example filename — adjust to your saved name
k6 run section-control-script.js

Options and setup

Virtual users and duration:

export const options = {
vus: 30, // number of virtual users
duration: "600s", // test duration
// iterations: 3000, // optional cap on total iterations; if omitted, loops for duration
};

Environment variables (setup()):

export function setup() {
const vars = {};

vars["apiurl"] = "https://nf4-onprem-test.stclab.com"; // NetFUNNEL base URL

vars['sid'] = "service_1"; // Project ID
vars['aid'] = "section_control"; // Segment ID

vars['btn_click_delay'] = "2.5"; // simulated service duration (seconds)
vars['section_count'] = "10"; // number of Alive Notice cycles

return vars;
}

Key fields:

  • section_count: number of Alive Notice cycles to simulate a longer stay in the section
Difference vs. Basic Control

Section Control sends Alive Notice (5003) cycles between entry (200) and completion (5004), simulating a longer in-section presence.

Code breakdown

1) Fetch configuration asset

group("NGINX", function () {
const resources = [
{ url: `${vars.apiurl}/assets/nf-setting/1/nf-setting.json`, type: "json" }
];
// ... request and timing trend
});

2) Request entry key (5101), handle queue with retries (5002)

const nf4URL = new URL(vars.apiurl + "/ts.wseq")
nf4URL.searchParams.append('opcode', '5101')
nf4URL.searchParams.append('sid', vars.sid)
nf4URL.searchParams.append('aid', vars.aid)
response = http.get(nf4URL.toString(), { tags: { name: "NF_5101" }})
// parse and if 201, loop with opcode=5002 until allowed

3) After 200, run Alive Notice cycles (5003) section_count times

if (parseInt(retval) === 200) {
retval = 201;
let count = 0;
while (parseInt(retval) === 201 && count < parseInt(vars.section_count)) {
ttl = parseInt(data.ttl);
sleep(ttl);
const nf4AliveActionURL = new URL(`${vars.apiurl}:${data.port || 443}/ts.wseq`);
nf4AliveActionURL.searchParams.append('opcode', '5003');
nf4AliveActionURL.searchParams.append('key', data.key);
nf4AliveActionURL.searchParams.append('sticky', data.sticky);
response = http.get(nf4AliveActionURL.toString(), { tags: { name: "NF_5003" } });
check(response, { "NF_5003 status 200": r => r.status === 200 });
nf5003Trend.add(response.timings.blocked + response.timings.connecting + response.timings.tls_handshaking + response.timings.duration);
// parse response and continue
count++;
}
}

4) Simulate work then return key (5004)

if (parseInt(retval) === 201) {
sleep(vars.btn_click_delay)
const nf4CompleteURL = new URL(`${vars.apiurl}:${data.port || 443}/ts.wseq`)
nf4CompleteURL.searchParams.append('opcode', '5004')
nf4CompleteURL.searchParams.append('key', data.key)
nf4CompleteURL.searchParams.append('sticky', data.sticky)
response = http.get(nf4CompleteURL.toString(), { tags: { name: "NF_5004" }})
check(response, { "NF_5004 status 200": r => r.status === 200 });
nf5004Trend.add(response.timings.blocked + response.timings.connecting + response.timings.tls_handshaking + response.timings.duration);
}