-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathJsonSample.groovy
52 lines (45 loc) · 1.26 KB
/
JsonSample.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import org.apache.jmeter.services.FileServer
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.ConcurrentHashMap
class JsonSampleHolder {
static samples =
MapWithDefault.newInstance(
new ConcurrentHashMap<String, JsonSample>(), { String f -> new JsonSample(f) }
)
static def next(file) {
return samples.get(file).next()
}
}
class JsonSample {
def data = null
def ite = null
JsonSample(file) {
if(data == null) {
def inputPath = FileServer.getFileServer().getBaseDir() + file
File inputFile = new File(inputPath)
def slurper = new JsonSlurper()
data = new ConcurrentLinkedQueue(slurper.parse(inputFile))
ite = data.iterator()
}
}
synchronized def next() {
if(!ite.hasNext()) {
ite = data.iterator()
}
return ite.next()
}
}
Thread thread = Thread.currentThread();
Long threadNum = thread.getId();
def value = JsonSampleHolder.next(args[0])
def json = JsonOutput.toJson(value)
log.info(threadNum + " : " + value)
vars.putObject(args[1], value)
vars.put(args[1] + "_json", json)
if(binding.hasVariable('SampleResult')) {
SampleResult.setContentType("application/json")
SampleResult.setResponseCodeOK()
SampleResult.setResponseData(json, "utf-8")
}