From dc065c0bb2a88281c4c9b58a102cbd9c2f92c218 Mon Sep 17 00:00:00 2001 From: Austin Seto Date: Thu, 25 Jun 2020 10:17:09 -0400 Subject: [PATCH 1/6] Count metric test validates increase in metric rather than it being >= 1 --- README.adoc | 6 +++--- .../it/io/openliberty/guides/metrics/MetricsIT.java | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.adoc b/README.adoc index 9cf0712..0553670 100644 --- a/README.adoc +++ b/README.adoc @@ -315,9 +315,9 @@ the `localhost` host to the inventory. Next, the test case makes a connection to Then, it asserts whether the time that is needed to retrieve the system properties for localhost is less than 4 seconds. -* The [hotspot=testInventoryAccessCountMetric file=0]`testInventoryAccessCountMetric()` test case validates the [hotspot=countedForList file=1]`@Counted` metric. The test case sends a request to the -`\http://localhost:9080/inventory/systems` URL to retrieve the whole inventory, and then it asserts -that this endpoint is accessed at least once. +* The [hotspot=testInventoryAccessCountMetric file=0]`testInventoryAccessCountMetric()` test case validates the [hotspot=countedForList file=1]`@Counted` metric. +The test case obtains the metric before and after a request to the `\http://localhost:9080/inventory/systems` URL. +It asserts that the metric was increased after the access. * The [hotspot=testInventorySizeGaugeMetric file=0]`testInventorySizeGaugeMetric()` test case validates the [hotspot=gaugeForGetTotal file=1]`@Gauge` metric. The test case first ensures that the localhost is in the inventory, then looks for the [hotspot=gaugeForGetTotal file=1]`@Gauge` metric and asserts diff --git a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java index 3c48e21..71bdc89 100644 --- a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java +++ b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java @@ -106,14 +106,22 @@ public void testPropertiesRequestTimeMetric() { // end::Order2[] // tag::testInventoryAccessCountMetric[] public void testInventoryAccessCountMetric() { + metrics = getMetrics(); + int accessCountBefore = 0; + int accessCountAfter = 0; + for (String metric : metrics) { + if (metric.startsWith("application_inventoryAccessCount_total")) { + accessCountBefore = Integer.parseInt(metric.split(" ")[metric.split(" ").length - 1]); + } + } connectToEndpoint(baseHttpUrl + INVENTORY_HOSTS); metrics = getMetrics(); for (String metric : metrics) { if (metric.startsWith("application_inventoryAccessCount_total")) { - assertTrue( - 1 <= Integer.parseInt(metric.split(" ")[metric.split(" ").length - 1])); + accessCountAfter = Integer.parseInt(metric.split(" ")[metric.split(" ").length - 1]); } } + assertTrue(accessCountAfter > accessCountBefore); } // end::testInventoryAccessCountMetric[] From b3a370ec944514f08f9e2767b11c8dc160ea06b7 Mon Sep 17 00:00:00 2001 From: Austin Seto Date: Thu, 25 Jun 2020 10:56:49 -0400 Subject: [PATCH 2/6] Code formatting --- .../openliberty/guides/metrics/MetricsIT.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java index 71bdc89..c81e0fa 100644 --- a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java +++ b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java @@ -107,20 +107,13 @@ public void testPropertiesRequestTimeMetric() { // tag::testInventoryAccessCountMetric[] public void testInventoryAccessCountMetric() { metrics = getMetrics(); - int accessCountBefore = 0; + int accessCountBefore = getIntMetric(metrics, + "application_inventoryAccessCount_total"); int accessCountAfter = 0; - for (String metric : metrics) { - if (metric.startsWith("application_inventoryAccessCount_total")) { - accessCountBefore = Integer.parseInt(metric.split(" ")[metric.split(" ").length - 1]); - } - } connectToEndpoint(baseHttpUrl + INVENTORY_HOSTS); metrics = getMetrics(); - for (String metric : metrics) { - if (metric.startsWith("application_inventoryAccessCount_total")) { - accessCountAfter = Integer.parseInt(metric.split(" ")[metric.split(" ").length - 1]); - } - } + accessCountAfter = getIntMetric(metrics, + "application_inventoryAccessCount_total"); assertTrue(accessCountAfter > accessCountBefore); } // end::testInventoryAccessCountMetric[] @@ -134,12 +127,7 @@ public void testInventoryAccessCountMetric() { // tag::testInventorySizeGaugeMetric[] public void testInventorySizeGaugeMetric() { metrics = getMetrics(); - for (String metric : metrics) { - if (metric.startsWith("application_inventorySizeGauge")) { - assertTrue( - 1 <= Character.getNumericValue(metric.charAt(metric.length() - 1))); - } - } + assertTrue(1 <= getIntMetric(metrics, "application_inventorySizeGauge")); } // end::testInventorySizeGaugeMetric[] @@ -206,5 +194,16 @@ private Response getResponse(String url) { private void assertResponse(String url, Response response) { assertEquals(200, response.getStatus(), "Incorrect response code from " + url); } + + private int getIntMetric(List metrics, String metricName) { + int output = -1; + for (String metric : metrics) { + if (metric.startsWith(metricName)) { + String[] mSplit = metric.split(" "); + output = Integer.parseInt(mSplit[mSplit.length - 1]); + } + } + return output; + } } // end::MetricsTest[] From d2701dec0b16a65aaa3f9519080bf64b86308a22 Mon Sep 17 00:00:00 2001 From: Austin Seto Date: Fri, 26 Jun 2020 10:48:04 -0400 Subject: [PATCH 3/6] Restore use of escape character --- .../test/java/it/io/openliberty/guides/metrics/MetricsIT.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java index c81e0fa..8573097 100644 --- a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java +++ b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java @@ -91,7 +91,7 @@ public void testPropertiesRequestTimeMetric() { for (String metric : metrics) { if (metric.startsWith( "application_inventoryProcessingTime_rate_per_second")) { - float seconds = Float.parseFloat(metric.split(" ")[1]); + float seconds = Float.parseFloat(metric.split("\\s+")[1]); assertTrue(4 > seconds); } } @@ -199,7 +199,7 @@ private int getIntMetric(List metrics, String metricName) { int output = -1; for (String metric : metrics) { if (metric.startsWith(metricName)) { - String[] mSplit = metric.split(" "); + String[] mSplit = metric.split("\\s+"); output = Integer.parseInt(mSplit[mSplit.length - 1]); } } From 17e233a8e656e88536b1eaa244963bed6e85e0ae Mon Sep 17 00:00:00 2001 From: Austin Seto Date: Fri, 7 Aug 2020 14:00:12 -0400 Subject: [PATCH 4/6] Requested change to test made --- .../java/it/io/openliberty/guides/metrics/MetricsIT.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java index 8573097..002ea24 100644 --- a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java +++ b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java @@ -109,11 +109,10 @@ public void testInventoryAccessCountMetric() { metrics = getMetrics(); int accessCountBefore = getIntMetric(metrics, "application_inventoryAccessCount_total"); - int accessCountAfter = 0; connectToEndpoint(baseHttpUrl + INVENTORY_HOSTS); metrics = getMetrics(); - accessCountAfter = getIntMetric(metrics, - "application_inventoryAccessCount_total"); + int accessCountAfter = getIntMetric(metrics, + "application_inventoryAccessCount_total"); assertTrue(accessCountAfter > accessCountBefore); } // end::testInventoryAccessCountMetric[] From dabbd42027713d6b5ab4d1335e1645e159ec0a2f Mon Sep 17 00:00:00 2001 From: Austin Seto Date: Fri, 7 Aug 2020 14:39:19 -0400 Subject: [PATCH 5/6] Use map to store all metrics found for comparison instead of just last one --- .../openliberty/guides/metrics/MetricsIT.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java index 002ea24..a53cc41 100644 --- a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java +++ b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java @@ -107,13 +107,17 @@ public void testPropertiesRequestTimeMetric() { // tag::testInventoryAccessCountMetric[] public void testInventoryAccessCountMetric() { metrics = getMetrics(); - int accessCountBefore = getIntMetric(metrics, - "application_inventoryAccessCount_total"); + Map accessCountsBefore = getIntMetrics(metrics, + "application_inventoryAccessCount_total"); connectToEndpoint(baseHttpUrl + INVENTORY_HOSTS); metrics = getMetrics(); - int accessCountAfter = getIntMetric(metrics, - "application_inventoryAccessCount_total"); - assertTrue(accessCountAfter > accessCountBefore); + Map accessCountsAfter = getIntMetrics(metrics, + "application_inventoryAccessCount_total"); + for (String key : accessCountsBefore.keySet()) { + Integer accessCountBefore = accessCountsBefore.get(key); + Integer accessCountAfter = accessCountsAfter.get(key); + assertTrue(accessCountAfter > accessCountBefore); + } } // end::testInventoryAccessCountMetric[] @@ -126,7 +130,11 @@ public void testInventoryAccessCountMetric() { // tag::testInventorySizeGaugeMetric[] public void testInventorySizeGaugeMetric() { metrics = getMetrics(); - assertTrue(1 <= getIntMetric(metrics, "application_inventorySizeGauge")); + Map inventorySizeGauges = getIntMetrics(metrics, + "application_inventorySizeGauge"); + for (Integer value : inventorySizeGauges.values()) { + assertTrue(1 <= value); + } } // end::testInventorySizeGaugeMetric[] @@ -194,12 +202,14 @@ private void assertResponse(String url, Response response) { assertEquals(200, response.getStatus(), "Incorrect response code from " + url); } - private int getIntMetric(List metrics, String metricName) { - int output = -1; + private Map getIntMetrics(List metrics, String metricName) { + Map output = new HashMap(); for (String metric : metrics) { if (metric.startsWith(metricName)) { - String[] mSplit = metric.split("\\s+"); - output = Integer.parseInt(mSplit[mSplit.length - 1]); + String[] mSplit = metric.split("\\s+"); + String key = mSplit[0]; + Integer value = Integer.parseInt(mSplit[mSplit.length - 1]); + output.put(key, value); } } return output; From 2f3cdd99fed2b3230bd0a6738ad0770127e56e87 Mon Sep 17 00:00:00 2001 From: Austin Seto Date: Tue, 8 Dec 2020 09:17:20 -0500 Subject: [PATCH 6/6] Address ID Review --- README.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.adoc b/README.adoc index ab589ab..c8e112c 100644 --- a/README.adoc +++ b/README.adoc @@ -308,8 +308,8 @@ Then, it asserts whether the time that is needed to retrieve the system properties for localhost is less than 4 seconds. * The [hotspot=testInventoryAccessCountMetric file=0]`testInventoryAccessCountMetric()` test case validates the [hotspot=countedForList file=1]`@Counted` metric. -The test case obtains the metric before and after a request to the `\http://localhost:9080/inventory/systems` URL. -It asserts that the metric was increased after the access. +The test case obtains metric data before and after a request to the `\http://localhost:9080/inventory/systems` URL. +It then asserts that the metric was increased after the URL was accessed. * The [hotspot=testInventorySizeGaugeMetric file=0]`testInventorySizeGaugeMetric()` test case validates the [hotspot=gaugeForGetTotal file=1]`@Gauge` metric. The test case first ensures that the localhost is in the inventory, then looks for the [hotspot=gaugeForGetTotal file=1]`@Gauge` metric and asserts