|
| 1 | +/* |
| 2 | + * Copyright 2024 VMware, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.dynatrace.types; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.Meter; |
| 19 | +import io.micrometer.core.instrument.MockClock; |
| 20 | +import io.micrometer.core.instrument.Tags; |
| 21 | +import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; |
| 22 | +import org.assertj.core.data.Offset; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.concurrent.CountDownLatch; |
| 27 | +import java.util.concurrent.ExecutorService; |
| 28 | +import java.util.concurrent.Executors; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link DynatraceLongTaskTimer}. |
| 35 | + */ |
| 36 | +class DynatraceLongTaskTimerTest { |
| 37 | + |
| 38 | + private static final Offset<Double> OFFSET = Offset.offset(0.0001); |
| 39 | + |
| 40 | + private static final Meter.Id ID = new Meter.Id("test.id", Tags.empty(), "1", "desc", |
| 41 | + Meter.Type.DISTRIBUTION_SUMMARY); |
| 42 | + |
| 43 | + private static final DistributionStatisticConfig DISTRIBUTION_STATISTIC_CONFIG = DistributionStatisticConfig.NONE; |
| 44 | + |
| 45 | + private static final MockClock CLOCK = new MockClock(); |
| 46 | + |
| 47 | + private static final Offset<Double> TOLERANCE = Offset.offset(0.000001); |
| 48 | + |
| 49 | + @Test |
| 50 | + void singleTaskValuesAreRecorded() throws InterruptedException { |
| 51 | + DynatraceLongTaskTimer ltt = new DynatraceLongTaskTimer(ID, CLOCK, TimeUnit.MILLISECONDS, |
| 52 | + DISTRIBUTION_STATISTIC_CONFIG, false); |
| 53 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 54 | + |
| 55 | + CountDownLatch taskHasBeenRunningLatch = new CountDownLatch(1); |
| 56 | + CountDownLatch stopLatch = new CountDownLatch(1); |
| 57 | + |
| 58 | + executorService.submit(() -> ltt.record(() -> { |
| 59 | + CLOCK.add(Duration.ofMillis(100)); |
| 60 | + taskHasBeenRunningLatch.countDown(); |
| 61 | + |
| 62 | + try { |
| 63 | + // wait until the snapshot has been taken |
| 64 | + assertThat(stopLatch.await(300, TimeUnit.MILLISECONDS)).isTrue(); |
| 65 | + } |
| 66 | + catch (InterruptedException e) { |
| 67 | + throw new RuntimeException(e); |
| 68 | + } |
| 69 | + })); |
| 70 | + |
| 71 | + assertThat(taskHasBeenRunningLatch.await(300, TimeUnit.MILLISECONDS)).isTrue(); |
| 72 | + |
| 73 | + DynatraceSummarySnapshot snapshot = ltt.takeSummarySnapshot(); |
| 74 | + // can release the background task |
| 75 | + stopLatch.countDown(); |
| 76 | + |
| 77 | + assertThat(snapshot.getMin()).isCloseTo(100, TOLERANCE); |
| 78 | + assertThat(snapshot.getMax()).isCloseTo(100, TOLERANCE); |
| 79 | + assertThat(snapshot.getCount()).isEqualTo(1); |
| 80 | + // in the case of count == 1, the total has to be equal to min and max |
| 81 | + assertThat(snapshot.getTotal()).isGreaterThan(0) |
| 82 | + .isCloseTo(snapshot.getMin(), TOLERANCE) |
| 83 | + .isCloseTo(snapshot.getMax(), TOLERANCE); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void parallelTasksValuesAreRecorded() throws InterruptedException { |
| 88 | + DynatraceLongTaskTimer ltt = new DynatraceLongTaskTimer(ID, CLOCK, TimeUnit.MILLISECONDS, |
| 89 | + DISTRIBUTION_STATISTIC_CONFIG, false); |
| 90 | + ExecutorService executorService = Executors.newFixedThreadPool(2); |
| 91 | + |
| 92 | + CountDownLatch firstTaskHasBeenRunning = new CountDownLatch(1); |
| 93 | + // both tasks need to be running for a while before we take the snapshot |
| 94 | + CountDownLatch bothTasksHaveBeenRunningLatch = new CountDownLatch(2); |
| 95 | + CountDownLatch stopLatch = new CountDownLatch(1); |
| 96 | + |
| 97 | + // Task 1 |
| 98 | + executorService.submit(() -> ltt.record(() -> { |
| 99 | + try { |
| 100 | + CLOCK.add(Duration.ofMillis(40)); |
| 101 | + |
| 102 | + // task 1 starts first, runs for 40ms (see CLOCK.add(Duration) above), |
| 103 | + // then the second task starts. The second task can start after this |
| 104 | + // latch has counted down (unblocked) the other thread. |
| 105 | + firstTaskHasBeenRunning.countDown(); |
| 106 | + bothTasksHaveBeenRunningLatch.countDown(); |
| 107 | + |
| 108 | + // wait until the snapshot has been taken |
| 109 | + assertThat(stopLatch.await(300, TimeUnit.MILLISECONDS)).isTrue(); |
| 110 | + } |
| 111 | + catch (InterruptedException e) { |
| 112 | + throw new RuntimeException(e); |
| 113 | + } |
| 114 | + })); |
| 115 | + |
| 116 | + // Task 1 (see above) has been running for 40ms, and is still running now (until |
| 117 | + // stopLatch is unblocked). |
| 118 | + assertThat(firstTaskHasBeenRunning.await(300, TimeUnit.MILLISECONDS)).isTrue(); |
| 119 | + |
| 120 | + // Task 2 |
| 121 | + executorService.submit(() -> ltt.record(() -> { |
| 122 | + try { |
| 123 | + // At this point both tasks are running. |
| 124 | + // Adding to the clock here means that both tasks are running at the |
| 125 | + // same time and adding 30ms adds 30ms to both running task. |
| 126 | + CLOCK.add(Duration.ofMillis(30)); |
| 127 | + |
| 128 | + // Release the latch: this means that both tasks |
| 129 | + // have been running and the time has been added to the clock. Both |
| 130 | + // tasks will (conceptually) continue to run until the stopLatch is |
| 131 | + // unblocked. |
| 132 | + bothTasksHaveBeenRunningLatch.countDown(); |
| 133 | + |
| 134 | + // wait until the snapshot has been taken |
| 135 | + assertThat(stopLatch.await(300, TimeUnit.MILLISECONDS)).isTrue(); |
| 136 | + } |
| 137 | + catch (InterruptedException e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
| 140 | + })); |
| 141 | + |
| 142 | + // both tasks have been running for different lengths of time (task 1 for a total |
| 143 | + // of 70ms (40+30ms), and task 2 for a total of 30ms). |
| 144 | + assertThat(bothTasksHaveBeenRunningLatch.await(300, TimeUnit.MILLISECONDS)).isTrue(); |
| 145 | + |
| 146 | + // take a snapshot of the state where both tasks are running for different lengths |
| 147 | + // of time. |
| 148 | + DynatraceSummarySnapshot snapshot = ltt.takeSummarySnapshot(); |
| 149 | + |
| 150 | + // the two running tasks are now allowed to exit. |
| 151 | + stopLatch.countDown(); |
| 152 | + |
| 153 | + // Task 1 has been "running" for 70ms at the time of recording and will |
| 154 | + // supply the max |
| 155 | + assertThat(snapshot.getMax()).isCloseTo(70, OFFSET); |
| 156 | + // Task 2 has been "running" for only 30ms at the time of recording and |
| 157 | + // will supply the min |
| 158 | + assertThat(snapshot.getMin()).isCloseTo(30, OFFSET); |
| 159 | + // Both tasks have been running in parallel. |
| 160 | + // After the second CLOCK.add(Duration) is called, the first task has been running |
| 161 | + // for 70ms, and the second task has been running for 30ms |
| 162 | + // together, they have been running for 100ms in total. |
| 163 | + assertThat(snapshot.getTotal()).isCloseTo(100, OFFSET); |
| 164 | + // Two tasks were running in parallel. |
| 165 | + assertThat(snapshot.getCount()).isEqualTo(2); |
| 166 | + // On the clock, 70ms have passed. MockClock starts at 1, that's why the result |
| 167 | + // here |
| 168 | + // is 71 instead of 70. |
| 169 | + assertThat(CLOCK.wallTime()).isEqualTo(71); |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments