Search in sources :

Example 26 with HttpUrl

use of okhttp3.HttpUrl 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)

Example 27 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class NettyHttpClient method release.

private void release(HttpChannel httpChannel) {
    HttpUrl url;
    synchronized (this) {
        url = backlog.pop();
        if (url == null) {
            // There were no URLs in the backlog. Pool this channel for later.
            freeChannels.push(httpChannel);
            return;
        }
    }
    // We removed a URL from the backlog. Schedule it right away.
    httpChannel.sendRequest(url);
}
Also used : HttpUrl(okhttp3.HttpUrl)

Example 28 with HttpUrl

use of okhttp3.HttpUrl in project retrofit by square.

the class RetrofitTest method baseUrlStringPropagated.

@Test
public void baseUrlStringPropagated() {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").build();
    HttpUrl baseUrl = retrofit.baseUrl();
    assertThat(baseUrl).isEqualTo(HttpUrl.parse("http://example.com/"));
}
Also used : HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 29 with HttpUrl

use of okhttp3.HttpUrl in project retrofit by square.

the class Crawler method crawlPage.

public void crawlPage(HttpUrl url) {
    // Skip hosts that we've visited many times.
    AtomicInteger hostnameCount = new AtomicInteger();
    AtomicInteger previous = hostnames.putIfAbsent(url.host(), hostnameCount);
    if (previous != null)
        hostnameCount = previous;
    if (hostnameCount.incrementAndGet() > 100)
        return;
    // Asynchronously visit URL.
    pageService.get(url).enqueue(new Callback<Page>() {

        @Override
        public void onResponse(Call<Page> call, Response<Page> response) {
            if (!response.isSuccessful()) {
                System.out.println(call.request().url() + ": failed: " + response.code());
                return;
            }
            // Print this page's URL and title.
            Page page = response.body();
            HttpUrl base = response.raw().request().url();
            System.out.println(base + ": " + page.title);
            // Enqueue its links for visiting.
            for (String link : page.links) {
                HttpUrl linkUrl = base.resolve(link);
                if (linkUrl != null && !fetchedUrls.add(linkUrl)) {
                    crawlPage(linkUrl);
                }
            }
        }

        @Override
        public void onFailure(Call<Page> call, Throwable t) {
            System.out.println(call.request().url() + ": failed: " + t);
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpUrl(okhttp3.HttpUrl)

Example 30 with HttpUrl

use of okhttp3.HttpUrl in project retrofit by square.

the class RetrofitTest method baseHttpUrlPropagated.

@Test
public void baseHttpUrlPropagated() {
    HttpUrl url = HttpUrl.parse("http://example.com/");
    Retrofit retrofit = new Retrofit.Builder().baseUrl(url).build();
    assertThat(retrofit.baseUrl()).isSameAs(url);
}
Also used : HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Aggregations

HttpUrl (okhttp3.HttpUrl)64 Test (org.junit.Test)51 MockResponse (okhttp3.mockwebserver.MockResponse)49 Request (okhttp3.Request)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 IOException (java.io.IOException)19 Response (okhttp3.Response)15 RequestBody (okhttp3.RequestBody)12 MockWebServer (okhttp3.mockwebserver.MockWebServer)10 Moshi (com.squareup.moshi.Moshi)6 CookieManager (java.net.CookieManager)6 Call (retrofit2.Call)6 Callback (retrofit2.Callback)6 Response (retrofit2.Response)6 LoginFailedException (com.pokegoapi.exceptions.request.LoginFailedException)5 HttpCookie (java.net.HttpCookie)5 QueryRequest (zipkin.storage.QueryRequest)5 ResponseCache (java.net.ResponseCache)4 Map (java.util.Map)4 Call (okhttp3.Call)4