1 19 20 package org.apache.jmeter.module.loadgenerator.spi.impl; 21 22 import java.awt.Image ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.concurrent.Semaphore ; 28 import org.apache.jmeter.engine.JMeterEngineException; 29 import org.apache.jmeter.module.exceptions.InitializationException; 30 import org.apache.jmeter.module.integration.*; 31 import org.apache.jmeter.util.JMeterUtils; 32 import org.netbeans.modules.loadgenerator.spi.ProcessInstance; 33 import org.netbeans.modules.loadgenerator.spi.Engine; 34 import org.openide.ErrorManager; 35 36 40 public class JMeterProcess extends ProcessInstance { 41 private Semaphore runSwitchLock = new Semaphore (1); 42 private boolean running = false; 43 44 private ProcessDescriptor runningProcess = null; 45 46 private PropertyChangeListener processStateChangeListener = new PropertyChangeListener () { 47 public void propertyChange(PropertyChangeEvent evt) { 48 final boolean state = ((Boolean )evt.getNewValue()).booleanValue(); 49 50 setRunning(state); 51 if (state) { 52 getWriter().println("JMeter test plan running"); 53 publishStart(getEngine().getLogPath()); 54 } else { 55 getWriter().println("JMeter test plan stopped"); 56 publishStop(); 57 runningProcess.removePropertyChangeListener(ProcessDescriptor.RUNNING, this); 58 } 59 } 60 }; 61 62 public JMeterProcess(final Engine factory) { 63 super(factory); 64 } 65 66 public boolean isRunning() { 67 try { 68 try { 69 runSwitchLock.acquire(); 70 } catch (InterruptedException e) {} 71 return running; 72 } finally { 73 runSwitchLock.release(); 74 } 75 } 76 77 public String getDisplayName() { 78 if (getCurrentScript() == null) 79 return ""; 80 81 String filename = getCurrentScript(); 82 filename = filename.substring(filename.lastIndexOf(File.separatorChar) + 1); 83 return filename; 84 } 85 86 public Image getIcon() { 87 return JMeterUtils.getImage("beaker.gif").getImage(); 88 } 89 90 private void setRunning(final boolean value) { 91 try { 92 try { 93 runSwitchLock.acquire(); 94 } catch (InterruptedException e) {} 95 running = value; 96 } finally { 97 runSwitchLock.release(); 98 } 99 } 100 101 private JMeterIntegrationEngine getEngine() { 102 try { 103 return JMeterIntegrationEngine.getDefault(); 104 } catch (InitializationException e) { 105 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 106 } 107 return null; 108 } 109 110 public void performStart(final String scriptFileName) { 111 try { 112 getWriter().reset(); 113 getWriter().print("Starting JMeter subsystem... "); 114 runningProcess = getEngine().prepareTest(scriptFileName); 115 116 getWriter().println("Done"); 117 if (runningProcess != null) { 118 runningProcess.addPropertyChangeListener(ProcessDescriptor.RUNNING, processStateChangeListener); 119 getEngine().clearLog(); 120 getWriter().println("Starting JMeter test plan named " + runningProcess.getDisplayName() + " (" + scriptFileName + ")"); 121 getWriter().println("Simulating " + runningProcess.getThreadsCount() + " users with ramp-up time of " + runningProcess.getRampup() + "s"); 122 runningProcess.start(); 123 } else { 124 throw new JMeterEngineException("Can't start JMeter script " + scriptFileName); 125 } 126 } catch (JMeterEngineException e) { 127 ErrorManager.getDefault().log(ErrorManager.EXCEPTION, e.getMessage()); 128 } catch (IOException e) { 129 ErrorManager.getDefault().log(ErrorManager.EXCEPTION, e.getMessage()); 130 } 131 } 132 133 public void performStop(final boolean force) { 134 if (runningProcess != null) { 135 getWriter().println("Stopping JMeter test plan"); 136 runningProcess.stop(); 137 } else { 138 ErrorManager.getDefault().log(ErrorManager.WARNING, "Stopping a non-running instance"); 139 } 140 } 141 } 142 | Popular Tags |