Skip to content

Commit 3ae1285

Browse files
author
Stephen-Bao
committed
Adjusted the code format
1 parent 0a8e009 commit 3ae1285

File tree

4 files changed

+86
-68
lines changed

4 files changed

+86
-68
lines changed

src/jmh/java/software/amazon/cloudwatchlogs/emf/MetricsLoggerBenchmark.java

+38-38
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public void measureAllMethods() {
8080
}
8181

8282
/**
83-
* Each thread publishes 1000 metrics, 10 threads in total.
84-
* no lock: 0.949 ms/op; RW lock: 3.823 ms/op; S lock: 3.078 ms/op
83+
* Each thread publishes 1000 metrics, 10 threads in total. no lock: 0.949 ms/op; RW lock: 3.823
84+
* ms/op; S lock: 3.078 ms/op
8585
*
8686
* @throws InterruptedException
8787
*/
@@ -91,8 +91,8 @@ public void measurePutMetricWith10Threads() throws InterruptedException {
9191
}
9292

9393
/**
94-
* Each thread publishes 1000 metrics, 20 threads in total.
95-
* no lock: 1.860 ms/op; RW lock: 9.806 ms/op; S lock: 7.929 ms/op
94+
* Each thread publishes 1000 metrics, 20 threads in total. no lock: 1.860 ms/op; RW lock: 9.806
95+
* ms/op; S lock: 7.929 ms/op
9696
*
9797
* @throws InterruptedException
9898
*/
@@ -102,8 +102,8 @@ public void measurePutMetricWith20Threads() throws InterruptedException {
102102
}
103103

104104
/**
105-
* Each thread publishes 1000 metrics, 50 threads in total.
106-
* no lock: 6.548 ms/op; RW lock: 28.754 ms/op; S lock: 24.700 ms/op
105+
* Each thread publishes 1000 metrics, 50 threads in total. no lock: 6.548 ms/op; RW lock:
106+
* 28.754 ms/op; S lock: 24.700 ms/op
107107
*
108108
* @throws InterruptedException
109109
*/
@@ -113,8 +113,8 @@ public void measurePutMetricWith50Threads() throws InterruptedException {
113113
}
114114

115115
/**
116-
* Each thread publishes 1000 metrics, 200 threads in total.
117-
* no lock: 37.662 ms/op; RW lock: 135.824 ms/op; S lock: 114.467 ms/op
116+
* Each thread publishes 1000 metrics, 200 threads in total. no lock: 37.662 ms/op; RW lock:
117+
* 135.824 ms/op; S lock: 114.467 ms/op
118118
*
119119
* @throws InterruptedException
120120
*/
@@ -124,8 +124,8 @@ public void measurePutMetricWith200Threads() throws InterruptedException {
124124
}
125125

126126
/**
127-
* Each thread publishes 1000 metrics, 500 threads in total.
128-
* no lock: 90.148 ms/op; RW lock: 345.197 ms/op; S lock: 287.908 ms/op
127+
* Each thread publishes 1000 metrics, 500 threads in total. no lock: 90.148 ms/op; RW lock:
128+
* 345.197 ms/op; S lock: 287.908 ms/op
129129
*
130130
* @throws InterruptedException
131131
*/
@@ -137,8 +137,8 @@ public void measurePutMetricWith500Threads() throws InterruptedException {
137137
}
138138

139139
/**
140-
* Each thread flushes 100 times, 10 threads in total.
141-
* no lock: 12.900 ms/op; RW lock: 25.015 ms/op; S lock: 24.778 ms/op
140+
* Each thread flushes 100 times, 10 threads in total. no lock: 12.900 ms/op; RW lock: 25.015
141+
* ms/op; S lock: 24.778 ms/op
142142
*
143143
* @throws InterruptedException
144144
*/
@@ -148,8 +148,8 @@ public void measureFlushWith10Threads() throws InterruptedException {
148148
}
149149

150150
/**
151-
* Each thread flushes 100 times, 20 threads in total.
152-
* no lock: 20.824 ms/op; RW lock: 47.123 ms/op; S lock: 48.511 ms/op
151+
* Each thread flushes 100 times, 20 threads in total. no lock: 20.824 ms/op; RW lock: 47.123
152+
* ms/op; S lock: 48.511 ms/op
153153
*
154154
* @throws InterruptedException
155155
*/
@@ -159,8 +159,8 @@ public void measureFlushWith20Threads() throws InterruptedException {
159159
}
160160

