Advertisement
Guest User

Untitled

a guest
May 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.52 KB | None | 0 0
  1. public void runTestWithPercentages(String format, int threadCount, String resultsFile, String firstFile,String secondFile,int firstPercent, int secondPercent,int iterations)throws IOException, FileNotFoundException
  2.   {
  3.  
  4.    Double firstValue=new Double(firstPercent);
  5.    Double secondValue = new Double(secondPercent);
  6.    Double total= new Double(threadCount);
  7.  
  8.     Double threadCount1 = ((total) * (firstValue/100));
  9.     Double threadCount2 = ((total) * (secondValue/100));
  10.  
  11.     System.out.println(threadCount1);
  12.     System.out.println(threadCount2);
  13.  
  14.  
  15.  
  16.     //Create the JMeter engine to be used (similar to Android's GUI engine)
  17.     StandardJMeterEngine jEngine = new StandardJMeterEngine();
  18.  
  19.     JMeterUtils.setJMeterHome("target/jmeter");
  20.     //import the jmeter properties, as is provided
  21.     JMeterUtils.loadJMeterProperties("target/jmeter/bin/jmeter.properties");
  22.     //Set locale
  23.     JMeterUtils.initLocale();
  24.  
  25.     //Will be used to compose the testPlan, acts as a container
  26.     HashTree hashTree = new HashTree();
  27.  
  28.     //HTTPSampler acts as the container for the request to the site.
  29.  
  30.     Arguments newParameters = new Arguments();
  31.  
  32.     newParameters
  33.         .addArgument("file",firstFile);
  34.  
  35.     JavaSamplerContext context = new JavaSamplerContext(newParameters);
  36.  
  37.     CDCSampler httpHandler = new CDCSampler();
  38.  
  39.     httpHandler.setName("CDCAPIRequest");
  40.     httpHandler.setClassname("com.icims.jmeter.test.update.CDCSampler");
  41.     httpHandler.setArguments(newParameters);
  42.     httpHandler.setProperty(TestElement.TEST_CLASS, JavaSampler.class.getName());
  43.     httpHandler.setProperty(TestElement.GUI_CLASS, JavaTestSamplerGui.class.getName());
  44.     // httpHandler.runTest(context); let engine call this itself
  45.  
  46.     //Add other pieces
  47. //THIS IS THREAD GROUP 1
  48.     //LoopController, handles iteration settings
  49.     LoopController controller = new LoopController();
  50.     controller.setLoops(iterations);
  51.     controller.setFirst(true);
  52.     controller.initialize();
  53.     //Adding pieces to enable this to be exported to a .jmx file
  54.     controller.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
  55.     controller.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
  56.     controller.initialize();
  57.  
  58.     //Thread groups/user count/multi threading
  59.     SetupThreadGroup group = new SetupThreadGroup();
  60.     group.setName("TG1");
  61.     group.setNumThreads(threadCount1.intValue());
  62.     group.setRampUp(1);
  63.     group.setSamplerController(controller);
  64.     //Adding pieces to enable this to be exported to a .jmx file
  65.     group.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
  66.     group.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
  67.  
  68.     //Would be Test Plan
  69.     TestPlan testPlan = new TestPlan("CDCPerformenceTesting"+threadCount);
  70.     //Adding pieces to enable this to be exported to a .jmx file
  71.     testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
  72.     testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
  73.     testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
  74.  
  75.     hashTree.add(testPlan);
  76.  
  77.     //IMPORTANT : Although it seems like this variable is not used, if these lines aren't included, program fails.
  78.     //This is the testplan adopting group, then group adopts HTTPHandler; So they're transitively linked
  79.     HashTree groupTree = hashTree.add(testPlan,group);
  80.     groupTree.add(httpHandler);
  81.  
  82.     //THIS IS THREAD GROUP 2
  83.     //LoopController, handles iteration settings
  84.     //The same loop controller can be used in the second thread group
  85.  
  86.     //Thread groups/user count/multi threading
  87.     SetupThreadGroup group2 = new SetupThreadGroup();
  88.     group.setName("TG2");
  89.     group.setNumThreads(threadCount2.intValue());
  90.     group.setRampUp(1);
  91.     group.setSamplerController(controller);
  92.     //Adding pieces to enable this to be exported to a .jmx file
  93.     group.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
  94.     group.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
  95.  
  96.     //Have to create another sampler with different input file
  97.     //HTTPSampler acts as the container for the request to the site.
  98.  
  99.     Arguments newParameters2 = new Arguments();
  100.  
  101.     newParameters2
  102.         .addArgument("file",secondFile);
  103.  
  104.      context = null;
  105.      context = new JavaSamplerContext(newParameters2);
  106.  
  107.     CDCSampler httpHandler2 = new CDCSampler();
  108.  
  109.     httpHandler.setName("CDCAPIRequest2");
  110.     httpHandler.setClassname("com.icims.jmeter.test.update.CDCSampler");
  111.     httpHandler.setArguments(newParameters2);
  112.     httpHandler.setProperty(TestElement.TEST_CLASS, JavaSampler.class.getName());
  113.     httpHandler.setProperty(TestElement.GUI_CLASS, JavaTestSamplerGui.class.getName());
  114.  
  115.     //IMPORTANT : Although it seems like this variable is not used, if these lines aren't included, program fails.
  116.     //This is the testplan adopting group, then group adopts HTTPHandler; So they're transitively linked
  117.     SetupThreadGroup[] arrObject = {group,group2};
  118.     hashTree.add(testPlan,arrObject);
  119.     hashTree.add(httpHandler2);
  120.  
  121.  
  122.  
  123.     //
  124.     //In order to get actual useful information to console, use a Summarizer
  125.  
  126.     Summariser summeriser = new Summariser("summaryOfResults");
  127.  
  128.     //Collect results
  129.     ResultCollector logger = new ResultCollector(summeriser);
  130.  
  131.     logger.setFilename("src/pt/resources/JMetrics/"+resultsFile+".csv");
  132.  
  133.     hashTree.add(hashTree.getArray()[0],logger);
  134.  
  135.     jEngine.configure(hashTree);
  136.     jEngine.run();
  137.  
  138.  
  139.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement