Skip to content

Commit

Permalink
dynamic cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
s-vivien committed May 11, 2021
1 parent c3447c0 commit 0490336
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ seedList:
- seed=586660547
- seed=410110611

# Cooldown between every match, 20 is the minimum to avoid CG's limitation
# /!\ WARNING /!\ : During the Spring-Challenge-2021, the restrictions on CG's side are much stronger, expect a few errors with that cooldown
requestCooldown: 20

# [0, N] forced start position at N
# -1 : Each seed is played with every starting positions configuration. (Works only with fixed seed list).
# In 1v1, it will generate 2 games, 6 games in 1v2 and 24 games in 1v3. Best suited for non symmetrical and/or turn-based games.
Expand Down Expand Up @@ -110,6 +106,7 @@ defaultEnemies:
```

### Latest features :
- Cooldown is now dynamic, applying the lowest one first and adjusting it as long as CGBenchmark hits the servers' limitations. FYI : the maximum number of games one account can play is now 600 per 24 hours.
- New CG API compliance
- Confidence interval in winrate stats
- Password isn't mandatory in the config file. If not provided, it will be asked in a prompt
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'

sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.2.0'
version = '1.3.0'

repositories {
mavenCentral()
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/fr/svivien/cgbenchmark/CGBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.commons.logging.LogFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.representer.Representer;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
Expand Down Expand Up @@ -92,7 +93,7 @@ public CGBenchmark(String cfgFilePath, boolean saveLogs) {
} else {
retrieveAccountCookieAndSession(accountCfg);
}
consumers.add(new Consumer(testBroker, accountCfg, globalConfiguration.getRequestCooldown(), pause, globalConfiguration.isSaveLogs()));
consumers.add(new Consumer(testBroker, accountCfg, pause, globalConfiguration.isSaveLogs()));
LOG.info("Account " + accountCfg.getAccountName() + " successfully registered");
}
} catch (IllegalStateException e) {
Expand Down Expand Up @@ -361,7 +362,9 @@ private void checkConfiguration(GlobalConfiguration globalConfiguration) throws

private GlobalConfiguration parseConfigurationFile(String cfgFilePath) throws FileNotFoundException {
LOG.info("Loading configuration file : " + cfgFilePath);
Yaml yaml = new Yaml(new Constructor(GlobalConfiguration.class));
Representer representer = new Representer();
representer.getPropertyUtils().setSkipMissingProperties(true);
Yaml yaml = new Yaml(new Constructor(GlobalConfiguration.class), representer);
FileInputStream configFileInputStream = new FileInputStream(cfgFilePath);
GlobalConfiguration cfg = yaml.load(configFileInputStream);

Expand Down
29 changes: 21 additions & 8 deletions src/main/java/fr/svivien/cgbenchmark/business/Consumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public class Consumer implements Runnable {
private ResultWrapper resultWrapper;
private String cookie;
private String ide;
private int cooldown;
private int cooldownIdx = 0;
private boolean saveLogs;
private long globalStartTime = 0;
private long totalTestNumber = 0;
private long totalPauseDuration = 0;

private AtomicBoolean pause;

public Consumer(TestBroker testBroker, AccountConfiguration accountCfg, int cooldown, AtomicBoolean pause, boolean saveLogs) {
public Consumer(TestBroker testBroker, AccountConfiguration accountCfg, AtomicBoolean pause, boolean saveLogs) {
OkHttpClient client = new OkHttpClient.Builder().readTimeout(600, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder().client(client).baseUrl(Constants.CG_HOST).addConverterFactory(GsonConverterFactory.create()).build();
this.cookie = accountCfg.getAccountCookie();
Expand All @@ -58,7 +58,6 @@ public Consumer(TestBroker testBroker, AccountConfiguration accountCfg, int cool
this.testBroker = testBroker;
this.pause = pause;
this.cgPlayApi = retrofit.create(CGPlayApi.class);
this.cooldown = cooldown;
this.saveLogs = saveLogs;
}

Expand All @@ -83,16 +82,23 @@ public void run() {
resultWrapper.addTestResult(result);
break;
} else {
triggerPause();
// Error occurred, waiting before retrying again
Thread.sleep(tries < Constants.PLAY_TRIES_BEFORE_DEGRADED ? Constants.PLAY_ERROR_RETRY_COOLDOWN : Constants.PLAY_ERROR_RETRY_DEGRADED_COOLDOWN);
if (result.getResultString().contains(Constants.RESTRICTIONS_ERROR_MESSAGE)) {
if (cooldownIdx + 1 < Constants.COOLDOWNS_DURATION.length) {
cooldownIdx++;
LOG.info(String.format("Hitting the server limitations, now using a cooldown of %d seconds between games, suited for a %s long benchmark", Constants.COOLDOWNS_DURATION[cooldownIdx], Constants.COOLDOWNS_NAMES[cooldownIdx]));
}
applyCooldown(tryStart);
} else {
Thread.sleep(Constants.PLAY_ERROR_RETRY_COOLDOWN);
}
triggerPause();
}
}

if (testBroker.size() > 0) {
triggerPause();
// The cooldown is applied on the start-time of each test, and not on the end-time of previous test
Thread.sleep(Math.max(100, cooldown * 1000 - (System.currentTimeMillis() - tryStart)));
triggerPause();
applyCooldown(tryStart);
}
}
LOG.info("Consumer " + this.name + " finished its job.");
Expand All @@ -101,6 +107,13 @@ public void run() {
}
}

private void applyCooldown(long tryStart) throws InterruptedException {
triggerPause();
// The cooldown is applied on the start-time of each test, and not on the end-time of previous test
Thread.sleep(Math.max(100, Constants.COOLDOWNS_DURATION[cooldownIdx] * 1000L - (System.currentTimeMillis() - tryStart)));
triggerPause();
}

public void resetDurationStats() {
totalTestNumber = 0;
totalPauseDuration = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public class GlobalConfiguration {

private List<String> seedList;

@NotNull
@Min(0)
private Integer requestCooldown;

@NotNull
private Boolean randomSeed;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/fr/svivien/cgbenchmark/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ public class Constants {
public static final String SET_COOKIE = "Set-Cookie";
public static final String TIMEOUT_INFORMATION_PART = "timeout";
public static final String TIMEOUT_BIS_INFORMATION_PART = "timed out";
public static final String RESTRICTIONS_ERROR_MESSAGE = "You reached the limit of plays for a period of time.";
public static final String INVALID_INFORMATION_PART = "is not a correct movement";
public static final String INVALID_BIS_INFORMATION_PART = "invalid";
public static final String CONTEST_SESSION_SERVICE_URL = "/services/ChallengeCandidateRemoteService/findChallengerByChallenge";
public static final String PUZZLE_SESSION_SERVICE_URL = "/services/PuzzleRemoteService/generateSessionFromPuzzlePrettyId";
public static final long RANDOM_SEED = 2820027331L;
public static final int PLAY_MAX_RETRIES = 20;
public static final int PLAY_TRIES_BEFORE_DEGRADED = 10;
public static final int PLAY_ERROR_RETRY_COOLDOWN = 20000;
public static final int PLAY_ERROR_RETRY_DEGRADED_COOLDOWN = 40000;
public static final String DOUBLE_FORMAT = "#0.00";

public static final int[] COOLDOWNS_DURATION = {30, 45, 72, 108, 144};
public static final String[] COOLDOWNS_NAMES = {"15 minutes", "1 hour", "3 hours", "6 hours", "24 hours"};
}

0 comments on commit 0490336

Please sign in to comment.