From f83448caa54ad0130e9bef31d2b2509b68c84739 Mon Sep 17 00:00:00 2001 From: Siddhant Deshmukh Date: Mon, 5 Aug 2024 15:21:06 -0700 Subject: [PATCH] Refactor unit tests Signed-off-by: Siddhant Deshmukh --- .../insights/QueryInsightsTestUtils.java | 18 + .../service/QueryGroupingServiceTests.java | 548 ++++-------------- 2 files changed, 122 insertions(+), 444 deletions(-) diff --git a/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java b/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java index 6cae81e..c17b2ef 100644 --- a/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java +++ b/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java @@ -178,6 +178,24 @@ public static List generateQueryInsightsRecordsWithMeasuremen return records; } + public static List> generateMultipleQueryInsightsRecordsWithMeasurement( + int count, + MetricType metricType, + List measurements + ) { + List> multipleRecordLists = new ArrayList<>(); + + for (int i = 0; i < measurements.size(); i++) { + List records = generateQueryInsightRecords(count); + multipleRecordLists.add(records); + for (SearchQueryRecord record : records) { + record.getMeasurements().get(metricType).setMeasurement(measurements.get(i)); + } + QueryInsightsTestUtils.populateHashcode(records, i); + } + return multipleRecordLists; + } + public static void populateSameQueryHashcodes(List searchQueryRecords) { for (SearchQueryRecord record : searchQueryRecords) { record.getAttributes().put(Attribute.QUERY_HASHCODE, 1); diff --git a/src/test/java/org/opensearch/plugin/insights/core/service/QueryGroupingServiceTests.java b/src/test/java/org/opensearch/plugin/insights/core/service/QueryGroupingServiceTests.java index 34ea56e..a68fa98 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/service/QueryGroupingServiceTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/service/QueryGroupingServiceTests.java @@ -8,7 +8,6 @@ package org.opensearch.plugin.insights.core.service; -import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; @@ -34,13 +33,7 @@ public class QueryGroupingServiceTests extends OpenSearchTestCase { @Before public void setup() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.DEFUALT_AGGREGATION_TYPE, - topQueriesStore, - 10 - ); + queryGroupingService = getQueryGroupingService(AggregationType.DEFUALT_AGGREGATION_TYPE, 10); } public void testWithAllDifferentHashcodes() { @@ -210,13 +203,7 @@ public void testVaryingTopNSize() { } public void testAddMeasurementSumAggregationLatency() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.SUM, - topQueriesStore, - 10 - ); + queryGroupingService = getQueryGroupingService(AggregationType.SUM, 10); int numOfRecords = 10; List records = QueryInsightsTestUtils.generateQueryInsightRecords(numOfRecords, AggregationType.NONE); @@ -234,13 +221,7 @@ public void testAddMeasurementSumAggregationLatency() { } public void testAddMeasurementAverageAggregationLatency() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 10 - ); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 10); int numOfRecords = 10; List records = QueryInsightsTestUtils.generateQueryInsightRecords(numOfRecords, AggregationType.NONE); @@ -261,13 +242,7 @@ public void testAddMeasurementAverageAggregationLatency() { } public void testAddMeasurementNoneAggregationLatency() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.NONE, - topQueriesStore, - 10 - ); + queryGroupingService = getQueryGroupingService(AggregationType.NONE, 10); int numOfRecords = 10; List records = QueryInsightsTestUtils.generateQueryInsightRecords(numOfRecords, AggregationType.NONE); @@ -365,37 +340,17 @@ public void testNoneGroupingTypeIllegalArgumentException() { // 1. New query group not existing added to MIN public void testNewGroupAddedToMin() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 2; - - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1000 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 2, MetricType.LATENCY, - 1100 + List.of(1000, 1100) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); - - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2); for (List recordList : allRecords) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -406,45 +361,17 @@ public void testNewGroupAddedToMin() { // 2. New query group not existing added to MIN and overflows to MAX public void testNewGroupOverflowsMinToMax() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 2; - - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1000 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1100 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 2, MetricType.LATENCY, - 900 + List.of(1000, 1100, 900) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3); for (List recordList : allRecords) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -455,45 +382,17 @@ public void testNewGroupOverflowsMinToMax() { // 3. New query group not existing added to MIN and causes other group to overflow to MAX public void testNewGroupCausesOtherGroupOverflowMinToMax() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 2; + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 2, MetricType.LATENCY, - 1000 + List.of(1000, 1100, 1200) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1100 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); - - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1200 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3); for (List recordList : allRecords) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -504,54 +403,25 @@ public void testNewGroupCausesOtherGroupOverflowMinToMax() { // 4. Existing query group update to MIN increases average public void testExistingGroupUpdateToMinIncreaseAverage() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 1000 + List.of(1100, 1200, 1000) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 1200 + List.of(1300) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1100 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); + allRecords1.addAll(allRecords2); - // Average will decrease when this record added and record should be moved out of Top N - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1300 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); - - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4); - - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -562,54 +432,25 @@ public void testExistingGroupUpdateToMinIncreaseAverage() { // 5. Existing query group update to MIN decrease average - stay in MIN public void testExistingGroupUpdateToMinDecreaseAverageStayInMin() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; - - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1000 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 600 + List.of(1100, 600, 1000) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 1100 + List.of(700) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - - // Average will decrease when this record added and record should be moved out of Top N - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 700 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4); + allRecords1.addAll(allRecords2); - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -620,54 +461,25 @@ public void testExistingGroupUpdateToMinDecreaseAverageStayInMin() { // 6. Existing query group update to MIN decrease average - overflows to MAX public void testExistingGroupUpdateToMinDecreaseAverageOverflowsToMax() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 1000 + List.of(1199, 1100, 1000) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 1100 + List.of(1) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1199 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); + allRecords1.addAll(allRecords2); - // Average will decrease when this record added and record should be moved out of Top N - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); - - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4); - - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -678,53 +490,25 @@ public void testExistingGroupUpdateToMinDecreaseAverageOverflowsToMax() { // 7. Existing query group update to MAX increases average - stay in MAX public void testExistingGroupUpdateToMaxIncreaseAverageStayInMax() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; - - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 950 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 975 + List.of(900, 975, 950) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 900 + List.of(920) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 920 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); + allRecords1.addAll(allRecords2); - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4); - - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -735,53 +519,25 @@ public void testExistingGroupUpdateToMaxIncreaseAverageStayInMax() { // 8. Existing query group update to MAX increases average - promote to MIN public void testExistingGroupUpdateToMaxIncreaseAveragePromoteToMin() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; - - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 950 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 975 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 900 + List.of(900, 975, 950) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 1100 + List.of(1100) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4); + allRecords1.addAll(allRecords2); - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -792,53 +548,25 @@ public void testExistingGroupUpdateToMaxIncreaseAveragePromoteToMin() { // 9. Existing query group update to MAX decrease average public void testExistingGroupUpdateToMaxDecreaseAverage() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 950 + List.of(900, 975, 950) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 975 + List.of(800) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 900 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 800 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); + allRecords1.addAll(allRecords2); - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4); - - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -848,53 +576,25 @@ public void testExistingGroupUpdateToMaxDecreaseAverage() { } public void testSwitchGroupingTypeToNone() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; - - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 950 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 975 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 900 + List.of(900, 975, 950) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 800 + List.of(800) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4); + allRecords1.addAll(allRecords2); - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -905,73 +605,29 @@ public void testSwitchGroupingTypeToNone() { queryGroupingService.setGroupingType(GroupingType.NONE); assertEquals(0, queryGroupingService.numberOfTopGroups()); - assertThrows(IllegalArgumentException.class, () -> { queryGroupingService.addQueryToGroup(allRecords.get(0).get(0)); }); + assertThrows(IllegalArgumentException.class, () -> { queryGroupingService.addQueryToGroup(allRecords1.get(0).get(0)); }); } public void testMultipleQueryGroupsUpdates() { - queryGroupingService = new QueryGroupingService( - MetricType.LATENCY, - GroupingType.SIMILARITY, - AggregationType.AVERAGE, - topQueriesStore, - 2 - ); - int numOfRecords = 1; - - List records1 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1000 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records1, 1); - - List records2 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 1000 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records2, 2); + queryGroupingService = getQueryGroupingService(AggregationType.AVERAGE, 2); - List records3 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords1 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 900 + List.of(900, 1000, 1000) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records3, 3); - - List records4 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 800 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records4, 3); - - List records5 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, - MetricType.LATENCY, - 400 - ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records5, 2); - List records6 = QueryInsightsTestUtils.generateQueryInsightsRecordsWithMeasurement( - numOfRecords, + List> allRecords2 = QueryInsightsTestUtils.generateMultipleQueryInsightsRecordsWithMeasurement( + 1, MetricType.LATENCY, - 1200 + List.of(800, 400, 1200) ); - // Set all records to have the same hashcode for aggregation - QueryInsightsTestUtils.populateHashcode(records6, 1); - SearchQueryRecord aggregatedRecord = null; - List> allRecords = Arrays.asList(records1, records2, records3, records4, records5, records6); + allRecords1.addAll(allRecords2); - for (List recordList : allRecords) { + for (List recordList : allRecords1) { for (SearchQueryRecord record : recordList) { - aggregatedRecord = queryGroupingService.addQueryToGroup(record); + queryGroupingService.addQueryToGroup(record); } } @@ -979,4 +635,8 @@ public void testMultipleQueryGroupsUpdates() { assertEquals(850L, topQueriesStore.poll().getMeasurement(MetricType.LATENCY)); assertEquals(1100L, topQueriesStore.poll().getMeasurement(MetricType.LATENCY)); } + + private QueryGroupingService getQueryGroupingService(AggregationType aggregationType, int topNSize) { + return new QueryGroupingService(MetricType.LATENCY, GroupingType.SIMILARITY, aggregationType, topQueriesStore, topNSize); + } }