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;
}
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);
}
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/"));
}
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);
}
});
}
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);
}
Aggregations