Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Aug 23, 2023
1 parent e71a722 commit 44b6d98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import net.thucydides.model.digest.Digest;
import net.thucydides.model.util.NameConverter;

import java.util.HashMap;
import java.util.Map;

import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_COMPRESS_FILENAMES;

/**
Expand All @@ -13,8 +16,12 @@
*/
public class ReportNamer {

private static final Map<ReportType, ReportNamer> REPORT_NAMERS = new HashMap<>();
public static ReportNamer forReportType(ReportType type) {
return new ReportNamer(type);
if (!REPORT_NAMERS.containsKey(type)) {
REPORT_NAMERS.put(type, new ReportNamer(type));
}
return REPORT_NAMERS.get(type);
}

private ReportType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ public class SystemEnvironmentVariables implements EnvironmentVariables {
private volatile Config config;
private volatile boolean configLoaded = false;

private static final ThreadLocal<SystemEnvironmentVariables> LOADED_ENVIRONMENT_VARIABLES
= ThreadLocal.withInitial(SystemEnvironmentVariables::createEnvironmentVariables);
private static SystemEnvironmentVariables CACHED_ENVIRONMENT_VARIABLES;

private static SystemEnvironmentVariables getCachedEnvironmentVariables() {
if (CACHED_ENVIRONMENT_VARIABLES == null) {
CACHED_ENVIRONMENT_VARIABLES = createEnvironmentVariables();
}
return CACHED_ENVIRONMENT_VARIABLES;
}

/**
* System properties as loaded from the system, without test-specific configuration
Expand Down Expand Up @@ -57,15 +63,15 @@ public SystemEnvironmentVariables() {
}

public static EnvironmentUpdater currentEnvironment() {
return new EnvironmentUpdater(LOADED_ENVIRONMENT_VARIABLES.get().loadLocalConfig());
return new EnvironmentUpdater(getCachedEnvironmentVariables().loadLocalConfig());
}

/**
* Get the current environment variables, including any values updated for the scope of this test.
* Test-local environment variables can be updated using the TestLocalEnvironmentVariables class.
*/
public static EnvironmentVariables currentEnvironmentVariables() {
return LOADED_ENVIRONMENT_VARIABLES.get().copy();
return getCachedEnvironmentVariables();
}

public void setConfig(Config typesafeConfig) {
Expand Down Expand Up @@ -298,11 +304,8 @@ private static SystemEnvironmentVariables createEnvironmentVariables(SystemEnvir
return environmentVariables;
}

ReentrantLock configLock = new ReentrantLock();

private EnvironmentVariables loadLocalConfig() {
if (!configLoaded) {
configLock.lock();
LocalPreferences localPreferences = new PropertiesLocalPreferences(this.properties);
try {
localPreferences.loadPreferences();
Expand All @@ -311,7 +314,6 @@ private EnvironmentVariables loadLocalConfig() {
e.printStackTrace();
}
configLoaded = true;
configLock.unlock();
}
return this;
}
Expand Down

0 comments on commit 44b6d98

Please sign in to comment.