use of org.apache.http.client.methods.CloseableHttpResponse in project pinot by linkedin.
the class AutoLoadPinotMetricsUtils method getSchemaFromSchemaEndpoint.
private Schema getSchemaFromSchemaEndpoint(String dataset) throws IOException {
Schema schema = null;
HttpGet schemaReq = new HttpGet(String.format(PINOT_SCHEMA_ENDPOINT, URLEncoder.encode(dataset, UTF_8)));
LOG.info("Retrieving schema: {}", schemaReq);
CloseableHttpResponse schemaRes = pinotControllerClient.execute(pinotControllerHost, schemaReq);
try {
if (schemaRes.getStatusLine().getStatusCode() != 200) {
LOG.error("Schema {} not found, {}", dataset, schemaRes.getStatusLine().toString());
} else {
InputStream schemaContent = schemaRes.getEntity().getContent();
schema = new org.codehaus.jackson.map.ObjectMapper().readValue(schemaContent, Schema.class);
}
} catch (Exception e) {
LOG.error("Exception in retrieving schema collections, skipping {}", dataset);
} finally {
if (schemaRes.getEntity() != null) {
EntityUtils.consume(schemaRes.getEntity());
}
schemaRes.close();
}
return schema;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project elasticsearch-analysis-ik by medcl.
the class Dictionary method getRemoteWords.
/**
* 从远程服务器上下载自定义词条
*/
private static List<String> getRemoteWords(String location) {
List<String> buffer = new ArrayList<String>();
RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000).build();
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response;
BufferedReader in;
HttpGet get = new HttpGet(location);
get.setConfig(rc);
try {
response = httpclient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
String charset = "UTF-8";
// 获取编码,默认为utf-8
if (response.getEntity().getContentType().getValue().contains("charset=")) {
String contentType = response.getEntity().getContentType().getValue();
charset = contentType.substring(contentType.lastIndexOf("=") + 1);
}
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
String line;
while ((line = in.readLine()) != null) {
buffer.add(line);
}
in.close();
response.close();
return buffer;
}
response.close();
} catch (ClientProtocolException e) {
logger.error("getRemoteWords {} error", e, location);
} catch (IllegalStateException e) {
logger.error("getRemoteWords {} error", e, location);
} catch (IOException e) {
logger.error("getRemoteWords {} error", e, location);
}
return buffer;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project pinot by linkedin.
the class DruidThroughput method main.
@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
final int numQueries = QUERIES.length;
final Random random = new Random(RANDOM_SEED);
final AtomicInteger counter = new AtomicInteger(0);
final AtomicLong totalResponseTime = new AtomicLong(0L);
final ExecutorService executorService = Executors.newFixedThreadPool(NUM_CLIENTS);
for (int i = 0; i < NUM_CLIENTS; i++) {
executorService.submit(new Runnable() {
@Override
public void run() {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost post = new HttpPost("http://localhost:8082/druid/v2/?pretty");
post.addHeader("content-type", "application/json");
CloseableHttpResponse res;
while (true) {
try (BufferedReader reader = new BufferedReader(new FileReader(QUERY_FILE_DIR + File.separator + random.nextInt(numQueries) + ".json"))) {
int length = reader.read(BUFFER);
post.setEntity(new StringEntity(new String(BUFFER, 0, length)));
}
long start = System.currentTimeMillis();
res = client.execute(post);
res.close();
counter.getAndIncrement();
totalResponseTime.getAndAdd(System.currentTimeMillis() - start);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
long startTime = System.currentTimeMillis();
while (true) {
Thread.sleep(REPORT_INTERVAL_MILLIS);
double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND;
int count = counter.get();
double avgResponseTime = ((double) totalResponseTime.get()) / count;
System.out.println("Time Passed: " + timePassedSeconds + "s, Query Executed: " + count + ", QPS: " + count / timePassedSeconds + ", Avg Response Time: " + avgResponseTime + "ms");
}
}
use of org.apache.http.client.methods.CloseableHttpResponse in project pinot by linkedin.
the class PinotResponseTime method main.
public static void main(String[] args) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost post = new HttpPost("http://localhost:8099/query");
CloseableHttpResponse res;
if (STORE_RESULT) {
File dir = new File(RESULT_DIR);
if (!dir.exists()) {
dir.mkdirs();
}
}
int length;
// Make sure all segments online
System.out.println("Test if number of records is " + RECORD_NUMBER);
post.setEntity(new StringEntity("{\"pql\":\"select count(*) from tpch_lineitem\"}"));
while (true) {
System.out.print('*');
res = client.execute(post);
boolean valid;
try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent())) {
length = in.read(BUFFER);
valid = new String(BUFFER, 0, length, "UTF-8").contains("\"value\":\"" + RECORD_NUMBER + "\"");
}
res.close();
if (valid) {
break;
} else {
Thread.sleep(5000);
}
}
System.out.println("Number of Records Test Passed");
// Start Benchmark
for (int i = 0; i < QUERIES.length; i++) {
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Start running query: " + QUERIES[i]);
post.setEntity(new StringEntity("{\"pql\":\"" + QUERIES[i] + "\"}"));
// Warm-up Rounds
System.out.println("Run " + WARMUP_ROUND + " times to warm up cache...");
for (int j = 0; j < WARMUP_ROUND; j++) {
res = client.execute(post);
if (!isValid(res, null)) {
System.out.println("\nInvalid Response, Sleep 20 Seconds...");
Thread.sleep(20000);
}
res.close();
System.out.print('*');
}
System.out.println();
// Test Rounds
int[] time = new int[TEST_ROUND];
int totalTime = 0;
int validIdx = 0;
System.out.println("Run " + TEST_ROUND + " times to get average time...");
while (validIdx < TEST_ROUND) {
long startTime = System.currentTimeMillis();
res = client.execute(post);
long endTime = System.currentTimeMillis();
boolean valid;
if (STORE_RESULT && validIdx == 0) {
valid = isValid(res, RESULT_DIR + File.separator + i + ".json");
} else {
valid = isValid(res, null);
}
if (!valid) {
System.out.println("\nInvalid Response, Sleep 20 Seconds...");
Thread.sleep(20000);
res.close();
continue;
}
res.close();
time[validIdx] = (int) (endTime - startTime);
totalTime += time[validIdx];
System.out.print(time[validIdx] + "ms ");
validIdx++;
}
System.out.println();
// Process Results
double avgTime = (double) totalTime / TEST_ROUND;
double stdDev = 0;
for (int temp : time) {
stdDev += (temp - avgTime) * (temp - avgTime) / TEST_ROUND;
}
stdDev = Math.sqrt(stdDev);
System.out.println("The average response time for the query is: " + avgTime + "ms");
System.out.println("The standard deviation is: " + stdDev);
}
}
}
use of org.apache.http.client.methods.CloseableHttpResponse in project spring-boot by spring-projects.
the class AbstractHttpClientMockTests method mockProjectGenerationError.
protected void mockProjectGenerationError(int status, String message) throws IOException, JSONException {
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, createJsonError(status, message).getBytes(), "application/json");
mockStatus(response, status);
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
}
Aggregations