Search in sources :

Example 56 with HttpPost

use of org.apache.http.client.methods.HttpPost 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 57 with HttpPost

use of org.apache.http.client.methods.HttpPost in project pinpoint by naver.

the class HttpClientIT method test.

@Test
public void test() throws Exception {
    HttpClient httpClient = new DefaultHttpClient();
    try {
        HttpPost post = new HttpPost("http://www.naver.com");
        post.addHeader("Content-Type", "application/json;charset=UTF-8");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        httpClient.execute(post, responseHandler);
    } catch (Exception ignored) {
    } finally {
        if (null != httpClient && null != httpClient.getConnectionManager()) {
            httpClient.getConnectionManager().shutdown();
        }
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    Class<?> connectorClass;
    try {
        connectorClass = Class.forName("org.apache.http.impl.conn.ManagedClientConnectionImpl");
    } catch (ClassNotFoundException e) {
        connectorClass = Class.forName("org.apache.http.impl.conn.AbstractPooledConnAdapter");
    }
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", AbstractHttpClient.class.getMethod("execute", HttpUriRequest.class, ResponseHandler.class)));
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", connectorClass.getMethod("open", HttpRoute.class, HttpContext.class, HttpParams.class), annotation("http.internal.display", "www.naver.com")));
    verifier.verifyTrace(event("HTTP_CLIENT_4", HttpRequestExecutor.class.getMethod("execute", HttpRequest.class, HttpClientConnection.class, HttpContext.class), null, null, "www.naver.com", annotation("http.url", "/"), annotation("http.status.code", 200), annotation("http.io", anyAnnotationValue())));
    verifier.verifyTraceCount(0);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Example 58 with HttpPost

use of org.apache.http.client.methods.HttpPost in project android-uploader by nightscout.

the class AbstractRestUploaderTest method testUploads_setsEntity.

@Test
public void testUploads_setsEntity() throws IOException, InvalidRecordLengthException {
    setUpExecuteCaptor();
    restUploader.uploadGlucoseDataSets(Lists.newArrayList(mockGlucoseDataSet()));
    HttpPost post = (HttpPost) captor.getValue();
    String entity = CharStreams.toString(new InputStreamReader(post.getEntity().getContent()));
    assertThat(entity, containsString("jsonKey"));
    assertThat(entity, containsString("jsonValue"));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStreamReader(java.io.InputStreamReader) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 59 with HttpPost

use of org.apache.http.client.methods.HttpPost in project android-uploader by nightscout.

the class RestLegacyUploaderTest method testDeviceStatus_Entity.

@Test
public void testDeviceStatus_Entity() throws Exception {
    AbstractUploaderDevice deviceStatus = mockDeviceStatus();
    restUploader.uploadDeviceStatus(deviceStatus);
    HttpPost post = (HttpPost) captor.getValue();
    String entity = CharStreams.toString(new InputStreamReader(post.getEntity().getContent()));
    verifyDeviceStatus(new JSONObject(entity), deviceStatus);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) AbstractUploaderDevice(com.nightscout.core.drivers.AbstractUploaderDevice) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 60 with HttpPost

use of org.apache.http.client.methods.HttpPost in project android-uploader by nightscout.

the class RestV1UploaderTest method testMeterRecord_Entity.

@Test
public void testMeterRecord_Entity() throws Exception {
    restUploader.uploadMeterRecords(Lists.newArrayList(mockMeterRecord()));
    HttpPost post = (HttpPost) captor.getValue();
    String entity = CharStreams.toString(new InputStreamReader(post.getEntity().getContent()));
    verifyMeterRecord(new JSONObject(entity));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

HttpPost (org.apache.http.client.methods.HttpPost)531 HttpResponse (org.apache.http.HttpResponse)238 StringEntity (org.apache.http.entity.StringEntity)220 Test (org.junit.Test)153 IOException (java.io.IOException)143 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)107 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)99 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)90 ArrayList (java.util.ArrayList)86 NameValuePair (org.apache.http.NameValuePair)84 HttpEntity (org.apache.http.HttpEntity)83 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)68 HttpClient (org.apache.http.client.HttpClient)64 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)63 HttpGet (org.apache.http.client.methods.HttpGet)55 TestHttpClient (io.undertow.testutils.TestHttpClient)54 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JsonNode (com.fasterxml.jackson.databind.JsonNode)39 InputStream (java.io.InputStream)29