Skip to content

Commit cd3a349

Browse files
authored
Merge branch 'master' into file-channel
2 parents c71366d + 455562c commit cd3a349

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ workspace.
139139
-V, --version Print version information and exit.
140140
-w, --workspacePath=<workspacePath>
141141
Path to Bazel workspace directory.
142+
--displayElapsedTime This flag controls whether to print out elapsed time
143+
for bazel query and content hashing
142144
```
143145

144146
## Installing

src/main/java/com/bazel_diff/BazelClient.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.nio.file.Path;
1010
import java.security.MessageDigest;
1111
import java.security.NoSuchAlgorithmException;
12+
import java.time.Duration;
13+
import java.time.Instant;
1214
import java.util.ArrayList;
1315
import java.util.HashMap;
1416
import java.util.List;
@@ -26,6 +28,7 @@ class BazelClientImpl implements BazelClient {
2628
private Path bazelPath;
2729
private Boolean verbose;
2830
private Boolean keepGoing;
31+
private Boolean displayElapsedTime;
2932
private List<String> startupOptions;
3033
private List<String> commandOptions;
3134

@@ -35,25 +38,44 @@ class BazelClientImpl implements BazelClient {
3538
String startupOptions,
3639
String commandOptions,
3740
Boolean verbose,
38-
Boolean keepGoing
41+
Boolean keepGoing,
42+
Boolean displayElapsedTime
3943
) {
4044
this.workingDirectory = workingDirectory.normalize();
4145
this.bazelPath = bazelPath;
4246
this.startupOptions = startupOptions != null ? Arrays.asList(startupOptions.split(" ")): new ArrayList<String>();
4347
this.commandOptions = commandOptions != null ? Arrays.asList(commandOptions.split(" ")): new ArrayList<String>();
4448
this.verbose = verbose;
4549
this.keepGoing = keepGoing;
50+
this.displayElapsedTime = displayElapsedTime;
4651
}
4752

4853
@Override
4954
public List<BazelTarget> queryAllTargets() throws IOException {
55+
Instant queryStartTime = Instant.now();
5056
List<Build.Target> targets = performBazelQuery("'//external:all-targets' + '//...:all-targets'");
57+
Instant queryEndTime = Instant.now();
58+
if (displayElapsedTime) {
59+
long querySeconds = Duration.between(queryStartTime, queryEndTime).getSeconds();
60+
System.out.printf("BazelDiff: All targets queried in %d seconds%n", querySeconds);
61+
}
5162
return targets.stream().map( target -> new BazelTargetImpl(target)).collect(Collectors.toList());
5263
}
5364

5465
@Override
5566
public Map<String, BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException {
56-
return processBazelSourcefileTargets(performBazelQuery("kind('source file', //...:all-targets)"), true);
67+
Instant queryStartTime = Instant.now();
68+
List<Build.Target> targets = performBazelQuery("kind('source file', //...:all-targets)");
69+
Instant queryEndTime = Instant.now();
70+
Map<String, BazelSourceFileTarget> sourceFileTargets = processBazelSourcefileTargets(targets, true);
71+
Instant contentHashEndTime = Instant.now();
72+
if (displayElapsedTime) {
73+
long querySeconds = Duration.between(queryStartTime, queryEndTime).getSeconds();
74+
long contentHashSeconds = Duration.between(queryEndTime, contentHashEndTime).getSeconds();
75+
System.out.printf("BazelDiff: All source files queried in %d seconds%n", querySeconds);
76+
System.out.printf("BazelDiff: Content hash calculated in %d seconds%n", contentHashSeconds);
77+
}
78+
return sourceFileTargets;
5779
}
5880

5981
private Map<String, BazelSourceFileTarget> processBazelSourcefileTargets(List<Build.Target> targets, Boolean readSourcefileTargets) throws IOException, NoSuchAlgorithmException {

src/main/java/com/bazel_diff/main.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.io.*;
1111
import java.nio.file.Path;
1212
import java.security.NoSuchAlgorithmException;
13+
import java.time.Duration;
14+
import java.time.Instant;
1315
import java.util.HashMap;
1416
import java.util.HashSet;
1517
import java.util.Map;
@@ -33,6 +35,9 @@ class GenerateHashes implements Callable<Integer> {
3335
@Option(names = {"-s", "--seed-filepaths"}, description = "A text file containing a newline separated list of filepaths, each of these filepaths will be read and used as a seed for all targets.")
3436
File seedFilepaths;
3537

38+
@Option(names = {"--displayElapsedTime"}, description = "This flag controls whether to print out elapsed time for bazel query and content hashing")
39+
boolean displayElapsedTime;
40+
3641
@Parameters(index = "0", description = "The filepath to write the resulting JSON of dictionary target => SHA-256 values")
3742
File outputPath;
3843

@@ -44,9 +49,11 @@ public Integer call() {
4449
parent.bazelStartupOptions,
4550
parent.bazelCommandOptions,
4651
BazelDiff.isVerbose(),
47-
parent.keepGoing);
52+
parent.keepGoing,
53+
displayElapsedTime);
4854
TargetHashingClient hashingClient = new TargetHashingClientImpl(bazelClient, new FilesClientImp());
4955
try {
56+
Instant generateHashStartTime = Instant.now();
5057
Set<Path> seedFilepathsSet = new HashSet<>();
5158
if (seedFilepaths != null) {
5259
FileReader fileReader = new FileReader(seedFilepaths);
@@ -60,6 +67,11 @@ public Integer call() {
6067
FileWriter myWriter = new FileWriter(outputPath);
6168
myWriter.write(gson.toJson(hashes));
6269
myWriter.close();
70+
Instant generateHashEndTime = Instant.now();
71+
if (displayElapsedTime) {
72+
long generateHashSeconds = Duration.between(generateHashStartTime, generateHashEndTime).getSeconds();
73+
System.out.printf("BazelDiff: Generate-hashes command finishes in %d seconds%n", generateHashSeconds);
74+
}
6375
return ExitCode.OK;
6476
} catch (IOException | NoSuchAlgorithmException e) {
6577
e.printStackTrace();
@@ -121,7 +133,8 @@ public Integer call() throws IOException {
121133
bazelStartupOptions,
122134
bazelCommandOptions,
123135
BazelDiff.isVerbose(),
124-
keepGoing
136+
keepGoing,
137+
false
125138
);
126139
TargetHashingClient hashingClient = new TargetHashingClientImpl(bazelClient, new FilesClientImp());
127140
Gson gson = new Gson();

0 commit comments

Comments
 (0)