运行 具有不同参数的相同 gradle 任务并行?

Running same gradle task with different parameters in parallel?

我想同时 运行 两个具有不同参数(在 2 个环境中)的 gatlingRun 任务(由 gatling gradle 插件创建)。

我知道newgradle提供了WorkerAPI,但是这个好像不适用这种情况。

正如您所说,Gradle Worker API 并不真正适用于您的情况,因为它支持并行处理,但在单个 Task 的上下文中,如文档所述:

The Worker API provides the ability to break up the execution of a task action into discrete units of work and then to execute that work concurrently and asynchronously.

为了并行执行不同的任务 运行,您可以使用 Parallel execution 功能。但请注意,只有当它们属于不同的子项目(同一多项目构建)时,您才能 运行 并行执行任务:

Most builds consist of more than one project and some of those projects are usually independent of one another. Yet Gradle will only run one task at a time by default, regardless of the project structure (this will be improved soon). By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.

如果您的构建脚本被 "only" 用作 Gatlin 负载测试的启动器,那么也许您可以将构建脚本实现为多项目构建,每个目标环境有一个子项目进行测试;然后您可以启用 --parallel 选项以并行执行负载测试任务。

@M.Ricciuti 工人 API 的能力是错误​​的。

Gradle 的文档对此含糊不清,但是 I asked and got the answer Worker API 确实允许我们 运行 在同一个项目中并行执行多个任务。

这是一个工作示例,任务不同 类:

import javax.inject.Inject

class Sleeping5 extends DefaultTask {
  static class Sleeper5 implements Runnable {
    String name
    @Inject
    public Sleeper5(String name) {
      this.@name = name
    }
    @Override
    void run() {
      (1..50).each { int i ->
        Thread.sleep(1000)
        println "$name: $i"
      }
    }
  }
  private final WorkerExecutor workerExecutor
  @Inject
  public Sleeping5(WorkerExecutor workerExecutor) {
    this.workerExecutor = workerExecutor
  }
  @TaskAction
  void sleep() {
    workerExecutor.submit(Sleeper5, new Action<WorkerConfiguration>() {
      @Override
      void execute(WorkerConfiguration config) {
        config.isolationMode = IsolationMode.NONE
        config.displayName = name
        config.params = [name]
      }
    })
  }
}

class Sleeping10 extends DefaultTask {
  static class Sleeper10 implements Runnable {
    String name
    @Inject
    public Sleeper10(String name) {
      this.@name = name
    }
    @Override
    void run() {
      (1..70).each { int i ->
        Thread.sleep(1000)
        println "$name: $i"
      }
    }
  }
  private final WorkerExecutor workerExecutor
  @Inject
  public Sleeping10(WorkerExecutor workerExecutor) {
    this.workerExecutor = workerExecutor
  }
  @TaskAction
  void sleep() {
    workerExecutor.submit(Sleeper10, new Action<WorkerConfiguration>() {
      @Override
      void execute(WorkerConfiguration config) {
        config.isolationMode = IsolationMode.NONE
        config.displayName = name
        config.params = [name]
      }
    })
  }
}
task(type: Sleeping5, 'sleep5')
task(type: Sleeping10, 'sleep10')

假设你有多核 CPU 或设置 gradle.workers.max 属性,并行任务 sleep5sleep10 运行。

但是,根据你的实际情况,还是不行。

工作人员 API 应该在任务实施中使用,GatlingRunTask 没有使用它。

您或插件作者必须重写任务实现才能在内部使用 Worker API。