161161
/**
162-
* Each thread flushes 100 times, 50 threads in total.
163-
* no lock: 77.463 ms/op; RW lock: 121.857 ms/op; S lock: 125.212 ms/op
162+
* Each thread flushes 100 times, 50 threads in total. no lock: 77.463 ms/op; RW lock: 121.857
163+
* ms/op; S lock: 125.212 ms/op
164164
*
165165
* @throws InterruptedException
166166
*/
@@ -170,8 +170,8 @@ public void measureFlushWith50Threads() throws InterruptedException {
170170
}
171171

172172
/**
173-
* Each thread flushes 100 times, 200 threads in total.
174-
* no lock: 390.252 ms/op; RW lock: 474.439 ms/op; S lock: 488.809 ms/op
173+
* Each thread flushes 100 times, 200 threads in total. no lock: 390.252 ms/op; RW lock: 474.439
174+
* ms/op; S lock: 488.809 ms/op
175175
*
176176
* @throws InterruptedException
177177
*/
@@ -181,8 +181,8 @@ public void measureFlushWith200Threads() throws InterruptedException {
181181
}
182182

183183
/**
184-
* Each thread flushes 100 times, 500 threads in total.
185-
* no lock: 300.280 ms/op; RW lock: 1161.098 ms/op; S lock: 1247.972 ms/op
184+
* Each thread flushes 100 times, 500 threads in total. no lock: 300.280 ms/op; RW lock:
185+
* 1161.098 ms/op; S lock: 1247.972 ms/op
186186
*
187187
* @throws InterruptedException
188188
*/
@@ -194,9 +194,9 @@ public void measureFlushWith500Threads() throws InterruptedException {
194194
}
195195

196196
/**
197-
* Each thread executes all methods 100 times, 10 threads in total.
198-
* no lock (need to sync getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 7.215 ms/op;
199-
* RW lock: 32.159; S lock: 34.226
197+
* Each thread executes all methods 100 times, 10 threads in total. no lock (need to sync
198+
* getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 7.215 ms/op; RW lock:
199+
* 32.159; S lock: 34.226
200200
*
201201
* @throws InterruptedException
202202
*/
@@ -206,9 +206,9 @@ public void measureAllMethodsWith10Threads() throws InterruptedException {
206206
}
207207

208208
/**
209-
* Each thread executes all methods 100 times, 20 threads in total.
210-
* no lock (need to sync getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 11.833 ms/op;
211-
* RW lock: 60.510 ms/op; S lock: 75.125 ms/op
209+
* Each thread executes all methods 100 times, 20 threads in total. no lock (need to sync
210+
* getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 11.833 ms/op; RW lock:
211+
* 60.510 ms/op; S lock: 75.125 ms/op
212212
*
213213
* @throws InterruptedException
214214
*/
@@ -218,9 +218,9 @@ public void measureAllMethodsWith20Threads() throws InterruptedException {
218218
}
219219

220220
/**
221-
* Each thread executes all methods 100 times, 50 threads in total.
222-
* no lock (need to sync getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 36.051 ms/op;
223-
* RW lock: 150.022 ms/op; S lock: 244.934 ms/op
221+
* Each thread executes all methods 100 times, 50 threads in total. no lock (need to sync
222+
* getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 36.051 ms/op; RW lock:
223+
* 150.022 ms/op; S lock: 244.934 ms/op
224224
*
225225
* @throws InterruptedException
226226
*/
@@ -230,9 +230,9 @@ public void measureAllMethodsWith50Threads() throws InterruptedException {
230230
}
231231

232232
/**
233-
* Each thread executes all methods 100 times, 200 threads in total.
234-
* no lock (need to sync getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 108.775 ms/op;
235-
* RW lock: 629.826 ms/op; S lock: 1220.959 ms/op
233+
* Each thread executes all methods 100 times, 200 threads in total. no lock (need to sync
234+
* getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 108.775 ms/op; RW lock:
235+
* 629.826 ms/op; S lock: 1220.959 ms/op
236236
*
237237
* @throws InterruptedException
238238
*/
@@ -244,9 +244,9 @@ public void measureAllMethodsWith200Threads() throws InterruptedException {
244244
}
245245

