4141import io .opentelemetry .sdk .metrics .internal .data .ImmutableMetricData ;
4242import io .opentelemetry .sdk .metrics .internal .data .ImmutableSumData ;
4343import io .opentelemetry .sdk .testing .exporter .InMemoryMetricReader ;
44- import java .time .Duration ;
4544import java .util .ArrayList ;
4645import java .util .Arrays ;
4746import java .util .Collections ;
6564class SynchronousInstrumentStressTest {
6665
6766 private static final String INSTRUMENT_NAME = "instrument" ;
68- private static final Duration ONE_MICROSECOND = Duration .ofNanos (1000 );
6967 private static final List <Double > BUCKET_BOUNDARIES =
7068 ExplicitBucketHistogramUtils .DEFAULT_HISTOGRAM_BUCKET_BOUNDARIES ;
7169 private static final double [] BUCKET_BOUNDARIES_ARR =
@@ -93,6 +91,7 @@ void stressTest(
9391 }
9492 }
9593
94+ @ SuppressWarnings ("ThreadPriorityCheck" )
9695 private void stressTestOnce (
9796 AggregationTemporality aggregationTemporality ,
9897 InstrumentType instrumentType ,
@@ -130,15 +129,17 @@ private void stressTestOnce(
130129 int threadCount = 4 ;
131130 List <Thread > recordThreads = new ArrayList <>();
132131 CountDownLatch latch = new CountDownLatch (threadCount );
132+ CountDownLatch startSignal = new CountDownLatch (1 );
133133 for (int i = 0 ; i < threadCount ; i ++) {
134134 recordThreads .add (
135135 new Thread (
136136 () -> {
137+ Uninterruptibles .awaitUninterruptibly (startSignal );
137138 for (Long measurement : measurements ) {
138139 for (Attributes attr : attributes ) {
139140 instrument .record (measurement , attr );
140141 }
141- Uninterruptibles . sleepUninterruptibly ( ONE_MICROSECOND );
142+ Thread . yield ( );
142143 }
143144 latch .countDown ();
144145 }));
@@ -150,8 +151,9 @@ private void stressTestOnce(
150151 Thread collectThread =
151152 new Thread (
152153 () -> {
154+ Uninterruptibles .awaitUninterruptibly (startSignal );
153155 while (latch .getCount () != 0 ) {
154- Uninterruptibles . sleepUninterruptibly ( ONE_MICROSECOND );
156+ Thread . yield ( );
155157 collectedMetrics .addAll (
156158 reader .collectAllMetrics ().stream ()
157159 .map (SynchronousInstrumentStressTest ::copy )
@@ -163,9 +165,10 @@ private void stressTestOnce(
163165 .collect (toList ()));
164166 });
165167
166- // Start all the threads
168+ // Start all the threads, then release the start signal so they begin simultaneously
167169 collectThread .start ();
168170 recordThreads .forEach (Thread ::start );
171+ startSignal .countDown ();
169172
170173 // Wait for the collect thread to end, which collects until the record threads are done
171174 Uninterruptibles .joinUninterruptibly (collectThread );
0 commit comments