PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-profiling/src/test/java/com/atlassian/util/profiling/MetricsBuilderTest.java

https://bitbucket.org/atlassian/atlassian-profiling
Java | 331 lines | 258 code | 72 blank | 1 comment | 0 complexity | abaf7333a98c89d9e1621874cb91d519 MD5 | raw file
  1. package com.atlassian.util.profiling;
  2. import com.atlassian.plugin.util.PluginKeyStack;
  3. import com.atlassian.util.profiling.strategy.MetricStrategy;
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.junit.BeforeClass;
  7. import org.junit.Rule;
  8. import org.junit.Test;
  9. import org.mockito.ArgumentCaptor;
  10. import org.mockito.Captor;
  11. import org.mockito.Mock;
  12. import org.mockito.Mockito;
  13. import org.mockito.junit.MockitoJUnit;
  14. import org.mockito.junit.MockitoRule;
  15. import java.util.List;
  16. import java.util.stream.Collectors;
  17. import static com.atlassian.util.profiling.MetricTag.FROM_PLUGIN_KEY_TAG_KEY;
  18. import static com.atlassian.util.profiling.MetricTag.INVOKER_PLUGIN_KEY_TAG_KEY;
  19. import static java.util.Arrays.asList;
  20. import static java.util.Collections.singleton;
  21. import static org.hamcrest.MatcherAssert.assertThat;
  22. import static org.hamcrest.Matchers.equalTo;
  23. import static org.hamcrest.Matchers.hasItem;
  24. import static org.hamcrest.Matchers.hasItems;
  25. import static org.hamcrest.Matchers.not;
  26. import static org.mockito.ArgumentMatchers.any;
  27. import static org.mockito.Mockito.never;
  28. public class MetricsBuilderTest {
  29. @Rule
  30. public MockitoRule initRule = MockitoJUnit.rule();
  31. @Captor
  32. private ArgumentCaptor<MetricKey> metricCaptor;
  33. @Mock
  34. private static MetricStrategy metricStrategy;
  35. @BeforeClass
  36. public static void setupSuite() {
  37. Metrics.getConfiguration().setEnabled(true);
  38. }
  39. @Before
  40. public void setup() {
  41. StrategiesRegistry.addMetricStrategy(metricStrategy);
  42. }
  43. @After
  44. public void teardown() {
  45. StrategiesRegistry.removeMetricStrategy(metricStrategy);
  46. }
  47. @Test
  48. public void testMetricName() {
  49. final String metricName = "metricName";
  50. Metrics
  51. .metric(metricName) // method under test
  52. .startTimer().close();
  53. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  54. assertThat(metricCaptor.getValue().getMetricName(), equalTo(metricName));
  55. }
  56. @Test
  57. public void testAddingASingleTag() {
  58. final String tagKey = "theTagKey";
  59. final String tagValue = "theTagValue";
  60. Metrics.metric("ignored")
  61. .tag(tagKey, tagValue) // method under test
  62. .startTimer().close();
  63. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  64. assertThat(metricCaptor.getValue().getTags(), hasItem(MetricTag.of(tagKey, tagValue)));
  65. }
  66. @Test
  67. public void testAddingVarArgTags() {
  68. final MetricTag[] metricTags = {MetricTag.of("key1", "val1"), MetricTag.of("key2", "val2")};
  69. Metrics.metric("ignored")
  70. .tags(metricTags) // method under test
  71. .startTimer().close();
  72. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  73. assertThat(metricCaptor.getValue().getTags(), hasItems(metricTags));
  74. }
  75. @Test
  76. public void testAddingCollectionOfTags() {
  77. final MetricTag[] metricTags = {MetricTag.of("key1", "val1"), MetricTag.of("key2", "val2")};
  78. Metrics.metric("ignored")
  79. .tags(asList(metricTags)) // method under test
  80. .startTimer().close();
  81. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  82. assertThat(metricCaptor.getValue().getTags(), hasItems(metricTags));
  83. }
  84. @Test
  85. public void testAddingTagsInEveryWay() {
  86. final String tagKey = "theTagKey";
  87. final String tagValue = "theTagValue";
  88. final MetricTag[] varArgsMetricTags =
  89. {MetricTag.of("varArgKey1", "varArgVal1"), MetricTag.of("varArgKey2", "varArgVal2")};
  90. final MetricTag[] iterableMetricTags =
  91. {MetricTag.of("iterableKey1", "iterableVal1"), MetricTag.of("iterableKey2", "iterableVal2")};
  92. Metrics.metric("ignored")
  93. // Methods under test
  94. .tag(tagKey, tagValue)
  95. .tags(varArgsMetricTags)
  96. .tags(asList(iterableMetricTags))
  97. .startTimer().close();
  98. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  99. assertThat(metricCaptor.getValue().getTags(), hasItem(MetricTag.of(tagKey, tagValue)));
  100. assertThat(metricCaptor.getValue().getTags(), hasItems(varArgsMetricTags));
  101. assertThat(metricCaptor.getValue().getTags(), hasItems(iterableMetricTags));
  102. }
  103. @Test
  104. public void testWithAnalytics() {
  105. Metrics.metric("ignored")
  106. .withAnalytics() // method under test
  107. .startTimer().close();
  108. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  109. assertThat(metricCaptor.getValue().getTags(), hasItem(MetricTag.SEND_ANALYTICS));
  110. }
  111. @Test
  112. public void testFromPluginKey() {
  113. final String pluginKey = "pluginKey";
  114. Metrics.metric("ignored")
  115. .fromPluginKey(pluginKey) // method under test
  116. .startTimer().close();
  117. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  118. assertThat(metricCaptor.getValue().getTags(),
  119. hasItem(MetricTag.of(FROM_PLUGIN_KEY_TAG_KEY, pluginKey)));
  120. }
  121. @Test
  122. public void testFromPluginKey_should_onlySetTheTagOnce() {
  123. final String firstPluginKey = "firstPluginKey";
  124. final String secondPluginKey = "secondPluginKey";
  125. Metrics.metric("ignored")
  126. .fromPluginKey(firstPluginKey) // method under test
  127. .fromPluginKey(secondPluginKey) // method under test
  128. .startTimer().close();
  129. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  130. assertThat(metricCaptor.getValue().getTags(),
  131. hasItem(MetricTag.of(FROM_PLUGIN_KEY_TAG_KEY, secondPluginKey)));
  132. assertThat(metricCaptor.getValue().getTags(),
  133. not(hasItem(MetricTag.of(FROM_PLUGIN_KEY_TAG_KEY, firstPluginKey))));
  134. }
  135. @Test
  136. public void testFromPluginKey_should_notAddANullPluginKey() {
  137. final String nullPluginKey = null;
  138. Metrics.metric("ignored")
  139. .fromPluginKey(nullPluginKey) // method under test
  140. .startTimer().close();
  141. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  142. final List<String> tagKeys =
  143. metricCaptor.getValue().getTags().stream().map(MetricTag::getKey).collect(Collectors.toList());
  144. assertThat(tagKeys, not(hasItem(FROM_PLUGIN_KEY_TAG_KEY)));
  145. }
  146. @Test
  147. public void testInvokerPluginKey() {
  148. final String pluginKey = "pluginKey";
  149. Metrics.metric("ignored")
  150. .invokerPluginKey(pluginKey) // method under test
  151. .startTimer().close();
  152. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  153. assertThat(metricCaptor.getValue().getTags(),
  154. hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, pluginKey)));
  155. }
  156. @Test
  157. public void testInvokerPluginKey_should_onlySetTheTagOnce() {
  158. final String firstPluginKey = "firstPluginKey";
  159. final String secondPluginKey = "secondPluginKey";
  160. Metrics.metric("ignored")
  161. .invokerPluginKey(firstPluginKey) // method under test
  162. .invokerPluginKey(secondPluginKey) // method under test
  163. .startTimer().close();
  164. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  165. assertThat(metricCaptor.getValue().getTags(),
  166. hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, secondPluginKey)));
  167. assertThat(metricCaptor.getValue().getTags(),
  168. not(hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, firstPluginKey))));
  169. }
  170. @Test
  171. public void testInvokerPluginKey_should_notAddANullPluginKey() {
  172. final String nullPluginKey = null;
  173. Metrics.metric("ignored")
  174. .invokerPluginKey(nullPluginKey) // method under test
  175. .startTimer().close();
  176. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  177. final List<String> tagKeys =
  178. metricCaptor.getValue().getTags().stream().map(MetricTag::getKey).collect(Collectors.toList());
  179. assertThat(tagKeys, not(hasItem(INVOKER_PLUGIN_KEY_TAG_KEY)));
  180. }
  181. @Test
  182. public void testWithInvokerPluginKey() {
  183. final String providedPluginKey = "providedPluginKey";
  184. PluginKeyStack.push(providedPluginKey);
  185. Metrics.metric("ignored")
  186. .withInvokerPluginKey() // method under test
  187. .startTimer().close();
  188. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  189. assertThat(metricCaptor.getValue().getTags(),
  190. hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, providedPluginKey)));
  191. }
  192. @Test
  193. public void testStartTimer() {
  194. final String trackedMetricName = "uniqueRegularTimerMetricName";
  195. Metrics.metric(trackedMetricName)
  196. .startTimer() // method under test
  197. .close();
  198. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  199. assertThat(metricCaptor.getValue().getMetricName(), equalTo(trackedMetricName));
  200. }
  201. @Test
  202. public void testStartLongRunningTimer() {
  203. final String trackedMetricName = "uniqueLongRunningTimerMetricName";
  204. Metrics.metric(trackedMetricName)
  205. .startLongRunningTimer() // method under test
  206. .close();
  207. Mockito.verify(metricStrategy).startLongRunningTimer(metricCaptor.capture());
  208. assertThat(metricCaptor.getValue().getMetricName(), equalTo(trackedMetricName));
  209. }
  210. @Test
  211. public void testIncrementCounter() {
  212. final MetricKey metricKey = MetricKey.metricKey("counter", MetricTag.of("tagKey", "tagValue"));
  213. final long trackedUpdateValue = 363;
  214. Metrics
  215. .metric(metricKey.getMetricName())
  216. .tags(metricKey.getTags())
  217. .incrementCounter(trackedUpdateValue);
  218. final ArgumentCaptor<Long> updateValueCaptor = ArgumentCaptor.forClass(Long.class);
  219. Mockito.verify(metricStrategy).incrementCounter(metricCaptor.capture(), updateValueCaptor.capture());
  220. assertThat(metricCaptor.getValue(), equalTo(metricKey));
  221. assertThat(updateValueCaptor.getValue(), equalTo(trackedUpdateValue));
  222. }
  223. @Test(expected = IllegalArgumentException.class)
  224. public void testIncrementCounter_shouldNotAcceptNegativeValues() {
  225. final long negativeNumber = -1L;
  226. Metrics
  227. .metric("ignored")
  228. .incrementCounter(negativeNumber);
  229. Mockito.verify(metricStrategy, never()).incrementCounter(any(), any());
  230. }
  231. @Test
  232. public void testHistogramUpdate() {
  233. final String trackedMetricName = "uniqueLongRunningTimerMetricName";
  234. final long trackedUpdateValue = 77777;
  235. Metrics.metric(trackedMetricName)
  236. .histogram() // method under test
  237. .update(trackedUpdateValue);
  238. ArgumentCaptor<Long> histogramValueCaptor = ArgumentCaptor.forClass(Long.class);
  239. Mockito.verify(metricStrategy).updateHistogram(metricCaptor.capture(), histogramValueCaptor.capture());
  240. assertThat(metricCaptor.getValue().getMetricName(), equalTo(trackedMetricName));
  241. assertThat(histogramValueCaptor.getValue(), equalTo(trackedUpdateValue));
  242. }
  243. @Test
  244. public void testNullTagValues_shouldShowInOutput() {
  245. final String tag1Key = "tag1";
  246. final String tag2Key = "tag2";
  247. final String tag3Key = "tag3";
  248. final String[] expectedTagKeys = {tag1Key, tag2Key, tag3Key};
  249. Metrics.metric("irrelevant")
  250. .tag(tag1Key, null)
  251. .tags(MetricTag.of(tag2Key, null))
  252. .tags(singleton(MetricTag.of(tag3Key, null)))
  253. .timer().start().close();
  254. Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
  255. final List<String> capturedMetricTags = metricCaptor.getValue().getTags().stream()
  256. .map(MetricTag::getKey).collect(Collectors.toList());
  257. assertThat(capturedMetricTags, hasItems(expectedTagKeys));
  258. }
  259. }