|
7 | 7 |
|
8 | 8 | import static org.junit.Assert.assertEquals; |
9 | 9 | import static org.junit.Assert.assertFalse; |
10 | | -import static org.junit.Assert.assertNotNull; |
11 | 10 | import static org.junit.Assert.assertNull; |
12 | 11 | import static org.junit.Assert.assertThrows; |
13 | 12 | import static org.junit.Assert.assertTrue; |
|
21 | 20 | import org.junit.Test; |
22 | 21 | import org.opensearch.sql.api.compiler.UnifiedQueryCompiler; |
23 | 22 | import org.opensearch.sql.api.transpiler.UnifiedQueryTranspiler; |
24 | | -import org.opensearch.sql.executor.QueryType; |
25 | 23 | import org.opensearch.sql.monitor.profile.QueryProfile; |
26 | 24 | import org.opensearch.sql.monitor.profile.QueryProfiling; |
27 | 25 |
|
28 | 26 | /** Tests for profiling across unified query components and the measure() API. */ |
29 | 27 | public class UnifiedQueryProfilingTest extends UnifiedQueryTestBase { |
30 | 28 |
|
31 | | - @Test |
32 | | - public void testProfilingEnabled() throws Exception { |
33 | | - try (UnifiedQueryContext ctx = buildContext(true)) { |
34 | | - assertTrue(QueryProfiling.current().isEnabled()); |
35 | | - } |
| 29 | + @Override |
| 30 | + protected UnifiedQueryContext.Builder buildContext() { |
| 31 | + return super.buildContext().profiling(true); |
36 | 32 | } |
37 | 33 |
|
38 | 34 | @Test |
39 | | - public void testProfilingDisabledByDefault() throws Exception { |
40 | | - try (UnifiedQueryContext ctx = |
41 | | - UnifiedQueryContext.builder() |
42 | | - .language(QueryType.PPL) |
43 | | - .catalog("opensearch", testSchema) |
44 | | - .defaultNamespace("opensearch") |
45 | | - .build()) { |
46 | | - assertFalse(QueryProfiling.current().isEnabled()); |
47 | | - } |
| 35 | + public void testProfilingEnabled() { |
| 36 | + assertTrue(QueryProfiling.current().isEnabled()); |
48 | 37 | } |
49 | 38 |
|
50 | 39 | @Test |
51 | | - public void testProfilingExplicitlyDisabled() throws Exception { |
52 | | - try (UnifiedQueryContext ctx = buildContext(false)) { |
| 40 | + public void testProfilingDisabledByDefault() throws Exception { |
| 41 | + try (UnifiedQueryContext ctx = super.buildContext().build()) { |
53 | 42 | assertFalse(QueryProfiling.current().isEnabled()); |
54 | 43 | } |
55 | 44 | } |
56 | 45 |
|
57 | 46 | @Test |
58 | 47 | public void testProfilingClearedAfterClose() throws Exception { |
59 | | - UnifiedQueryContext ctx = buildContext(true); |
60 | 48 | assertTrue(QueryProfiling.current().isEnabled()); |
61 | | - ctx.close(); |
| 49 | + context.close(); |
62 | 50 | assertFalse(QueryProfiling.current().isEnabled()); |
63 | 51 | } |
64 | 52 |
|
65 | 53 | @Test |
66 | 54 | public void testGetProfileReturnsNullWhenDisabled() throws Exception { |
67 | | - try (UnifiedQueryContext ctx = buildContext(false)) { |
| 55 | + try (UnifiedQueryContext ctx = super.buildContext().build()) { |
68 | 56 | assertNull(ctx.getProfile()); |
69 | 57 | } |
70 | 58 | } |
71 | 59 |
|
72 | 60 | @Test |
73 | | - public void testPlannerAutoProfilesAnalyzePhase() throws Exception { |
74 | | - try (UnifiedQueryContext ctx = buildContext(true)) { |
75 | | - new UnifiedQueryPlanner(ctx).plan("source = opensearch.employees"); |
76 | | - QueryProfile profile = ctx.getProfile(); |
77 | | - assertNotNull(profile); |
78 | | - assertTrue(profile.getPhases().get("analyze").getTimeMillis() > 0); |
79 | | - } |
| 61 | + public void testPlannerAutoProfilesAnalyzePhase() { |
| 62 | + planner.plan("source = catalog.employees"); |
| 63 | + assertTrue(context.getProfile().getPhases().get("analyze").getTimeMillis() >= 0); |
80 | 64 | } |
81 | 65 |
|
82 | 66 | @Test |
83 | | - public void testCompilerAutoProfilesOptimizePhase() throws Exception { |
84 | | - try (UnifiedQueryContext ctx = buildContext(true)) { |
85 | | - RelNode plan = new UnifiedQueryPlanner(ctx).plan("source = opensearch.employees"); |
86 | | - PreparedStatement stmt = new UnifiedQueryCompiler(ctx).compile(plan); |
87 | | - assertNotNull(stmt); |
88 | | - QueryProfile profile = ctx.getProfile(); |
89 | | - assertNotNull(profile); |
90 | | - assertTrue(profile.getPhases().get("optimize").getTimeMillis() > 0); |
91 | | - } |
| 67 | + public void testCompilerAutoProfilesOptimizePhase() { |
| 68 | + RelNode plan = planner.plan("source = catalog.employees"); |
| 69 | + new UnifiedQueryCompiler(context).compile(plan); |
| 70 | + assertTrue(context.getProfile().getPhases().get("optimize").getTimeMillis() >= 0); |
92 | 71 | } |
93 | 72 |
|
94 | 73 | @Test |
95 | | - public void testTranspilerAutoProfilesTranspilePhase() throws Exception { |
96 | | - try (UnifiedQueryContext ctx = buildContext(true)) { |
97 | | - RelNode plan = new UnifiedQueryPlanner(ctx).plan("source = opensearch.employees"); |
98 | | - UnifiedQueryTranspiler transpiler = new UnifiedQueryTranspiler(ctx, SparkSqlDialect.DEFAULT); |
99 | | - assertNotNull(transpiler.toSql(plan)); |
100 | | - QueryProfile profile = ctx.getProfile(); |
101 | | - assertNotNull(profile); |
102 | | - assertTrue(profile.getPhases().get("transpile").getTimeMillis() > 0); |
103 | | - } |
| 74 | + public void testTranspilerAutoProfilesTranspilePhase() { |
| 75 | + RelNode plan = planner.plan("source = catalog.employees"); |
| 76 | + new UnifiedQueryTranspiler(context, SparkSqlDialect.DEFAULT).toSql(plan); |
| 77 | + assertTrue(context.getProfile().getPhases().get("transpile").getTimeMillis() >= 0); |
104 | 78 | } |
105 | 79 |
|
106 | 80 | @Test |
107 | 81 | public void testMeasureRecordsMetric() throws Exception { |
108 | | - try (UnifiedQueryContext ctx = buildContext(true)) { |
109 | | - assertEquals("done", ctx.measure(EXECUTE, () -> "done")); |
110 | | - QueryProfile profile = ctx.getProfile(); |
111 | | - assertNotNull(profile); |
112 | | - assertTrue(profile.getPhases().get("execute").getTimeMillis() >= 0); |
113 | | - } |
| 82 | + assertEquals("done", context.measure(EXECUTE, () -> "done")); |
| 83 | + assertTrue(context.getProfile().getPhases().get("execute").getTimeMillis() >= 0); |
114 | 84 | } |
115 | 85 |
|
116 | 86 | @Test |
117 | 87 | public void testMeasureExecutesWhenProfilingDisabled() throws Exception { |
118 | | - try (UnifiedQueryContext ctx = buildContext(false)) { |
| 88 | + try (UnifiedQueryContext ctx = super.buildContext().build()) { |
119 | 89 | assertEquals("done", ctx.measure(EXECUTE, () -> "done")); |
120 | 90 | assertNull(ctx.getProfile()); |
121 | 91 | } |
122 | 92 | } |
123 | 93 |
|
124 | 94 | @Test |
125 | | - public void testMeasurePropagatesException() throws Exception { |
126 | | - try (UnifiedQueryContext ctx = buildContext(true)) { |
127 | | - assertThrows( |
128 | | - IOException.class, |
129 | | - () -> |
130 | | - ctx.measure( |
131 | | - EXECUTE, |
132 | | - () -> { |
133 | | - throw new IOException("test error"); |
134 | | - })); |
135 | | - } |
| 95 | + public void testMeasurePropagatesException() { |
| 96 | + assertThrows( |
| 97 | + IOException.class, |
| 98 | + () -> |
| 99 | + context.measure( |
| 100 | + EXECUTE, |
| 101 | + () -> { |
| 102 | + throw new IOException("test error"); |
| 103 | + })); |
136 | 104 | } |
137 | 105 |
|
138 | 106 | @Test |
139 | 107 | public void testFullPipelineProfiling() throws Exception { |
140 | | - try (UnifiedQueryContext ctx = buildContext(true)) { |
141 | | - RelNode plan = new UnifiedQueryPlanner(ctx).plan("source = opensearch.employees"); |
142 | | - PreparedStatement stmt = new UnifiedQueryCompiler(ctx).compile(plan); |
143 | | - ResultSet rs = ctx.measure(EXECUTE, stmt::executeQuery); |
144 | | - assertNotNull(rs); |
145 | | - |
146 | | - QueryProfile profile = ctx.getProfile(); |
147 | | - assertNotNull(profile); |
148 | | - assertTrue(profile.getSummary().getTotalTimeMillis() > 0); |
149 | | - assertTrue(profile.getPhases().get("analyze").getTimeMillis() > 0); |
150 | | - assertTrue(profile.getPhases().get("optimize").getTimeMillis() > 0); |
151 | | - assertTrue(profile.getPhases().get("execute").getTimeMillis() >= 0); |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - private UnifiedQueryContext buildContext(boolean profiling) { |
156 | | - return UnifiedQueryContext.builder() |
157 | | - .language(QueryType.PPL) |
158 | | - .catalog("opensearch", testSchema) |
159 | | - .defaultNamespace("opensearch") |
160 | | - .profiling(profiling) |
161 | | - .build(); |
| 108 | + RelNode plan = planner.plan("source = catalog.employees"); |
| 109 | + PreparedStatement stmt = new UnifiedQueryCompiler(context).compile(plan); |
| 110 | + ResultSet rs = context.measure(EXECUTE, stmt::executeQuery); |
| 111 | + |
| 112 | + QueryProfile profile = context.getProfile(); |
| 113 | + assertTrue(profile.getSummary().getTotalTimeMillis() >= 0); |
| 114 | + assertTrue(profile.getPhases().get("analyze").getTimeMillis() >= 0); |
| 115 | + assertTrue(profile.getPhases().get("optimize").getTimeMillis() >= 0); |
| 116 | + assertTrue(profile.getPhases().get("execute").getTimeMillis() >= 0); |
162 | 117 | } |
163 | 118 | } |
0 commit comments