@Override publicvoidrun() { log.info("Running the test!"); running = true; /* * Ensure that the sample variables are correctly initialised for each run. */ SampleEvent.initSampleVariables(); JMeterContextService.startTest(); try { PreCompilercompiler=newPreCompiler(); test.traverse(compiler); } catch (RuntimeException e) { log.error("Error occurred compiling the tree:", e); JMeterUtils.reportErrorToUser("Error occurred compiling the tree: - see log file", e); return; // no point continuing } /* * Notification of test listeners needs to happen after function * replacement, but before setting RunningVersion to true. */ SearchByClass<TestStateListener> testListeners = newSearchByClass<>(TestStateListener.class); // TL - S&E test.traverse(testListeners); // Merge in any additional test listeners // currently only used by the function parser testListeners.getSearchResults().addAll(testList); testList.clear(); // no longer needed test.traverse(newTurnElementsOn()); notifyTestListenersOfStart(testListeners); List<?> testLevelElements = newArrayList<>(test.list(test.getArray()[0])); removeThreadGroups(testLevelElements); SearchByClass<SetupThreadGroup> setupSearcher = newSearchByClass<>(SetupThreadGroup.class); SearchByClass<AbstractThreadGroup> searcher = newSearchByClass<>(AbstractThreadGroup.class); SearchByClass<PostThreadGroup> postSearcher = newSearchByClass<>(PostThreadGroup.class); test.traverse(setupSearcher); test.traverse(searcher); test.traverse(postSearcher); TestCompiler.initialize(); // for each thread group, generate threads // hand each thread the sampler controller // and the listeners, and the timer Iterator<SetupThreadGroup> setupIter = setupSearcher.getSearchResults().iterator(); Iterator<AbstractThreadGroup> iter = searcher.getSearchResults().iterator(); Iterator<PostThreadGroup> postIter = postSearcher.getSearchResults().iterator(); ListenerNotifiernotifier=newListenerNotifier(); intgroupCount=0; JMeterContextService.clearTotalThreads(); if (setupIter.hasNext()) { log.info("Starting setUp thread groups"); while (running && setupIter.hasNext()) {//for each setup thread group AbstractThreadGroupgroup= setupIter.next(); groupCount++; StringgroupName= group.getName(); log.info("Starting setUp ThreadGroup: {} : {} ", groupCount, groupName); startThreadGroup(group, groupCount, setupSearcher, testLevelElements, notifier); if (serialized && setupIter.hasNext()) { log.info("Waiting for setup thread group: {} to finish before starting next setup group", groupName); group.waitThreadsStopped(); } } log.info("Waiting for all setup thread groups to exit"); //wait for all Setup Threads To Exit waitThreadsStopped(); log.info("All Setup Threads have ended"); groupCount = 0; JMeterContextService.clearTotalThreads(); } groups.clear(); // The groups have all completed now /* * Here's where the test really starts. Run a Full GC now: it's no harm * at all (just delays test start by a tiny amount) and hitting one too * early in the test can impair results for short tests. */ JMeterUtils.helpGC(); JMeterContextService.getContext().setSamplingStarted(true); booleanmainGroups= running; // still running at this point, i.e. setUp was not cancelled while (running && iter.hasNext()) {// for each thread group AbstractThreadGroupgroup= iter.next(); //ignore Setup and Post here. We could have filtered the searcher. but then //future Thread Group objects wouldn't execute. if (group instanceof SetupThreadGroup || group instanceof PostThreadGroup) { continue; } groupCount++; StringgroupName= group.getName(); log.info("Starting ThreadGroup: {} : {}", groupCount, groupName); startThreadGroup(group, groupCount, searcher, testLevelElements, notifier); if (serialized && iter.hasNext()) { log.info("Waiting for thread group: {} to finish before starting next group", groupName); group.waitThreadsStopped(); } } // end of thread groups if (groupCount == 0) { // No TGs found log.info("No enabled thread groups found"); } else { if (running) { log.info("All thread groups have been started"); } else { log.info("Test stopped - no more thread groups will be started"); } } //wait for all Test Threads To Exit waitThreadsStopped(); groups.clear(); // The groups have all completed now if (postIter.hasNext()) { groupCount = 0; JMeterContextService.clearTotalThreads(); log.info("Starting tearDown thread groups"); if (mainGroups && !running) { // i.e. shutdown/stopped during main thread groups running = tearDownOnShutdown; // re-enable for tearDown if necessary } while (running && postIter.hasNext()) {//for each setup thread group AbstractThreadGroupgroup= postIter.next(); groupCount++; StringgroupName= group.getName(); log.info("Starting tearDown ThreadGroup: {} : {}", groupCount, groupName); startThreadGroup(group, groupCount, postSearcher, testLevelElements, notifier); if (serialized && postIter.hasNext()) { log.info("Waiting for post thread group: {} to finish before starting next post group", groupName); group.waitThreadsStopped(); } } waitThreadsStopped(); // wait for Post threads to stop } notifyTestListenersOfEnd(testListeners); JMeterContextService.endTest(); if (JMeter.isNonGUI() && SYSTEM_EXIT_FORCED) { log.info("Forced JVM shutdown requested at end of test"); System.exit(0); // NOSONAR Intentional } }
初始化与日志记录:
1 2
log.info("Running the test!"); running = true;
测试开始,并且设置一个标志表明测试正在运行。
采样器变量初始化
1 2 3 4 5 6 7 8 9
SampleEvent.initSampleVariables();
publicstaticvoidinitSampleVariables() { Stringvars= JMeterUtils.getProperty(SAMPLE_VARIABLES); variableNames = vars != null ? vars.split(",") : newString[0]; if (log.isInfoEnabled()) { log.info("List of sample_variables: {}", Arrays.toString(variableNames)); } }
/* * Notification of test listeners needs to happen after function * replacement, but before setting RunningVersion to true. */ SearchByClass<TestStateListener> testListeners = newSearchByClass<>(TestStateListener.class); // TL - S&E test.traverse(testListeners); // Merge in any additional test listeners // currently only used by the function parser testListeners.getSearchResults().addAll(testList); testList.clear(); // no longer needed test.traverse(newTurnElementsOn()); notifyTestListenersOfStart(testListeners);
JMeterContextService.getContext().setSamplingStarted(true); booleanmainGroups= running; // still running at this point, i.e. setUp was not cancelled while (running && iter.hasNext()) {// for each thread group AbstractThreadGroupgroup= iter.next(); //ignore Setup and Post here. We could have filtered the searcher. but then //future Thread Group objects wouldn't execute. if (group instanceof SetupThreadGroup || group instanceof PostThreadGroup) { continue; } groupCount++; StringgroupName= group.getName(); log.info("Starting ThreadGroup: {} : {}", groupCount, groupName); startThreadGroup(group, groupCount, searcher, testLevelElements, notifier); if (serialized && iter.hasNext()) { log.info("Waiting for thread group: {} to finish before starting next group", groupName); group.waitThreadsStopped(); } } // end of thread groups if (groupCount == 0) { // No TGs found log.info("No enabled thread groups found"); } else { if (running) { log.info("All thread groups have been started"); } else { log.info("Test stopped - no more thread groups will be started"); } }//wait for all Test Threads To Exit waitThreadsStopped(); groups.clear(); // The groups have all completed now
if (postIter.hasNext()) { groupCount = 0; JMeterContextService.clearTotalThreads(); log.info("Starting tearDown thread groups"); if (mainGroups && !running) { // i.e. shutdown/stopped during main thread groups running = tearDownOnShutdown; // re-enable for tearDown if necessary } while (running && postIter.hasNext()) {//for each setup thread group AbstractThreadGroupgroup= postIter.next(); groupCount++; StringgroupName= group.getName(); log.info("Starting tearDown ThreadGroup: {} : {}", groupCount, groupName); startThreadGroup(group, groupCount, postSearcher, testLevelElements, notifier); if (serialized && postIter.hasNext()) { log.info("Waiting for post thread group: {} to finish before starting next post group", groupName); group.waitThreadsStopped(); } } waitThreadsStopped(); // wait for Post threads to stop } notifyTestListenersOfEnd(testListeners); JMeterContextService.endTest(); if (JMeter.isNonGUI() && SYSTEM_EXIT_FORCED) { log.info("Forced JVM shutdown requested at end of test"); System.exit(0); // NOSONAR Intentional }
synchronized(testTree) { PreCompilercompiler=newPreCompiler(true); testTree.traverse(compiler); // limit the changes to client only test elements JMeterContextService.initClientSideVariables(compiler.getClientSideVariables()); testTree.traverse(newTurnElementsOn()); testTree.traverse(newConvertListeners()); }