246246
/**
247-
* Each thread executes all methods 100 times, 500 threads in total.
248-
* no lock (need to sync getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 335.183 ms/op;
249-
* RW lock: 1741.003 ms/op; S lock: 4192.327 ms/op
247+
* Each thread executes all methods 100 times, 500 threads in total. no lock (need to sync
248+
* getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 335.183 ms/op; RW lock:
249+
* 1741.003 ms/op; S lock: 4192.327 ms/op
250250
*
251251
* @throws InterruptedException
252252
*/
@@ -258,9 +258,9 @@ public void measureAllMethodsWith500Threads() throws InterruptedException {
258258
}
259259

260260
/**
261-
* Each thread executes all methods 100 times, 1000 threads in total.
262-
* no lock (need to sync getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 575.339 ms/op;
263-
* RW lock: 3230.403 ms/op; S lock: 13519.459 ms/op
261+
* Each thread executes all methods 100 times, 1000 threads in total. no lock (need to sync
262+
* getAllDimensions() & getAllDimensionKeys() in MetricsDirective): 575.339 ms/op; RW lock:
263+
* 3230.403 ms/op; S lock: 13519.459 ms/op
264264
*
265265
* @throws InterruptedException
266266
*/

src/main/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLogger.java

+29-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.concurrent.CompletableFuture;
2121
import java.util.concurrent.locks.ReentrantReadWriteLock;
2222
import java.util.function.Supplier;
23-
2423
import lombok.extern.slf4j.Slf4j;
2524
import software.amazon.cloudwatchlogs.emf.environment.Environment;
2625
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider;
@@ -39,9 +38,10 @@ public class MetricsLogger {
3938
private CompletableFuture<Environment> environmentFuture;
4039
private EnvironmentProvider environmentProvider;
4140
/**
42-
* This lock is used to create an internal sync context for flush() method in multi-threaded situations.
43-
* Flush() acquires write lock, other methods (accessing mutable shared data with flush()) acquires read lock.
44-
* This makes sure flush() is executed exclusively, while other methods can be executed concurrently.
41+
* This lock is used to create an internal sync context for flush() method in multi-threaded
42+
* situations. Flush() acquires write lock, other methods (accessing mutable shared data with
43+
* flush()) acquires read lock. This makes sure flush() is executed exclusively, while other
44+
* methods can be executed concurrently.
4545
*/
4646
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
4747

@@ -98,10 +98,11 @@ public void flush() {
9898
* @return the current logger
9999
*/
100100
public MetricsLogger putProperty(String key, Object value) {
101-
return applyReadLock(() -> {
102-
this.context.putProperty(key, value);
103-
return this;
104-
});
101+
return applyReadLock(
102+
() -> {
103+
this.context.putProperty(key, value);
104+
return this;
105+
});
105106
}
106107

107108
/**
@@ -116,10 +117,11 @@ public MetricsLogger putProperty(String key, Object value) {
116117
* @return the current logger
117118
*/
118119
public MetricsLogger putDimensions(DimensionSet dimensions) {
119-
return applyReadLock(() -> {
120-
context.putDimension(dimensions);
121-
return this;
122-
});
120+
return applyReadLock(
121+
() -> {
122+
context.putDimension(dimensions);
123+
return this;
124+
});
123125
}
124126

125127
/**
@@ -132,10 +134,11 @@ public MetricsLogger putDimensions(DimensionSet dimensions) {
132134
* @return the current logger
133135
*/
134136
public MetricsLogger setDimensions(DimensionSet... dimensionSets) {
135-
return applyReadLock(() -> {
136-
context.setDimensions(dimensionSets);
137-
return this;
138-
});
137+
return applyReadLock(
138+
() -> {
139+
context.setDimensions(dimensionSets);
140+
return this;
141+
});
139142
}
140143

141144
/**
@@ -149,10 +152,11 @@ public MetricsLogger setDimensions(DimensionSet... dimensionSets) {
149152
* @return the current logger
150153
*/
151154
public MetricsLogger putMetric(String key, double value, Unit unit) {
152-
return applyReadLock(() -> {
153-
this.context.putMetric(key, value, unit);
154-
return this;
155-
});
155+
return applyReadLock(
156+
() -> {
157+
this.context.putMetric(key, value, unit);
158+
return this;
159+
});
156160
}
157161

158162
/**
@@ -180,10 +184,11 @@ public MetricsLogger putMetric(String key, double value) {
180184
* @return the current logger
181185
*/
182186
public MetricsLogger putMetadata(String key, Object value) {
183-
return applyReadLock(() -> {
184-
this.context.putMetadata(key, value);
185-
return this;
186-
});
187+
return applyReadLock(
188+
() -> {
189+
this.context.putMetadata(key, value);
190+
return this;
191+
});
187192
}
188193

189194
/**

src/test/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLoggerThreadSafetyTest.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public void testConcurrentPutProperty() throws InterruptedException {
6464
Thread.sleep(targetTimestampToRun - System.currentTimeMillis());
6565
for (int j = 0; j < 1000; j++) {
6666
int propertyId = 1000 * id + j;
67-
logger.putProperty("Property-" + propertyId, String.valueOf(propertyId));
67+
logger.putProperty(
68+
"Property-" + propertyId,
69+
String.valueOf(propertyId));
6870
}
6971
} catch (Throwable e) {
7072
throwable = e; // ensure no exceptions are thrown
@@ -98,7 +100,9 @@ public void testConcurrentPutDimension() throws InterruptedException {
98100
Thread.sleep(targetTimestampToRun - System.currentTimeMillis());
99101
for (int j = 0; j < 1000; j++) {
100102
int dimensionId = 1000 * id + j;
101-
logger.putDimensions(DimensionSet.of("Dim", String.valueOf(dimensionId)));
103+
logger.putDimensions(
104+
DimensionSet.of(
105+
"Dim", String.valueOf(dimensionId)));
102106
}
103107
} catch (Throwable e) {
104108
throwable = e;
@@ -144,7 +148,9 @@ public void testConcurrentPutDimensionAfterSetDimension() throws InterruptedExce
144148
Thread.sleep(targetTimestampToRun - System.currentTimeMillis());
145149
for (int j = 0; j < 1000; j++) {
146150
int dimensionId = 1000 * id + j + 1;
147-
logger.putDimensions(DimensionSet.of("Dim", String.valueOf(dimensionId)));
151+
logger.putDimensions(
152+
DimensionSet.of(
153+
"Dim", String.valueOf(dimensionId)));
148154
}
149155
} catch (Throwable e) {
150156
throwable = e;
@@ -355,7 +361,7 @@ public void testConcurrentFlushAndMethodsOtherThanPutMetric() throws Interrupted
355361
@After
356362
public void tearDown() throws Throwable {
357363
if (throwable != null) throw throwable;
358-
throwable = null; // reset throwable to prevent repeat throwing
364+
throwable = null; // reset throwable to prevent repeat throwing
359365
}
360366

361367
private Map<String, Object> parseRootNode(String event) throws JsonProcessingException {

src/test/java/software/amazon/cloudwatchlogs/emf/model/MetricDirectiveThreadSafetyTest.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ public class MetricDirectiveThreadSafetyTest {
1313
public void testConcurrentPutMetricWithDifferentKey() throws InterruptedException {
1414
MetricDirective metricDirective = new MetricDirective();
1515
Thread[] threads = new Thread[100];
16-
long targetTimestampToRun = System.currentTimeMillis() + 500; // all threads should target running on this timestamp
16+
long targetTimestampToRun =
17+
System.currentTimeMillis()
18+
+ 500; // all threads should target running on this timestamp
1719

1820
for (int i = 0; i < 100; i++) {
1921
final int id = i;
2022
threads[i] =
2123
new Thread(
2224
() -> {
2325
try {
24-
Thread.sleep(targetTimestampToRun - System.currentTimeMillis()); // try to make all threads run at same time
26+
Thread.sleep(
27+
targetTimestampToRun
28+
- System.currentTimeMillis()); // try to make
29+
// all threads
30+
// run at same
31+
// time
2532
for (int j = 0; j < 1000; j++) {
2633
int metricId = 1000 * id + j;
2734
metricDirective.putMetric("Metric-" + metricId, metricId);

0 commit comments

Comments
 (0)