Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import com.google.devtools.build.lib.query2.proto.proto2api.Build
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.concurrent.ConcurrentMap
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
import java.util.Calendar

class BazelClient : KoinComponent {
private val logger: Logger by inject()
private val queryService: BazelQueryService by inject()

@OptIn(ExperimentalTime::class)
suspend fun queryAllTargets(): List<BazelTarget> {
val (targets, queryDuration) = measureTimedValue {
queryService.query("'//external:all-targets' + '//...:all-targets'")
}
var calendar = Calendar.getInstance()
val queryEpoch = calendar.getTimeInMillis()
val targets = queryService.query("'//external:all-targets' + '//...:all-targets'")
val queryDuration = calendar.getTimeInMillis() - queryEpoch
logger.i { "All targets queried in $queryDuration" }
return targets.mapNotNull { target: Build.Target ->
when (target.type) {
Expand All @@ -35,11 +34,11 @@ class BazelClient : KoinComponent {
}
}

@OptIn(ExperimentalTime::class)
suspend fun queryAllSourcefileTargets(): List<Build.Target> {
val (targets, queryDuration) = measureTimedValue {
queryService.query("kind('source file', //...:all-targets)")
}
var calendar = Calendar.getInstance()
val queryEpoch = calendar.getTimeInMillis()
val targets = queryService.query("kind('source file', //...:all-targets)")
val queryDuration = calendar.getTimeInMillis() - queryEpoch
logger.i { "All source files queried in $queryDuration" }

return targets
Expand Down
11 changes: 5 additions & 6 deletions cli/src/main/kotlin/com/bazel_diff/hash/BuildGraphHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import java.util.concurrent.ConcurrentMap
import java.util.concurrent.atomic.AtomicReference
import java.util.stream.Collectors
import kotlin.io.path.readBytes
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
import java.util.Calendar

class BuildGraphHasher(private val bazelClient: BazelClient) : KoinComponent {
private val targetHasher: TargetHasher by inject()
private val sourceFileHasher: SourceFileHasher by inject()
private val logger: Logger by inject()

@OptIn(ExperimentalTime::class)
fun hashAllBazelTargetsAndSourcefiles(seedFilepaths: Set<Path> = emptySet()): Map<String, String> {
/**
* Bazel will lock parallel queries but this is still allowing us to hash source files while executing a parallel query
Expand All @@ -45,9 +43,10 @@ class BuildGraphHasher(private val bazelClient: BazelClient) : KoinComponent {
* Querying targets and source hashing is done in parallel
*/
val sourceDigestsFuture = async(Dispatchers.IO) {
val (sourceFileTargets, sourceHashDuration) = measureTimedValue {
hashSourcefiles(sourceTargets)
}
var calendar = Calendar.getInstance()
val sourceHashDurationEpoch = calendar.getTimeInMillis()
val sourceFileTargets = hashSourcefiles(sourceTargets)
val sourceHashDuration = calendar.getTimeInMillis() - sourceHashDurationEpoch
logger.i { "Source file hashes calculated in $sourceHashDuration" }
sourceFileTargets
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,35 @@ import java.io.FileDescriptor
import java.io.FileReader
import java.io.FileWriter
import java.nio.file.Path
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
import java.util.Calendar

class GenerateHashesInteractor : KoinComponent {
private val buildGraphHasher: BuildGraphHasher by inject()
private val logger: Logger by inject()
private val gson: Gson by inject()

@OptIn(ExperimentalTime::class)
fun execute(seedFilepaths: File?, outputPath: File?): Boolean {
return try {
val duration = measureTime {
var seedFilepathsSet: Set<Path> = when {
seedFilepaths != null -> {
BufferedReader(FileReader(seedFilepaths)).use {
it.readLines()
.map { line: String -> File(line).toPath() }
.toSet()
}
var calendar = Calendar.getInstance()
val epoch = calendar.getTimeInMillis()
var seedFilepathsSet: Set<Path> = when {
seedFilepaths != null -> {
BufferedReader(FileReader(seedFilepaths)).use {
it.readLines()
.map { line: String -> File(line).toPath() }
.toSet()
}
else -> emptySet()
}
val hashes = buildGraphHasher.hashAllBazelTargetsAndSourcefiles(seedFilepathsSet)
when (outputPath) {
null -> FileWriter(FileDescriptor.out)
else -> FileWriter(outputPath)
}.use {
it.write(gson.toJson(hashes))
}
else -> emptySet()
}
val hashes = buildGraphHasher.hashAllBazelTargetsAndSourcefiles(seedFilepathsSet)
when (outputPath) {
null -> FileWriter(FileDescriptor.out)
else -> FileWriter(outputPath)
}.use {
it.write(gson.toJson(hashes))
}
val duration = calendar.getTimeInMillis() - epoch;
logger.i { "generate-hashes finished in $duration" }
true
} catch (e: Exception) {
Expand Down