use of org.apache.http.entity.StringEntity 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.entity.StringEntity in project OpenAttestation by OpenAttestation.
the class ApacheHttpClient method put.
public ApiResponse put(String requestURL, ApiRequest message) throws IOException, SignatureException {
log.debug("PUT url: {}", requestURL);
log.debug("PUT content: {}", message == null ? "(empty)" : message.content);
HttpPut request = new HttpPut(requestURL);
if (message != null && message.content != null) {
request.setEntity(new StringEntity(message.content, ContentType.create(message.contentType.toString(), "UTF-8")));
}
HttpResponse httpResponse = httpClient.execute(request);
ApiResponse apiResponse = readResponse(httpResponse);
request.releaseConnection();
return apiResponse;
}
use of org.apache.http.entity.StringEntity in project OpenAttestation by OpenAttestation.
the class ApacheHttpClient method post.
public ApiResponse post(String requestURL, ApiRequest message) throws IOException, SignatureException {
log.debug("POST url: {}", requestURL);
log.debug("POST content: {}", message == null ? "(empty)" : message.content);
HttpPost request = new HttpPost(requestURL);
if (message != null && message.content != null) {
request.setEntity(new StringEntity(message.content, ContentType.create(message.contentType.toString(), "UTF-8")));
}
HttpResponse httpResponse = httpClient.execute(request);
ApiResponse apiResponse = readResponse(httpResponse);
request.releaseConnection();
return apiResponse;
}
use of org.apache.http.entity.StringEntity in project openkit-android by OpenKit.
the class OKHTTPClient method getJSONString.
private static StringEntity getJSONString(JSONObject jsonObject) {
StringEntity sEntity = null;
try {
sEntity = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
sEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
return sEntity;
} catch (UnsupportedEncodingException e) {
return null;
}
}
use of org.apache.http.entity.StringEntity in project openkit-android by OpenKit.
the class OKHTTPClient method putJSON.
public static void putJSON(String relativeUrl, JSONObject requestParams, AsyncHttpResponseHandler responseHandler) {
StringEntity sEntity = getJSONString(requestParams);
HttpPut request = new HttpPut(getAbsoluteUrl(relativeUrl));
if (sEntity == null) {
responseHandler.onFailure(new Throwable("JSON encoding error"), "JSON encoding error");
} else {
request.setEntity(sEntity);
sign(request);
client.put(request, "application/json", responseHandler);
}
}
Aggregations