grinder进程管理分析

grinder agent的进程有2种,agent进程和子进程worker。
grinder agent进程的主入口是Grinder.java类。
1. 进程的启动。
class Grinder.java
  public static void main(String[] args) {

    try {
      final Grinder grinder = new Grinder(args, logger);
      //调用grinder的run方法。
      grinder.run();
    }
  private void run() throws GrinderException {
    //调用agent的run
    m_agent.run();
class AgentImplementation.java
 //m_agent_run的实现
 public void run() throws GrinderException {
 //通过workerLauncher启动所有的workers进程。    
 workerLauncher.startAllWorkers();
 //workerLauncher的初始化,传入proerties.getInt参数,workerfactory,m_eventSynchronisation。m_logge

          final WorkerLauncher workerLauncher =
            new WorkerLauncher(properties.getInt("grinder.processes", 1),
                               workerFactory,
                               m_eventSynchronisation,
                               m_logger);

 GrinderProperties properties;
 //workerFatory的定义,需要workerCommandLine,m_agentIdentity,m_fanOutStreamSender,script,properties参数
            workerFactory =
              new ProcessWorkerFactory(
                workerCommandLine, m_agentIdentity, m_fanOutStreamSender,
                consoleCommunication != null, script, properties);
//workerCommandLine的定义,需要properties, script.getDirectory()参数
            final WorkerProcessCommandLine workerCommandLine =
              new WorkerProcessCommandLine(properties,
                                           System.getProperties(),
                                           jvmArguments,
                                           script.getDirectory());
//以默认脚本为例
          if (script == null) {
            //scriptFile取GrinderProperties定义的DEFAULT_SCRIPT
            final File scriptFile =
              properties.resolveRelativeFile(
                properties.getFile(GrinderProperties.SCRIPT,
                                   GrinderProperties.DEFAULT_SCRIPT));
            // 传入file scriptFile
            script = new ScriptLocation(scriptFile);
          }
// GrinderProperties.java
  //默认脚本为grinder.py
  public static final File DEFAULT_SCRIPT = new File("grinder.py");
// ScriptLocation.java
  public ScriptLocation(File file) throws EngineException {
    this(new Directory(), file);
  }
//Directory.java
 public Directory() {
  //路径是当前路径
    m_directory = new File(".");
  }

//继续回到 workerLauncher.startAllWorkers()的实现。
class WorkerLauncher.java
  public void startAllWorkers() throws EngineException {
//调用startSomeWorkers方法。
    startSomeWorkers(m_workers.length - m_nextWorkerIndex);
  }
  public boolean startSomeWorkers(int numberOfWorkers)
    throws EngineException {

  private final WorkerFactory m_workerFactory;

  final Worker worker = m_workerFactory.create(System.out, System.err);

// class AbstractWorkerFactory.java

public Worker create(
    OutputStream outputStream, OutputStream errorStream)
    throws EngineException {

    final WorkerIdentityImplementation workerIdentity =
      m_agentIdentity.createWorkerIdentity();
 //创建work而进程
 final Worker worker = createWorker(workerIdentity,
                                       outputStream,
                                       errorStream);

//class ProcessWorkerFactory.java

 protected Worker createWorker(WorkerIdentityImplementation workerIdentity,
                                OutputStream outputStream,
                                OutputStream errorStream)
    throws EngineException {

    return new ProcessWorker(workerIdentity,
                             m_commandLine,
                             outputStream,
                             errorStream);
  }

// class ProcessWorker.java
  public ProcessWorker(WorkerIdentity workerIdentity,
                       CommandLine commandLine,
                       OutputStream outputStream,
                       OutputStream errorStream)
    throws EngineException {

    m_workerIdentity = workerIdentity;
    //processBuilder传入commandLine.getCommandList()参数
    final ProcessBuilder processBuilder =
      new ProcessBuilder(commandLine.getCommandList());

    processBuilder.directory(commandLine.getWorkingDirectory().getFile());

    try {
    //通过processBuilder.start
      m_process = processBuilder.start();
    }
//class WorkerProcessCommandLine.java

private final List m_command;

public List getCommandList() {
// m_command是个List
    return m_command;
  }

  public WorkerProcessCommandLine(GrinderProperties properties,
                                  Properties systemProperties,
                                  String jvmArguments,
                                  Directory workingDirectory)
    throws EngineException {
    m_command.add(properties.getProperty("grinder.jvm", "java"));
    m_command.add("-classpath");
    // m_command增加了WorkerProcessEntryPoint的启动。WorkerProcess子进程的入口。
    m_command.add(WorkerProcessEntryPoint.class.getName());

}

// class WorkerProcessEntryPoint.java
//WorkerProcess子进程的入口。
  public int run(InputStream agentCommunicationStream) {
 grinderProcess.run();
}
//class GrinderProcess.java
//The controller for a worker process.

  public void run() throws GrinderException {
   synchronized (m_eventSynchronisation) {
      m_threadStarter =
        new ThreadStarterImplementation(threadSynchronisation, scriptEngine);
      //启动线程。
      for (int i = 0; i < numberOfThreads; i++) {
        m_threadStarter.startThread(null);
      }
    }

2. 进程的销毁
class AgentImplementation
                // destroyAllWorkers() prevents further workers from starting.
                workerLauncher.destroyAllWorkers();
class WorkerLauncher

 public void destroyAllWorkers() {
    dontStartAnyMore();

    synchronized (m_workers) {
      for (int i = 0; i < m_workers.length; i++) {
        if (m_workers[i] != null) {
          m_workers[i].destroy();
        }
      }
    }
  }
class ProcessWorker
  public void destroy() {
//destroy Process
  m_process.destroy();
}




 








你可能感兴趣的:(grinder)