Search in sources :

Example 1 with ArbitraryMeasurement

use of com.google.caliper.model.ArbitraryMeasurement in project okhttp by square.

the class Benchmark method run.

@ArbitraryMeasurement(description = "requests per second")
public double run() throws Exception {
    if (VERBOSE)
        System.out.println(toString());
    HttpClient httpClient = client.create();
    // Prepare the client & server
    httpClient.prepare(this);
    MockWebServer server = startServer();
    HttpUrl url = server.url("/");
    int requestCount = 0;
    long reportStart = System.nanoTime();
    long reportPeriod = TimeUnit.SECONDS.toNanos(1);
    int reports = 0;
    double best = 0.0;
    // Run until we've printed enough reports.
    while (reports < NUM_REPORTS) {
        // Print a report if we haven't recently.
        long now = System.nanoTime();
        double reportDuration = now - reportStart;
        if (reportDuration > reportPeriod) {
            double requestsPerSecond = requestCount / reportDuration * TimeUnit.SECONDS.toNanos(1);
            if (VERBOSE) {
                System.out.println(String.format("Requests per second: %.1f", requestsPerSecond));
            }
            best = Math.max(best, requestsPerSecond);
            requestCount = 0;
            reportStart = now;
            reports++;
        }
        // Fill the job queue with work.
        while (httpClient.acceptingJobs()) {
            httpClient.enqueue(url);
            requestCount++;
        }
        // The job queue is full. Take a break.
        sleep(1);
    }
    return best;
}
Also used : MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) ArbitraryMeasurement(com.google.caliper.model.ArbitraryMeasurement)

Aggregations

ArbitraryMeasurement (com.google.caliper.model.ArbitraryMeasurement)1 HttpUrl (okhttp3.HttpUrl)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1