Skip to content

Instantly share code, notes, and snippets.

@teebow1e
Created October 21, 2024 03:24
Show Gist options
  • Save teebow1e/f62e71318d07fdae0d5c7cf2319a1e38 to your computer and use it in GitHub Desktop.
Save teebow1e/f62e71318d07fdae0d5c7cf2319a1e38 to your computer and use it in GitHub Desktop.
[K6] Test HTTP
import http from 'k6/http';
import { check, sleep } from 'k6';
import { randomString } from 'https://jslib.k6.io/k6-utils/1.1.0/index.js';
// Options for the load test scenario
export let options = {
stages: [
{ duration: '30s', target: 50 },
{ duration: '3m', target: 100 },
{ duration: '30s', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<2000'], // 95% of requests should be below 2s
},
};
// Base URL for the proxy server
const BASE_URL = 'http://192.168.194.139:1337';
// Function to simulate normal and malicious request patterns
function makeRequest() {
// Generate a random URL path
let path = randomString(10);
// Randomize request method and payload size to simulate attack vectors
let method = Math.random() < 0.5 ? 'GET' : 'POST';
let payload = method === 'POST' ? randomString(1000 + Math.floor(Math.random() * 9000)) : '';
let params = {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + randomString(20) // Random fake token
},
};
// Send the HTTP request to the proxy server
let res = http.request(method, `${BASE_URL}/${path}`, payload, params);
// Check the response status and content
check(res, {
'response body is not empty': (r) => r.body.length > 0
});
}
// The default function that will be run for each virtual user
export default function () {
// Each virtual user will make 10 requests with a random delay
for (let i = 0; i < 10; i++) {
makeRequest();
sleep(Math.random()); // Random sleep to simulate varied request patterns
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment