Skip to content

Instantly share code, notes, and snippets.

@robingustafsson
Created January 25, 2019 14:46
Show Gist options
  • Save robingustafsson/f847c27bee10d8c8c487bee00f0a8c14 to your computer and use it in GitHub Desktop.
Save robingustafsson/f847c27bee10d8c8c487bee00f0a8c14 to your computer and use it in GitHub Desktop.
JMeter-to-k6 PR review: JSON path extractor
<?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/>
<com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.JSONPathExtractor guiclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.gui.JSONPathExtractorGui" testclass="com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.JSONPathExtractor" testname="jp@gc - JSON Path Extractor" enabled="false">
<stringProp name="VAR">slideshowAuthor</stringProp>
<stringProp name="JSONPATH">slideshow.author</stringProp>
<stringProp name="DEFAULT"></stringProp>
<stringProp name="VARIABLE"></stringProp>
<stringProp name="SUBJECT">BODY</stringProp>
</com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.JSONPathExtractor>
<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/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
import http from "k6/http"
import jsonpath from "../build/jsonpath.js"
const vus = 1
let url, opts, auth, r, regex, match, matches, extract, output
let csvPage = {}, csvColumns = {}
const constants = {}
const vars = {}
const files = {}
export let options = {
stages: [{"target":1,"duration":"1s"}]
}
export default function (data) {
if (__VU >= 1 && __VU <= 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 })
console.log(JSON.stringify(vars));
}
} else throw new Error('Unexpected VU: ' + __VU)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment