Java Code Examples for org.apache.jmeter.threads.JMeterContextService#getContext()

The following examples show how to use org.apache.jmeter.threads.JMeterContextService#getContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParallelSampler.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
private HashTree getTestTree(TestElement te) {
    // can't use GenericController because of infinite looping
    CustomLoopController wrapper = new CustomLoopController(JMeterContextService.getContext());
    wrapper.setLoops(1);
    wrapper.setContinueForever(false);

    wrapper.addTestElement(te);
    wrapper.setName("wrapped " + te.getName());
    wrapper.setRunningVersion(isRunningVersion());

    HashTree tree = new HashTree();
    HashTree subTree = getSubTree(te);
    if (subTree != null) {
        tree.add(wrapper, subTree);
    } else {
        tree.add(wrapper);
    }
    return tree;
}
 
Example 2
Source File: PepperBoxLoadGenTest.java    From pepper-box with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws IOException {

    zkServer = new EmbeddedZookeeper();

    String zkConnect = ZKHOST + ":" + zkServer.port();
    zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
    ZkUtils zkUtils = ZkUtils.apply(zkClient, false);

    Properties brokerProps = new Properties();
    brokerProps.setProperty("zookeeper.connect", zkConnect);
    brokerProps.setProperty("broker.id", "0");
    brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
    brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST +":" + BROKERPORT);
    KafkaConfig config = new KafkaConfig(brokerProps);
    Time mock = new MockTime();
    kafkaServer = TestUtils.createServer(config, mock);
    //AdminUtils.createTopic(zkUtils, TOPIC, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);

    JMeterContext jmcx = JMeterContextService.getContext();
    jmcx.setVariables(new JMeterVariables());

}
 
Example 3
Source File: Debugger.java    From jmeter-debugger with Apache License 2.0 6 votes vote down vote up
public void start() {
    log.debug("Start debugging");
    frontend.started();

    HashTree hashTree = getSelectedTree();
    StandardJMeterEngine.register(new StateListener()); // oh, dear, they use static field then clean it...
    engine = new DebuggerEngine(JMeterContextService.getContext());
    engine.setStepper(this);
    JMeter.convertSubTree(hashTree);
    engine.configure(hashTree);
    try {
        engine.runTest();
    } catch (JMeterEngineException e) {
        log.error("Failed to pauseContinue debug", e);
        stop();
    }
}
 
Example 4
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcess_from_var_2() {
    System.out.println("process fromvar");
    JMeterContext context = JMeterContextService.getContext();
    JMeterVariables vars = context.getVariables();

    SampleResult res = new SampleResult();
    res.setResponseData("".getBytes());
    context.setPreviousResult(res);

    vars.put("SVAR", json);

    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setDefaultValue("DEFAULT");
    instance.setVar("test");
    instance.setJsonPath("$.store.bicycle");
    instance.setSubject(JSONPathExtractor.SUBJECT_VARIABLE);
    instance.setSrcVariableName("SVAR");
    instance.process();
    String test = vars.get("test");
    boolean thiis = "{\"color\":\"red\",\"price\":19.95}".equals(test);
    boolean thaat = "{\"price\":19.95,\"color\":\"red\"}".equals(test);
    assertTrue(thiis || thaat);
}
 
Example 5
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testYamlExtractor2() throws IOException {
    File test = new File(getClass().getResource("/test.yml").getPath());
    String response = FileUtils.readFileToString(test);

    JMeterContext context = JMeterContextService.getContext();
    JMeterVariables vars = context.getVariables();

    SampleResult samplerResult = new SampleResult();
    samplerResult.setResponseData(response.getBytes());
    context.setPreviousResult(samplerResult);

    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setInputFormat(JSONPathExtractor.INPUT_YAML);
    instance.setDefaultValue("DEFAULT");
    instance.setVar("architect");
    instance.setJsonPath("$.architect");
    instance.process();

    assertEquals("mihai", vars.get("architect"));
}
 
