Skip to content

Instantly share code, notes, and snippets.

@azhawkes
Created December 17, 2018 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azhawkes/750762c7b79c3fceaec8e437ba3b841b to your computer and use it in GitHub Desktop.
Save azhawkes/750762c7b79c3fceaec8e437ba3b841b to your computer and use it in GitHub Desktop.
// This is a contrived example of some of the things you can do in a Loadster code step.
// HTTP and Wait methods are synchronous by design, no callbacks required.
// More documentation is coming soon!
console.log("We're in a code step.");
// Wait between 0 and 2 seconds
user.wait(Math.random() * 2000);
// Make a GET request with page resources
user.get("https://loadster.app", {
resources: [
"/style.css",
"/logo.png"
]
});
// Set a user variable to a random integer from 0 to 19
user.setVariable("random", Math.floor(Math.random() * 20));
// Loop and make 5 requests with a counter param and value from a user variable
for (var i = 0; i < 5; i++) {
user.get("https://loadster.app?i=" + i + "&random=" + user.getVariable("random"));
}
// Make a POST with a JSON body
user.post("https://loadster.app/bogus", {
random: user.getVariable("random")
}, {
headers: [
{ name: "Content-Type", value: "application/json" }
]
});
// A note on variables:
// User variables are scoped to the v-user iteration, and carry over to subsequent steps.
// Set and retrieve them with setVariable/getVariable. Normal JavaScript variables
// (the var keyword) are scoped to this block only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment