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<String> m_command;

public List<String> 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();
}




 








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值