Search in sources :

Example 26 with ProfilerTask

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();
}
Also used : CONFLICT(com.google.copybara.git.github.api.GitHubApiException.ResponseCode.CONFLICT) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableList(com.google.common.collect.ImmutableList)

Example 27 with ProfilerTask

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));
        }
    };
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ValidationException(com.google.copybara.exception.ValidationException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Change(com.google.copybara.Change) URL(java.net.URL)

Example 28 with ProfilerTask

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);
    }
}
Also used : ByteSink(com.google.common.io.ByteSink) DigestInputStream(java.security.DigestInputStream) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RepoException(com.google.copybara.exception.RepoException) MessageDigest(java.security.MessageDigest) URL(java.net.URL)

Example 29 with ProfilerTask

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");
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Test(org.junit.Test)

Aggregations

ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)29 ValidationException (com.google.copybara.exception.ValidationException)10 ImmutableList (com.google.common.collect.ImmutableList)8 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)8 RepoException (com.google.copybara.exception.RepoException)7 Test (org.junit.Test)6 Change (com.google.copybara.Change)5 Endpoint (com.google.copybara.Endpoint)5 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)5 SkylarkConsole (com.google.copybara.transform.SkylarkConsole)5 IOException (java.io.IOException)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Preconditions (com.google.common.base.Preconditions)4 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)4 Action (com.google.copybara.action.Action)4 ChangeMigrationFinishedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)4 Path (java.nio.file.Path)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)3 ImmutableListMultimap.toImmutableListMultimap (com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap)3