Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.

Commit 9543969

Browse files
committed
Minor improvements
1 parent 1d1887b commit 9543969

File tree

4 files changed

+77
-34
lines changed

4 files changed

+77
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/reports
2626
*.csv
2727
*.log
28+
*.zip
2829

2930
# Mac-specific directory that no other operating system needs.
3031
.DS_Store

benchmarks-api/src/main/java/io/scalecube/benchmarks/BenchmarksSettings.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class BenchmarksSettings {
2424
private static final long NUM_OF_ITERATIONS = Long.MAX_VALUE;
2525
private static final Duration RAMP_UP_DURATION = Duration.ofSeconds(10);
2626
private static final Duration RAMP_UP_INTERVAL = Duration.ofSeconds(1);
27-
private static final String ALIAS = "alias";
27+
private static final boolean CONSOLE_REPORTER_ENABLED = true;
2828

2929
private final int nThreads;
3030
private final Duration executionTaskDuration;
@@ -38,6 +38,7 @@ public class BenchmarksSettings {
3838
private final long numOfIterations;
3939
private final Duration rampUpDuration;
4040
private final Duration rampUpInterval;
41+
private final boolean consoleReporterEnabled;
4142

4243
private final Map<String, String> options;
4344

@@ -51,23 +52,21 @@ private BenchmarksSettings(Builder builder) {
5152
this.executionTaskInterval = builder.executionTaskInterval;
5253
this.reporterInterval = builder.reporterInterval;
5354
this.numOfIterations = builder.numOfIterations;
54-
55+
this.consoleReporterEnabled = builder.consoleReporterEnabled;
5556
this.rampUpDuration = builder.rampUpDuration;
5657
this.rampUpInterval = builder.rampUpInterval;
57-
5858
this.options = builder.options;
59-
60-
this.registry = new MetricRegistry();
61-
6259
this.durationUnit = builder.durationUnit;
6360
this.rateUnit = builder.rateUnit;
6461

62+
this.registry = new MetricRegistry();
63+
6564
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
6665
this.taskName = minifyClassName(stackTrace[stackTrace.length - 1].getClassName());
6766

6867
String time = LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC)
6968
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"));
70-
this.csvReporterDirectory = Paths.get("benchmarks", "results", find(ALIAS, taskName), time).toFile();
69+
this.csvReporterDirectory = Paths.get("benchmarks", "results", find("alias", taskName), time).toFile();
7170
// noinspection ResultOfMethodCallIgnored
7271
this.csvReporterDirectory.mkdirs();
7372
}
@@ -124,6 +123,10 @@ public Duration rampUpInterval() {
124123
return rampUpInterval;
125124
}
126125

126+
public boolean consoleReporterEnabled() {
127+
return consoleReporterEnabled;
128+
}
129+
127130
@Override
128131
public String toString() {
129132
final StringBuilder sb = new StringBuilder("BenchmarksSettings{");
@@ -138,6 +141,7 @@ public String toString() {
138141
sb.append(", rateUnit=").append(rateUnit);
139142
sb.append(", rampUpDuration=").append(rampUpDuration);
140143
sb.append(", rampUpInterval=").append(rampUpInterval);
144+
sb.append(", consoleReporterEnabled=").append(consoleReporterEnabled);
141145
sb.append(", registry=").append(registry);
142146
sb.append(", options=").append(options);
143147
sb.append('}');
@@ -160,6 +164,7 @@ public static class Builder {
160164
private long numOfIterations = NUM_OF_ITERATIONS;
161165
private Duration rampUpDuration = RAMP_UP_DURATION;
162166
private Duration rampUpInterval = RAMP_UP_INTERVAL;
167+
private boolean consoleReporterEnabled = CONSOLE_REPORTER_ENABLED;
163168

164169
public Builder from(String[] args) {
165170
this.parse(args);
@@ -218,6 +223,11 @@ public Builder rampUpInterval(Duration rampUpInterval) {
218223
return this;
219224
}
220225

226+
public Builder consoleReporterEnabled(boolean consoleReporterEnabled) {
227+
this.consoleReporterEnabled = consoleReporterEnabled;
228+
return this;
229+
}
230+
221231
public BenchmarksSettings build() {
222232
return new BenchmarksSettings(this);
223233
}
@@ -250,6 +260,9 @@ private void parse(String[] args) {
250260
case "rampUpIntervalInMillis":
251261
rampUpInterval(Duration.ofMillis(Long.parseLong(value)));
252262
break;
263+
case "consoleReporterEnabled":
264+
consoleReporterEnabled(Boolean.parseBoolean(value));
265+
break;
253266
default:
254267
addOption(key, value);
255268
break;

benchmarks-api/src/main/java/io/scalecube/benchmarks/BenchmarksState.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
import java.time.Duration;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.concurrent.CountDownLatch;
2627
import java.util.concurrent.Executors;
28+
import java.util.concurrent.ThreadFactory;
2729
import java.util.concurrent.TimeUnit;
2830
import java.util.concurrent.atomic.AtomicBoolean;
2931
import java.util.function.BiFunction;
@@ -53,12 +55,11 @@ public class BenchmarksState<SELF extends BenchmarksState<SELF>> {
5355
private static final Logger LOGGER = LoggerFactory.getLogger(BenchmarksState.class);
5456

5557
private static final String MDC_BENCHMARK_DIR = "benchmarkDir";
56-
private static final String MDC_BENCHMARK_NAME = "benchmarkName";
5758

5859
protected final BenchmarksSettings settings;
5960

60-
protected Scheduler scheduler;
61-
protected List<Scheduler> schedulers;
61+
private Scheduler scheduler;
62+
private List<Scheduler> schedulers;
6263

6364
private ConsoleReporter consoleReporter;
6465
private CsvReporter csvReporter;
@@ -86,27 +87,30 @@ public final void start() {
8687
}
8788

8889
MDC.put(MDC_BENCHMARK_DIR, settings.csvReporterDirectory().toString());
89-
MDC.put(MDC_BENCHMARK_NAME, settings.taskName());
9090

9191
LOGGER.info("Benchmarks settings: " + settings);
9292

9393
settings.registry().register(settings.taskName() + "-memory", new MemoryUsageGaugeSet());
9494

95-
consoleReporter = ConsoleReporter.forRegistry(settings.registry())
96-
.outputTo(System.out)
97-
.convertDurationsTo(settings.durationUnit())
98-
.convertRatesTo(settings.rateUnit())
99-
.build();
95+
if (settings.consoleReporterEnabled()) {
96+
consoleReporter = ConsoleReporter.forRegistry(settings.registry())
97+
.outputTo(System.out)
98+
.convertDurationsTo(settings.durationUnit())
99+
.convertRatesTo(settings.rateUnit())
100+
.build();
101+
}
100102

101103
csvReporter = CsvReporter.forRegistry(settings.registry())
102104
.convertDurationsTo(settings.durationUnit())
103105
.convertRatesTo(settings.rateUnit())
104106
.build(settings.csvReporterDirectory());
105107

106-
scheduler = Schedulers.fromExecutor(Executors.newFixedThreadPool(settings.nThreads()));
108+
scheduler = Schedulers.fromExecutor(
109+
Executors.newFixedThreadPool(settings.nThreads(), newThreadFactory()));
107110

108111
schedulers = IntStream.rangeClosed(1, settings.nThreads())
109-
.mapToObj(i -> Schedulers.fromExecutorService(Executors.newSingleThreadScheduledExecutor()))
112+
.mapToObj(i -> Schedulers.fromExecutorService(
113+
Executors.newSingleThreadScheduledExecutor(newThreadFactory())))
110114
.collect(Collectors.toList());
111115

112116
try {
@@ -115,16 +119,20 @@ public final void start() {
115119
throw new IllegalStateException("BenchmarksState beforeAll() failed: " + ex, ex);
116120
}
117121

118-
consoleReporter.start(settings.reporterInterval().toMillis(), TimeUnit.MILLISECONDS);
122+
if (settings.consoleReporterEnabled()) {
123+
consoleReporter.start(settings.reporterInterval().toMillis(), TimeUnit.MILLISECONDS);
124+
}
125+
119126
csvReporter.start(settings.reporterInterval().toMillis(), TimeUnit.MILLISECONDS);
120127

121128
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
122129
if (started.get()) {
123130
csvReporter.report();
124-
consoleReporter.report();
131+
if (consoleReporter != null) {
132+
consoleReporter.report();
133+
}
125134
}
126135
}));
127-
128136
}
129137

130138
/**
@@ -159,7 +167,6 @@ public final void shutdown() {
159167
throw new IllegalStateException("BenchmarksState afterAll() failed: " + ex, ex);
160168
} finally {
161169
MDC.remove(MDC_BENCHMARK_DIR);
162-
MDC.remove(MDC_BENCHMARK_NAME);
163170
}
164171
}
165172

@@ -331,4 +338,24 @@ public final <T> void runWithRampUp(
331338
self.shutdown();
332339
}
333340
}
341+
342+
private ThreadFactory newThreadFactory() {
343+
return runnable -> {
344+
Map<String, String> mdcCopy = MDC.getCopyOfContextMap();
345+
346+
return new Thread(runnable) {
347+
348+
private boolean mdcWasSet;
349+
350+
@Override
351+
public void run() {
352+
if (!mdcWasSet) {
353+
MDC.setContextMap(mdcCopy);
354+
mdcWasSet = true;
355+
}
356+
super.run();
357+
}
358+
};
359+
};
360+
}
334361
}

benchmarks-api/src/main/resources/log4j2.xml renamed to benchmarks-examples/src/main/resources/log4j2.xml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88
<Appenders>
99

1010
<Routing name="separateBenchmarkLogs">
11-
<Routes pattern="$${ctx:benchmarkDir}_$${ctx:benchmarkName}">
12-
<!-- This route is chosen if ThreadContext has values for keys: benchmarkDir, benchmarkName. -->
11+
<Routes pattern="$${ctx:benchmarkDir}">
12+
<!-- This route is chosen if ThreadContext has values for keys: benchmarkDir -->
1313
<Route>
14-
<RollingFile name="Rolling-${ctx:benchmarkDir}-${ctx:benchmarkName}"
15-
fileName="${ctx:benchmarkDir}/${ctx:benchmarkName}.log"
16-
filePattern="${ctx:benchmarkDir}/${ctx:benchmarkName}-%d{yyyy-MM-dd-HH}.log">
14+
<RollingFile name="Rolling-${ctx:benchmarkDir}"
15+
fileName="${ctx:benchmarkDir}/benchmarks.log"
16+
filePattern="${ctx:benchmarkDir}/benchmarks-%d{yyyy-MM-dd-HH}-%i.log.zip"
17+
ignoreExceptions="false">
1718
<PatternLayout pattern="${patternLayout}"/>
1819
<Policies>
19-
<OnStartupTriggeringPolicy/>
2020
<TimeBasedTriggeringPolicy/>
21-
<SizeBasedTriggeringPolicy size="50 MB"/>
21+
<SizeBasedTriggeringPolicy size="50MB"/>
2222
</Policies>
23+
<DefaultRolloverStrategy max="1000"/>
2324
</RollingFile>
2425
</Route>
2526

26-
<!-- This route is chosen if ThreadContext has no value for keys: benchmarkDir, benchmarkName. -->
27-
<Route key="$${ctx:benchmarkDir}_$${ctx:benchmarkName}">
27+
<!-- This route is chosen if ThreadContext has no value for keys: benchmarkDir -->
28+
<Route key="$${ctx:benchmarkDir}">
2829
<RollingFile name="Rolling-default"
2930
fileName="benchmarks/default.log"
30-
filePattern="benchmarks/default-%d{yyyy-MM-dd-HH}.log">
31+
filePattern="benchmarks/default-%d{yyyy-MM-dd-HH}-%i.log.zip"
32+
ignoreExceptions="false">
3133
<PatternLayout pattern="${patternLayout}"/>
3234
<Policies>
33-
<OnStartupTriggeringPolicy/>
3435
<TimeBasedTriggeringPolicy/>
35-
<SizeBasedTriggeringPolicy size="50 MB"/>
36+
<SizeBasedTriggeringPolicy size="50MB"/>
3637
</Policies>
38+
<DefaultRolloverStrategy max="1000"/>
3739
</RollingFile>
3840
</Route>
3941

0 commit comments

Comments
 (0)