'How I can run all performance tests from fat jar with Gatling?

I have been trying to execute all my performance tests from my gatling fat-jar created with the assemble plugin, however, when I try to execute my performance tests I got the following error.

Command that I ran: java -jar performance-test-fat-tests.jar Result: There is no simulation script. Please check that your scripts are in user-files/simulations

I just can run my performance tests one by one using the following command: java -jar performance-test-fat-tests.jar -s simulations.MySimulation

but, I wanna run all my performance tests as when I execute my command: mvn gatling:test

Gatling documentation: https://gatling.io/docs/gatling/reference/current/core/configuration/

Thanks in advance



Solution 1:[1]

My solution looks something like following

public class Application {

  public static void main(String[] args) {
    final Set<String> simulations =
        findAllSimulations()
            .filter(simulation -> args.length == 0 || Arrays.stream(args).anyMatch(simulation::endsWith))
            .collect(Collectors.toSet());

    if (simulations.isEmpty()) throw new RuntimeException("Unable to find any simulation to run");

    simulations.forEach(Application::runGatlingSimulation);
  }

  private static Stream<String> findAllSimulations() {
    final String packageName = Application.class.getPackageName();
    System.out.printf("Finding simulations in %s package%n", packageName);

    final Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));
    return reflections.getSubTypesOf(Simulation.class).stream().map(Class::getName);
  }

  private static void runGatlingSimulation(String simulationFileName) {
    System.out.printf("Starting %s simulation%n", simulationFileName);
    final GatlingPropertiesBuilder gatlingPropertiesBuilder = new GatlingPropertiesBuilder();

    gatlingPropertiesBuilder.simulationClass(simulationFileName);
    gatlingPropertiesBuilder.resultsDirectory("test-reports");
    try {
      Gatling.fromMap(gatlingPropertiesBuilder.build());
    } catch (Exception exception) {
      System.err.printf(
          "Something went wrong for simulation %s %s%n", simulationFileName, exception);
    }
  }
}

Once you have the above Application.java we can use it as Main-Class for fatjar

configurations {
    fatJarDependencies.extendsFrom gatling
}

task fatJar(type: Jar, dependsOn: ['gatlingClasses', 'processResources']) {
    group =  "build"
    manifest {
        attributes 'Implementation-Title': project.name,
                'Implementation-Version': project.version,
                'Main-Class': 'com.suman.example.gatling.Application'
    }

    exclude 'META-INF/MANIFEST.MF'
    exclude 'META-INF/*.SF'
    exclude 'META-INF/*.DSA'
    exclude 'META-INF/*.RSA'

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    archiveClassifier = "all"

    from files(sourceSets.main.output.classesDirs)
    from files(sourceSets.gatling.output)
    from {
        configurations['fatJarDependencies']
                .collect { it.isDirectory() ? it : zipTree(it) }
    }
    with jar
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suman Maity