Created
January 30, 2019 15:46
-
-
Save robingustafsson/56cd5aef0e69ad25afbaf0bde1b572d7 to your computer and use it in GitHub Desktop.
JMeter-to-k6 PR review: If-controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.0 r1840935"> | |
<hashTree> | |
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true"> | |
<stringProp name="TestPlan.comments"></stringProp> | |
<boolProp name="TestPlan.functional_mode">false</boolProp> | |
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp> | |
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp> | |
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> | |
<collectionProp name="Arguments.arguments"/> | |
</elementProp> | |
<stringProp name="TestPlan.user_define_classpath"></stringProp> | |
</TestPlan> | |
<hashTree> | |
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true"> | |
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp> | |
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true"> | |
<boolProp name="LoopController.continue_forever">false</boolProp> | |
<stringProp name="LoopController.loops">1</stringProp> | |
</elementProp> | |
<stringProp name="ThreadGroup.num_threads">1</stringProp> | |
<stringProp name="ThreadGroup.ramp_time">1</stringProp> | |
<boolProp name="ThreadGroup.scheduler">false</boolProp> | |
<stringProp name="ThreadGroup.duration"></stringProp> | |
<stringProp name="ThreadGroup.delay"></stringProp> | |
</ThreadGroup> | |
<hashTree> | |
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true"> | |
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> | |
<collectionProp name="Arguments.arguments"/> | |
</elementProp> | |
<stringProp name="HTTPSampler.domain">httpbin.org</stringProp> | |
<stringProp name="HTTPSampler.port"></stringProp> | |
<stringProp name="HTTPSampler.protocol">https</stringProp> | |
<stringProp name="HTTPSampler.contentEncoding"></stringProp> | |
<stringProp name="HTTPSampler.path">/json</stringProp> | |
<stringProp name="HTTPSampler.method">GET</stringProp> | |
<boolProp name="HTTPSampler.follow_redirects">true</boolProp> | |
<boolProp name="HTTPSampler.auto_redirects">false</boolProp> | |
<boolProp name="HTTPSampler.use_keepalive">true</boolProp> | |
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp> | |
<stringProp name="HTTPSampler.embedded_url_re"></stringProp> | |
<stringProp name="HTTPSampler.connect_timeout"></stringProp> | |
<stringProp name="HTTPSampler.response_timeout"></stringProp> | |
</HTTPSamplerProxy> | |
<hashTree/> | |
<JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor" enabled="true"> | |
<stringProp name="JSONPostProcessor.referenceNames">slideshowAuthor</stringProp> | |
<stringProp name="JSONPostProcessor.jsonPathExprs">$.slideshow.author</stringProp> | |
<stringProp name="JSONPostProcessor.match_numbers"></stringProp> | |
<stringProp name="JSONPostProcessor.defaultValues">test name</stringProp> | |
</JSONPostProcessor> | |
<hashTree/> | |
<IfController guiclass="IfControllerPanel" testclass="IfController" testname="If Controller" enabled="true"> | |
<stringProp name="IfController.condition">${slideshowAuthor}=="Yours Truly"</stringProp> | |
<boolProp name="IfController.evaluateAll">false</boolProp> | |
<boolProp name="IfController.useExpression">true</boolProp> | |
</IfController> | |
<hashTree> | |
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true"> | |
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> | |
<collectionProp name="Arguments.arguments"/> | |
</elementProp> | |
<stringProp name="HTTPSampler.domain">test.loadimpact.com</stringProp> | |
<stringProp name="HTTPSampler.port"></stringProp> | |
<stringProp name="HTTPSampler.protocol"></stringProp> | |
<stringProp name="HTTPSampler.contentEncoding"></stringProp> | |
<stringProp name="HTTPSampler.path">/path1</stringProp> | |
<stringProp name="HTTPSampler.method">GET</stringProp> | |
<boolProp name="HTTPSampler.follow_redirects">true</boolProp> | |
<boolProp name="HTTPSampler.auto_redirects">false</boolProp> | |
<boolProp name="HTTPSampler.use_keepalive">true</boolProp> | |
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp> | |
<stringProp name="HTTPSampler.embedded_url_re"></stringProp> | |
<stringProp name="HTTPSampler.connect_timeout"></stringProp> | |
<stringProp name="HTTPSampler.response_timeout"></stringProp> | |
</HTTPSamplerProxy> | |
<hashTree/> | |
</hashTree> | |
</hashTree> | |
</hashTree> | |
</hashTree> | |
</jmeterTestPlan> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jsonpath from "./build/jsonpath.js" | |
import http from "k6/http" | |
import { check } from "k6" | |
const vars = {} | |
let matches, extract, url, opts, r | |
export let options = { | |
stages: [{ target: 1, duration: "1s" }] | |
} | |
export default function(data) { | |
if (__VU >= 1 && __VU <= 1) { | |
if (__ITER < 1) { | |
url = "https://httpbin.org/json" | |
opts = { | |
redirects: 999 | |
} | |
r = http.request("GET", url, "", opts) | |
{ | |
const queries = ["$.slideshow.author"] | |
const outputs = ["slideshowAuthor"] | |
const defaults = ["test name"] | |
const body = (() => { | |
try { | |
return JSON.parse(r.body) | |
} catch (e) { | |
return null | |
} | |
})() | |
if (body) { | |
for (let i = 0; i < queries.length; i++) { | |
const query = queries[i] | |
const output = outputs[i] | |
const defaultValue = defaults[i] | |
matches = jsonpath.query(body, query) | |
extract = | |
matches.length === 0 | |
? null | |
: matches[Math.floor(Math.random() * matches.length)] | |
vars[output] = extract || defaultValue | |
} | |
} else | |
defaults.forEach((value, i) => { | |
vars[outputs[i]] = value | |
}) | |
} | |
if (`${vars[`slideshowAuthor`]}=="Yours Truly"` === "true") { | |
url = "http://test.loadimpact.com/path1" | |
opts = { | |
redirects: 999 | |
} | |
r = http.request("GET", url, "", opts) | |
{ | |
const queries = ["$.slideshow.author"] | |
const outputs = ["slideshowAuthor"] | |
const defaults = ["test name"] | |
const body = (() => { | |
try { | |
return JSON.parse(r.body) | |
} catch (e) { | |
return null | |
} | |
})() | |
if (body) { | |
for (let i = 0; i < queries.length; i++) { | |
const query = queries[i] | |
const output = outputs[i] | |
const defaultValue = defaults[i] | |
matches = jsonpath.query(body, query) | |
extract = | |
matches.length === 0 | |
? null | |
: matches[Math.floor(Math.random() * matches.length)] | |
vars[output] = extract || defaultValue | |
} | |
} else | |
defaults.forEach((value, i) => { | |
vars[outputs[i]] = value | |
}) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment