-
Notifications
You must be signed in to change notification settings - Fork 984
/
DrillTest.java
194 lines (157 loc) · 6.08 KB
/
DrillTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.drill.test;
import java.lang.management.BufferPoolMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.List;
import org.apache.drill.common.util.DrillStringUtils;
import org.apache.drill.common.util.TestTools;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.rules.TestName;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.Logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class DrillTest {
// private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DrillTest.class);
protected static final ObjectMapper objectMapper;
static {
System.setProperty("line.separator", "\n");
objectMapper = new ObjectMapper();
}
static final SystemManager manager = new SystemManager();
static final Logger testReporter = org.slf4j.LoggerFactory.getLogger("org.apache.drill.TestReporter");
static final TestLogReporter LOG_OUTCOME = new TestLogReporter();
static MemWatcher memWatcher;
static String className;
@Rule public final TestRule TIMEOUT = TestTools.getTimeoutRule(50000);
@Rule public final TestLogReporter logOutcome = LOG_OUTCOME;
@Rule public final TestRule REPEAT_RULE = TestTools.getRepeatRule(false);
/**
* Rule for tests that verify {@link org.apache.drill.common.exceptions.UserException} type and message. See
* {@link UserExceptionMatcher} and e.g. {@link org.apache.drill.exec.server.TestOptions#checkValidationException}.
* Tests that do not use this rule are not affected.
*/
@Rule public final ExpectedException thrownException = ExpectedException.none();
@Rule public TestName TEST_NAME = new TestName();
@Before
public void printID() throws Exception {
System.out.printf("Running %s#%s\n", getClass().getName(), TEST_NAME.getMethodName());
}
@BeforeClass
public static void initDrillTest() throws Exception {
memWatcher = new MemWatcher();
}
@AfterClass
public static void finiDrillTest() throws InterruptedException{
testReporter.info(String.format("Test Class done (%s): %s.", memWatcher.getMemString(true), className));
LOG_OUTCOME.sleepIfFailure();
}
protected static class MemWatcher {
private long startDirect;
private long startHeap;
private long startNonHeap;
public MemWatcher() {
startDirect = manager.getMemDirect();
startHeap = manager.getMemHeap();
startNonHeap = manager.getMemNonHeap();
}
public Object getMemString() {
return getMemString(false);
}
public String getMemString(boolean runGC) {
if (runGC) {
Runtime.getRuntime().gc();
}
long endDirect = manager.getMemDirect();
long endHeap = manager.getMemHeap();
long endNonHeap = manager.getMemNonHeap();
return String.format("d: %s(%s), h: %s(%s), nh: %s(%s)", //
DrillStringUtils.readable(endDirect - startDirect), DrillStringUtils.readable(endDirect), //
DrillStringUtils.readable(endHeap - startHeap), DrillStringUtils.readable(endHeap), //
DrillStringUtils.readable(endNonHeap - startNonHeap), DrillStringUtils.readable(endNonHeap) //
);
}
}
private static class TestLogReporter extends TestWatcher {
private MemWatcher memWatcher;
private int failureCount = 0;
@Override
protected void starting(Description description) {
super.starting(description);
className = description.getClassName();
memWatcher = new MemWatcher();
}
@Override
protected void failed(Throwable e, Description description) {
testReporter.error(String.format("Test Failed (%s): %s", memWatcher.getMemString(), description.getDisplayName()), e);
failureCount++;
}
@Override
public void succeeded(Description description) {
testReporter.info(String.format("Test Succeeded (%s): %s", memWatcher.getMemString(), description.getDisplayName()));
}
public void sleepIfFailure() throws InterruptedException {
if(failureCount > 0){
Thread.sleep(2000);
failureCount = 0;
} else {
// pause to get logger to catch up.
Thread.sleep(250);
}
}
}
public static String escapeJsonString(String original) {
try {
return objectMapper.writeValueAsString(original);
} catch (JsonProcessingException e) {
return original;
}
}
private static class SystemManager {
final BufferPoolMXBean directBean;
final MemoryMXBean memoryBean;
public SystemManager(){
memoryBean = ManagementFactory.getMemoryMXBean();
BufferPoolMXBean localBean = null;
List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
for(BufferPoolMXBean b : pools){
if(b.getName().equals("direct")){
localBean = b;
}
}
directBean = localBean;
}
public long getMemDirect() {
return directBean.getMemoryUsed();
}
public long getMemHeap() {
return memoryBean.getHeapMemoryUsage().getUsed();
}
public long getMemNonHeap() {
return memoryBean.getNonHeapMemoryUsage().getUsed();
}
}
}