Example 6
Source File: MonitoringResultsCollector.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
   * Update the worker thread jmeter context with the main thread one
   * @param isInit if true the context a full copy is done, if false only update is done
   */
  private void syncContext(boolean isInit)
  {
  	// jmeter context synchronisation
  	JMeterContext current = JMeterContextService.getContext();
JMeterContext ctx = this.getThreadContext();

if (isInit)
{
	current.setCurrentSampler(ctx.getCurrentSampler());
	current.setEngine(ctx.getEngine());
	current.setRestartNextLoop(ctx.isRestartNextLoop());
	current.setSamplingStarted(ctx.isSamplingStarted());
	current.setThread(ctx.getThread());
	current.setThreadGroup(ctx.getThreadGroup());
	current.setThreadNum(ctx.getThreadNum());
}
current.setVariables(ctx.getVariables());
current.setPreviousResult(ctx.getPreviousResult());
//current.getSamplerContext().putAll(ctx.getSamplerContext());
  }
 
Example 7
Source File: JSONPathExtractorTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcess() {
    System.out.println("process");
    JMeterContext context = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData(json.getBytes());
    context.setPreviousResult(res);

    JSONPathExtractor instance = new JSONPathExtractor();
    instance.setDefaultValue("DEFAULT");
    instance.setVar("test");
    instance.setJsonPath("$.store.book[*].author");
    instance.process();
    JMeterVariables vars = context.getVariables();
    assertEquals("[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]", vars.get("test"));
}
 
Example 8
Source File: JSONFormatterTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess_null() {
    System.out.println("process null");
    JSONFormatter instance = new JSONFormatter();
    JMeterContext threadContext = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData("null", "UTF8");
    threadContext.setPreviousResult(res);
    instance.process();
}
 
Example 9
Source File: AbstractThreadStarter.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
public AbstractThreadStarter(int groupIndex, AbstractDynamicThreadGroup owner, ListedHashTree listedHashTree, ListenerNotifier listenerNotifier, StandardJMeterEngine standardJMeterEngine) {
    super();
    this.owner = owner;
    this.treeClone = cloneTree(listedHashTree); // it needs owner inside
    this.engine = standardJMeterEngine;
    this.groupIndex = groupIndex;
    this.threadGroupTree = listedHashTree;
    this.notifier = listenerNotifier;
    this.context = JMeterContextService.getContext();
    setDaemon(true);
}
 
Example 10
Source File: JSONFormatterTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess_float() {
    System.out.println("process float");
    JSONFormatter instance = new JSONFormatter();
    JMeterContext threadContext = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData("3.14", "UTF8");
    threadContext.setPreviousResult(res);
    instance.process();
}
 
Example 11
Source File: CaseFormatTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	changeCase = new CaseFormat();
    result = new SampleResult();
    JMeterContext jmctx = JMeterContextService.getContext();
    String data = "dummy data";
    result.setResponseData(data, null);
    JMeterVariables vars = new JMeterVariables();
    jmctx.setVariables(vars);
    jmctx.setPreviousResult(result);
    params = new LinkedList<>();
}
 
Example 12
Source File: JSONFormatterTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess_dict() {
    System.out.println("process dict");
    JSONFormatter instance = new JSONFormatter();
    JMeterContext threadContext = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData("{\"a\":1,\"b\":2}", "UTF8");
    threadContext.setPreviousResult(res);
    instance.process();
    Assert.assertEquals("{\n    \"a\": 1,\n    \"b\": 2\n}", res.getResponseDataAsString());
}
 
Example 13
Source File: JSONFormatterTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess_Failure() {
    System.out.println("process");
    JSONFormatter instance = new JSONFormatter();
    JMeterContext threadContext = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.setResponseData("<html>", "UTF8");
    threadContext.setPreviousResult(res);
    instance.process();
}
 
Example 14
Source File: MonitoringResultsCollector.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void sampleOccurred(SampleEvent event) {
    // just dropping regular test samples
	
	// update JMeterContext for share with samplers thread in order to provide
	// updated variables
	this.ctx = JMeterContextService.getContext();
}
 
