use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class GitHubApi method paginatedGet.
private <T> ImmutableList<T> paginatedGet(String path, String profilerName, Type type, String entity, ImmutableListMultimap<String, String> headers) throws RepoException, ValidationException {
ImmutableList.Builder<T> builder = ImmutableList.builder();
int pages = 0;
while (path != null && pages < MAX_PAGES) {
try (ProfilerTask ignore = profiler.start(String.format("%s_page_%d", profilerName, pages))) {
PaginatedList<T> page = transport.get(path, type, headers);
builder.addAll(page);
path = page.getNextUrl();
pages++;
} catch (GitHubApiException e) {
throw treatGitHubException(e, entity);
}
}
return builder.build();
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class RemoteArchiveOrigin method newReader.
@Override
public Reader<RemoteArchiveRevision> newReader(Glob originFiles, Authoring authoring) {
return new Reader<RemoteArchiveRevision>() {
@Override
public void checkout(RemoteArchiveRevision ref, Path workdir) throws ValidationException {
try {
// TODO(joshgoldman): Add richer ref object and ability to restrict download by host/url
URL url = new URL(ref.path.toString());
InputStream returned = transport.open(new URL(ref.path.toString()));
try (ProfilerTask ignored = profiler.start("remote_file_" + url);
ZipInputStream zipInputStream = remoteFileOptions.getZipInputStream(returned)) {
ZipEntry zipEntry;
while (((zipEntry = zipInputStream.getNextEntry()) != null)) {
if (originFiles.relativeTo(workdir.toAbsolutePath()).matches(workdir.resolve(Path.of(zipEntry.getName()))) && !zipEntry.isDirectory()) {
Files.createDirectories(workdir.resolve(zipEntry.getName()).getParent());
MoreFiles.asByteSink(workdir.resolve(zipEntry.getName())).writeFrom(zipInputStream);
}
}
}
} catch (IOException e) {
throw new ValidationException(String.format("Could not unzip file: %s \n%s", ref.path, e.getMessage()));
}
}
@Override
public ChangesResponse<RemoteArchiveRevision> changes(RemoteArchiveRevision fromRef, RemoteArchiveRevision toRef) throws RepoException {
return ChangesResponse.forChanges(ImmutableList.of(change(toRef)));
}
@Override
public Change<RemoteArchiveRevision> change(RemoteArchiveRevision ref) throws RepoException {
return new Change<>(ref, author, message, ref.readTimestamp(), ImmutableListMultimap.of());
}
@Override
public void visitChanges(RemoteArchiveRevision start, ChangesVisitor visitor) throws RepoException {
visitor.visit(change(start));
}
};
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class RemoteHttpFile method download.
protected synchronized void download() throws RepoException, ValidationException {
if (downloaded) {
return;
}
URL remote = getRemote();
try {
console.progressFmt("Fetching %s", remote);
ByteSink sink = getSink();
MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (ProfilerTask task = profiler.start("remote_file_" + remote)) {
try (DigestInputStream is = new DigestInputStream(transport.open(remote), digest)) {
sink.writeFrom(is);
sha256 = Optional.of(Bytes.asList(is.getMessageDigest().digest()).stream().map(b -> String.format("%02X", b)).collect(Collectors.joining()).toLowerCase());
}
downloaded = true;
}
} catch (IOException | NoSuchAlgorithmException e) {
throw new RepoException(String.format("Error downloading %s", remote), e);
}
}
use of com.google.copybara.profiler.Profiler.ProfilerTask in project copybara by google.
the class ConsoleProfilerListenerTest method testConsoleProfilerListener.
@Test
public void testConsoleProfilerListener() {
TestingConsole console = new TestingConsole();
profiler.init(ImmutableList.of(new ConsoleProfilerListener(console)));
try (ProfilerTask ignore = profiler.start("iterative")) {
ticker.advance(10, TimeUnit.MILLISECONDS);
try (ProfilerTask ignore2 = profiler.start("origin.checkout")) {
ticker.advance(5, TimeUnit.MILLISECONDS);
}
try (ProfilerTask ignore3 = profiler.start("transforms")) {
ticker.advance(20, TimeUnit.MILLISECONDS);
}
try (ProfilerTask ignore4 = profiler.start("destination.write")) {
ticker.advance(3, TimeUnit.MILLISECONDS);
}
}
profiler.stop();
console.assertThat().matchesNextSkipAhead(VERBOSE, "PROFILE:.*6 //copybara/iterative/origin.checkout").matchesNextSkipAhead(VERBOSE, "PROFILE:.*21 //copybara/iterative/transforms").matchesNextSkipAhead(VERBOSE, "PROFILE:.*4 //copybara/iterative/destination.write").matchesNextSkipAhead(VERBOSE, "PROFILE:.*45 //copybara/iterative").matchesNextSkipAhead(VERBOSE, "PROFILE:.*47 //copybara");
}
Aggregations