Skip to content

Commit 30e991d

Browse files
Andre van de Venandrevandeven
authored andcommitted
added tests
Signed-off-by: Andre van de Ven <andrebvandeven@gmail.com>
1 parent 34b7d77 commit 30e991d

File tree

5 files changed

+190
-4
lines changed

5 files changed

+190
-4
lines changed

server/src/main/java/org/opensearch/search/profile/fetch/FetchProfileShardResult.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,4 @@ public static FetchProfileShardResult fromXContent(XContentParser parser) throws
7171
}
7272
return new FetchProfileShardResult(results);
7373
}
74-
75-
76-
7774
}

server/src/test/java/org/opensearch/search/profile/SearchProfileShardResultsTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.opensearch.core.xcontent.XContentParser;
3939
import org.opensearch.search.profile.aggregation.AggregationProfileShardResult;
4040
import org.opensearch.search.profile.aggregation.AggregationProfileShardResultTests;
41+
import org.opensearch.search.profile.fetch.FetchProfileShardResult;
4142
import org.opensearch.search.profile.query.QueryProfileShardResult;
4243
import org.opensearch.search.profile.query.QueryProfileShardResultTests;
4344
import org.opensearch.test.OpenSearchTestCase;
@@ -69,10 +70,16 @@ public static SearchProfileShardResults createTestItem() {
6970
queryProfileResults.add(QueryProfileShardResultTests.createTestItem());
7071
}
7172
AggregationProfileShardResult aggProfileShardResult = AggregationProfileShardResultTests.createTestItem(1);
73+
List<ProfileResult> fetchResults = new ArrayList<>();
74+
int fetchItems = rarely() ? 0 : randomIntBetween(1, 2);
75+
for (int f = 0; f < fetchItems; f++) {
76+
fetchResults.add(ProfileResultTests.createTestItem(1, false));
77+
}
78+
FetchProfileShardResult fetchProfileShardResult = new FetchProfileShardResult(fetchResults);
7279
NetworkTime networkTime = new NetworkTime(inboundTime, outboundTime);
7380
searchProfileResults.put(
7481
randomAlphaOfLengthBetween(5, 10),
75-
new ProfileShardResult(queryProfileResults, aggProfileShardResult, networkTime)
82+
new ProfileShardResult(queryProfileResults, aggProfileShardResult, fetchProfileShardResult, networkTime)
7683
);
7784
}
7885
return new SearchProfileShardResults(searchProfileResults);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.search.profile.fetch;
10+
11+
import org.opensearch.search.profile.AbstractProfileBreakdown;
12+
import org.opensearch.search.profile.Timer;
13+
import org.opensearch.test.OpenSearchTestCase;
14+
15+
import java.util.Map;
16+
17+
import static org.hamcrest.Matchers.equalTo;
18+
import static org.hamcrest.Matchers.greaterThan;
19+
20+
public class FetchProfileBreakdownTests extends OpenSearchTestCase {
21+
22+
public void testBreakdownMap() {
23+
FetchProfileBreakdown breakdown = new FetchProfileBreakdown();
24+
for (FetchTimingType type : FetchTimingType.values()) {
25+
Timer t = breakdown.getTimer(type);
26+
t.start();
27+
t.stop();
28+
}
29+
Map<String, Long> map = breakdown.toBreakdownMap();
30+
assertEquals(FetchTimingType.values().length * 3, map.size());
31+
for (FetchTimingType type : FetchTimingType.values()) {
32+
String key = type.toString();
33+
assertThat(map.get(key), greaterThan(0L));
34+
assertThat(map.get(key + AbstractProfileBreakdown.TIMING_TYPE_COUNT_SUFFIX), equalTo(1L));
35+
assertThat(map.get(key + AbstractProfileBreakdown.TIMING_TYPE_START_TIME_SUFFIX), greaterThan(0L));
36+
}
37+
long total = breakdown.toNodeTime();
38+
long sum = 0;
39+
for (FetchTimingType type : FetchTimingType.values()) {
40+
sum += map.get(type.toString());
41+
}
42+
assertEquals(sum, total);
43+
}
44+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.search.profile.fetch;
9+
10+
import org.opensearch.common.xcontent.XContentType;
11+
import org.opensearch.core.common.bytes.BytesReference;
12+
import org.opensearch.core.xcontent.ToXContent;
13+
import org.opensearch.core.xcontent.XContentParser;
14+
import org.opensearch.core.xcontent.XContentParserUtils;
15+
import org.opensearch.search.profile.ProfileResult;
16+
import org.opensearch.search.profile.ProfileResultTests;
17+
import org.opensearch.test.OpenSearchTestCase;
18+
19+
import java.io.IOException;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import static org.opensearch.core.xcontent.XContentHelper.toXContent;
24+
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertToXContentEquivalent;
25+
26+
public class FetchProfileShardResultTests extends OpenSearchTestCase {
27+
28+
public static FetchProfileShardResult createTestItem() {
29+
int size = randomIntBetween(0, 5);
30+
List<ProfileResult> results = new ArrayList<>(size);
31+
for (int i = 0; i < size; i++) {
32+
results.add(ProfileResultTests.createTestItem(1, false));
33+
}
34+
return new FetchProfileShardResult(results);
35+
}
36+
37+
public void testFromXContent() throws IOException {
38+
FetchProfileShardResult profileResult = createTestItem();
39+
XContentType xContentType = randomFrom(XContentType.values());
40+
boolean humanReadable = randomBoolean();
41+
BytesReference originalBytes = toShuffledXContent(profileResult, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
42+
FetchProfileShardResult parsed;
43+
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
44+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
45+
XContentParserUtils.ensureFieldName(parser, parser.nextToken(), FetchProfileShardResult.FETCH);
46+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.nextToken(), parser);
47+
parsed = FetchProfileShardResult.fromXContent(parser);
48+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
49+
assertNull(parser.nextToken());
50+
}
51+
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
52+
}
53+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
/*
10+
* Licensed to Elasticsearch under one or more contributor
11+
* license agreements. See the NOTICE file distributed with
12+
* this work for additional information regarding copyright
13+
* ownership. Elasticsearch licenses this file to you under
14+
* the Apache License, Version 2.0 (the "License"); you may
15+
* not use this file except in compliance with the License.
16+
* You may obtain a copy of the License at
17+
*
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
*
20+
* Unless required by applicable law or agreed to in writing,
21+
* software distributed under the License is distributed on an
22+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23+
* KIND, either express or implied. See the License for the
24+
* specific language governing permissions and limitations
25+
* under the License.
26+
*/
27+
28+
/*
29+
* Modifications Copyright OpenSearch Contributors. See
30+
* GitHub history for details.
31+
*/
32+
package org.opensearch.search.profile.fetch;
33+
34+
import org.opensearch.search.profile.AbstractProfileBreakdown;
35+
import org.opensearch.search.profile.ProfileResult;
36+
import org.opensearch.search.profile.Timer;
37+
import org.opensearch.test.OpenSearchTestCase;
38+
39+
import java.util.List;
40+
import java.util.Map;
41+
42+
import static org.hamcrest.Matchers.equalTo;
43+
import static org.hamcrest.Matchers.greaterThan;
44+
45+
public class FetchProfilerTests extends OpenSearchTestCase {
46+
47+
public void testBasicProfiling() {
48+
FetchProfiler profiler = new FetchProfiler();
49+
FetchProfileBreakdown breakdown = profiler.getQueryBreakdown("fetch");
50+
for (FetchTimingType type : FetchTimingType.values()) {
51+
Timer t = breakdown.getTimer(type);
52+
t.start();
53+
t.stop();
54+
}
55+
profiler.pollLastElement();
56+
List<ProfileResult> results = profiler.getTree();
57+
assertEquals(1, results.size());
58+
ProfileResult profileResult = results.get(0);
59+
assertEquals("fetch", profileResult.getQueryName());
60+
Map<String, Long> map = profileResult.getTimeBreakdown();
61+
assertEquals(FetchTimingType.values().length * 3, map.size());
62+
long sum = 0;
63+
for (FetchTimingType type : FetchTimingType.values()) {
64+
String key = type.toString();
65+
assertThat(map.get(key), greaterThan(0L));
66+
assertThat(map.get(key + AbstractProfileBreakdown.TIMING_TYPE_COUNT_SUFFIX), equalTo(1L));
67+
assertThat(map.get(key + AbstractProfileBreakdown.TIMING_TYPE_START_TIME_SUFFIX), greaterThan(0L));
68+
sum += map.get(key);
69+
}
70+
assertEquals(sum, profileResult.getTime());
71+
}
72+
73+
public void testTimerAggregation() {
74+
FetchProfileBreakdown breakdown = new FetchProfileBreakdown();
75+
Timer timer = breakdown.getTimer(FetchTimingType.FETCH_FIELDS);
76+
timer.start();
77+
timer.stop();
78+
timer.start();
79+
timer.stop();
80+
Map<String, Long> map = breakdown.toBreakdownMap();
81+
assertThat(map.get(FetchTimingType.FETCH_FIELDS.toString()), greaterThan(0L));
82+
assertThat(map.get(FetchTimingType.FETCH_FIELDS + AbstractProfileBreakdown.TIMING_TYPE_COUNT_SUFFIX), equalTo(2L));
83+
assertThat(breakdown.toNodeTime(), equalTo(map.get(FetchTimingType.FETCH_FIELDS.toString())));
84+
}
85+
}

0 commit comments

Comments
 (0)