Example 15
Source File: DebuggerEngineTest.java    From jmeter-debugger with Apache License 2.0 5 votes vote down vote up
@Test
public void runVariablesInControllers() throws Exception {
    TestProvider prov = new TestProvider("/com/blazemeter/jmeter/debugger/loops.jmx", "loops.jmx");

    Debugger sel = new Debugger(prov, new FrontendMock());
    AbstractThreadGroup tg = prov.getTG(0);
    sel.selectThreadGroup(tg);
    HashTree testTree = sel.getSelectedTree();

    TestSampleListener listener = new TestSampleListener();
    testTree.add(testTree.getArray()[0], listener);

    DebuggingThreadGroup tg2 = (DebuggingThreadGroup) getFirstTG(testTree);
    LoopController samplerController = (LoopController) tg2.getSamplerController();
    samplerController.setLoops(1);
    samplerController.setContinueForever(false);

    JMeter.convertSubTree(testTree);

    DebuggerEngine engine = new DebuggerEngine(JMeterContextService.getContext());
    StepTriggerCounter hook = new StepTriggerCounter();
    engine.setStepper(hook);
    engine.configure(testTree);
    engine.runTest();
    while (engine.isActive()) {
        Thread.sleep(1000);
    }
    assertEquals(12, hook.cnt);

    assertEquals(3, listener.events.size());
}
 
Example 16
Source File: DebuggerEngineTest.java    From jmeter-debugger with Apache License 2.0 5 votes vote down vote up
@Test
public void runVariablesInAssertions() throws Exception {
    TestProvider prov = new TestProvider("/com/blazemeter/jmeter/debugger/debug.jmx", "debug.jmx");

    Debugger sel = new Debugger(prov, new FrontendMock());
    AbstractThreadGroup tg = prov.getTG(0);
    sel.selectThreadGroup(tg);
    HashTree testTree = sel.getSelectedTree();

    TestSampleListener listener = new TestSampleListener();
    testTree.add(testTree.getArray()[0], listener);

    DebuggingThreadGroup tg2 = (DebuggingThreadGroup) getFirstTG(testTree);
    LoopController samplerController = (LoopController) tg2.getSamplerController();
    samplerController.setLoops(1);
    samplerController.setContinueForever(false);

    JMeter.convertSubTree(testTree);

    DebuggerEngine engine = new DebuggerEngine(JMeterContextService.getContext());
    StepTriggerCounter hook = new StepTriggerCounter();
    engine.setStepper(hook);
    engine.configure(testTree);
    engine.runTest();
    while (engine.isActive()) {
        Thread.sleep(1000);
    }
    assertEquals(4, hook.cnt);

    assertEquals(1, listener.events.size());
    SampleEvent event = listener.events.get(0);
    SampleResult result = event.getResult();
    AssertionResult[] assertionResults = result.getAssertionResults();
    assertEquals(1, assertionResults.length);

    AssertionResult assertionRes = assertionResults[0];
    assertNull(assertionRes.getFailureMessage());
}
 
Example 17
Source File: DummySubPostProcessorTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    JMeterContext threadContext = JMeterContextService.getContext();
    SampleResult res = new SampleResult();
    res.sampleStart();
    res.sampleEnd();
    threadContext.setPreviousResult(res);
}
 
Example 18
Source File: DebuggingThreadGroup.java    From jmeter-debugger with Apache License 2.0 5 votes vote down vote up
@Override
public void start(int groupCount, ListenerNotifier notifier, ListedHashTree threadGroupTree, StandardJMeterEngine engine) {
    JMeterContext context = JMeterContextService.getContext();
    DebuggingThread jmThread = makeThread(groupCount, notifier, threadGroupTree, engine, 0, context);
    Thread newThread = new Thread(jmThread, jmThread.getThreadName());
    if (engine instanceof DebuggerEngine) {
        DebuggerEngine dbgEngine = (DebuggerEngine) engine;
        dbgEngine.setTarget(jmThread);
        dbgEngine.setThread(newThread);

        this.jmeterThread = jmThread;
        this.osThread = newThread;
    }
    newThread.start();
}
 
Example 19
Source File: JMXMonCollector.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void sampleOccurred(SampleEvent event) {
    // just dropping regular test samples
	
	// update JMeterContext for share with samplers thread in order to provide
	// updated variables
	this.ctx = JMeterContextService.getContext();
}
 
Example 20
Source File: PepperBoxConfigElementTest.java    From pepper-box with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setUp(){
    JMeterContext jmcx = JMeterContextService.getContext();
    jmcx.setVariables(new JMeterVariables());
}