Search in sources :

Example 41 with StringEntity

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);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BufferedInputStream(java.io.BufferedInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File)

Example 42 with StringEntity

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;
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut)

Example 43 with StringEntity

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse)

Example 44 with StringEntity

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;
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BasicHeader(org.apache.http.message.BasicHeader)

Example 45 with StringEntity

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);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPut(org.apache.http.client.methods.HttpPut)

Aggregations

StringEntity (org.apache.http.entity.StringEntity)458 HttpPost (org.apache.http.client.methods.HttpPost)251 HttpResponse (org.apache.http.HttpResponse)149 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)141 Test (org.junit.Test)105 HttpPut (org.apache.http.client.methods.HttpPut)94 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)88 IOException (java.io.IOException)85 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)68 HttpEntity (org.apache.http.HttpEntity)61 JsonNode (com.fasterxml.jackson.databind.JsonNode)54 Deployment (org.activiti.engine.test.Deployment)50 TestHttpClient (io.undertow.testutils.TestHttpClient)30 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)27 StatusLine (org.apache.http.StatusLine)27 HttpGet (org.apache.http.client.methods.HttpGet)27 Gson (com.google.gson.Gson)24 UnsupportedEncodingException (java.io.UnsupportedEncodingException)24 ProtocolVersion (org.apache.http.ProtocolVersion)24 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)23