9
9
package org .opensearch .plugin .insights .rules .resthandler .top_queries ;
10
10
11
11
import java .io .IOException ;
12
- import java .nio .charset .StandardCharsets ;
13
12
import java .util .List ;
14
13
import java .util .Map ;
15
14
import org .junit .Assert ;
20
19
import org .opensearch .common .xcontent .json .JsonXContent ;
21
20
import org .opensearch .core .xcontent .NamedXContentRegistry ;
22
21
import org .opensearch .plugin .insights .QueryInsightsRestTestCase ;
23
- import org .opensearch .plugin .insights .settings .QueryInsightsSettings ;
24
22
25
23
/** Rest Action tests for Top Queries */
26
24
public class TopQueriesRestIT extends QueryInsightsRestTestCase {
@@ -52,55 +50,19 @@ public void testTopQueriesResponses() throws IOException, InterruptedException {
52
50
waitForEmptyTopQueriesResponse ();
53
51
54
52
// Enable Top N Queries feature
55
- Request request = new Request ("PUT" , "/_cluster/settings" );
56
- request .setJsonEntity (defaultTopQueriesSettings ());
57
- Response response = client ().performRequest (request );
58
- Assert .assertEquals (200 , response .getStatusLine ().getStatusCode ());
59
- doSearch (2 );
60
- // run five times to make sure the records are drained to the top queries services
61
- for (int i = 0 ; i < 5 ; i ++) {
62
- // Get Top Queries
63
- request = new Request ("GET" , "/_insights/top_queries?pretty" );
64
- response = client ().performRequest (request );
65
- Assert .assertEquals (200 , response .getStatusLine ().getStatusCode ());
66
- String topRequests = new String (response .getEntity ().getContent ().readAllBytes (), StandardCharsets .UTF_8 );
67
- Assert .assertTrue (topRequests .contains ("top_queries" ));
53
+ updateClusterSettings (this ::defaultTopQueriesSettings );
68
54
69
- int topNArraySize = countTopQueries ( topRequests );
55
+ doSearch ( 2 );
70
56
71
- if (topNArraySize == 0 ) {
72
- Thread .sleep (QueryInsightsSettings .QUERY_RECORD_QUEUE_DRAIN_INTERVAL .millis ());
73
- continue ;
74
- }
75
- Assert .assertEquals (2 , topNArraySize );
76
- }
57
+ assertTopQueriesCount (2 , "latency" );
77
58
78
59
// Enable Top N Queries by resource usage
79
- request = new Request ("PUT" , "/_cluster/settings" );
80
- request .setJsonEntity (topQueriesByResourceUsagesSettings ());
81
- response = client ().performRequest (request );
82
- Assert .assertEquals (200 , response .getStatusLine ().getStatusCode ());
60
+ updateClusterSettings (this ::topQueriesByResourceUsagesSettings );
61
+
83
62
// Do Search
84
63
doSearch (2 );
85
64
86
- // Run five times to make sure the records are drained to the top queries services
87
- for (int i = 0 ; i < 5 ; i ++) {
88
- // Get Top Queries
89
- request = new Request ("GET" , "/_insights/top_queries?type=cpu&pretty" );
90
- response = client ().performRequest (request );
91
- Assert .assertEquals (200 , response .getStatusLine ().getStatusCode ());
92
- String topRequests = new String (response .getEntity ().getContent ().readAllBytes (), StandardCharsets .UTF_8 );
93
- Assert .assertTrue (topRequests .contains ("top_queries" ));
94
-
95
- // Use the countTopQueries method to determine the number of top queries
96
- int topNArraySize = countTopQueries (topRequests );
97
-
98
- if (topNArraySize == 0 ) {
99
- Thread .sleep (QueryInsightsSettings .QUERY_RECORD_QUEUE_DRAIN_INTERVAL .millis ());
100
- continue ;
101
- }
102
- Assert .assertEquals (2 , topNArraySize );
103
- }
65
+ assertTopQueriesCount (2 , "cpu" );
104
66
}
105
67
106
68
/**
@@ -125,10 +87,10 @@ private String topQueriesByResourceUsagesSettings() {
125
87
return "{\n "
126
88
+ " \" persistent\" : {\n "
127
89
+ " \" search.insights.top_queries.memory.enabled\" : \" true\" ,\n "
128
- + " \" search.insights.top_queries.memory.window_size\" : \" 600s \" ,\n "
90
+ + " \" search.insights.top_queries.memory.window_size\" : \" 1m \" ,\n "
129
91
+ " \" search.insights.top_queries.memory.top_n_size\" : \" 5\" ,\n "
130
92
+ " \" search.insights.top_queries.cpu.enabled\" : \" true\" ,\n "
131
- + " \" search.insights.top_queries.cpu.window_size\" : \" 600s \" ,\n "
93
+ + " \" search.insights.top_queries.cpu.window_size\" : \" 1m \" ,\n "
132
94
+ " \" search.insights.top_queries.cpu.top_n_size\" : 5,\n "
133
95
+ " \" search.insights.top_queries.group_by\" : \" none\" \n "
134
96
+ " }\n "
0 commit comments