Remove return statements in UpdateReportPersistenceBenchmark

This commit is contained in:
xuwei-k 2026-03-20 12:39:44 +09:00 committed by kenji yoshida
parent 87d8ae3a6c
commit 324f863d60
1 changed files with 43 additions and 42 deletions

View File

@ -31,60 +31,61 @@ final case class BenchmarkResult(
object UpdateReportPersistenceBenchmark: object UpdateReportPersistenceBenchmark:
@SuppressWarnings(Array("scalafix:DisableSyntax"))
def run( def run(
iterations: Int = 500, iterations: Int = 500,
configs: Seq[String] = Seq("compile", "test"), configs: Seq[String] = Seq("compile", "test"),
modulesPerConfig: Int = 50, modulesPerConfig: Int = 50,
warmupIterations: Int = 10 warmupIterations: Int = 10
): Either[String, BenchmarkResult] = ): Either[String, BenchmarkResult] =
if iterations <= 0 then return Left("iterations must be positive") for {
if configs.isEmpty then return Left("configs must be non-empty") _ <- Either.cond(iterations > 0, (), "iterations must be positive")
if modulesPerConfig <= 0 then return Left("modulesPerConfig must be positive") _ <- Either.cond(configs.nonEmpty, (), "configs must be non-empty")
if warmupIterations < 0 then return Left("warmupIterations must be non-negative") _ <- Either.cond(modulesPerConfig > 0, (), "modulesPerConfig must be positive")
_ <- Either.cond(warmupIterations >= 0, (), "warmupIterations must be non-negative")
baseDir = IO.createTemporaryDirectory
result <-
try
val report = buildSampleReport(baseDir, configs, modulesPerConfig)
val fullStore = CacheStore(new File(baseDir, "full-format.json"))
val cacheStore = CacheStore(new File(baseDir, "cache-format.json"))
val baseDir = IO.createTemporaryDirectory fullStore.write(report)
try UpdateReportPersistence.writeTo(cacheStore, UpdateReportPersistence.toCache(report))
val report = buildSampleReport(baseDir, configs, modulesPerConfig)
val fullStore = CacheStore(new File(baseDir, "full-format.json"))
val cacheStore = CacheStore(new File(baseDir, "cache-format.json"))
fullStore.write(report) for _ <- 0 until warmupIterations do
UpdateReportPersistence.writeTo(cacheStore, UpdateReportPersistence.toCache(report)) fullStore.read[UpdateReport]()
UpdateReportPersistence
.readFrom(cacheStore)
.map(UpdateReportPersistence.fromCache)
.getOrElse(sys.error("Expected cache report during warmup"))
for _ <- 0 until warmupIterations do val fullStart = System.currentTimeMillis()
fullStore.read[UpdateReport]() for _ <- 0 until iterations do fullStore.read[UpdateReport]()
UpdateReportPersistence val fullEnd = System.currentTimeMillis()
.readFrom(cacheStore)
.map(UpdateReportPersistence.fromCache)
.getOrElse(sys.error("Expected cache report during warmup"))
val fullStart = System.currentTimeMillis() val cacheStart = System.currentTimeMillis()
for _ <- 0 until iterations do fullStore.read[UpdateReport]() for _ <- 0 until iterations do
val fullEnd = System.currentTimeMillis() UpdateReportPersistence
.readFrom(cacheStore)
.map(UpdateReportPersistence.fromCache)
.getOrElse(sys.error("Expected cache report during benchmark"))
val cacheEnd = System.currentTimeMillis()
val cacheStart = System.currentTimeMillis() val fullSize = new File(baseDir, "full-format.json").length()
for _ <- 0 until iterations do val cacheSize = new File(baseDir, "cache-format.json").length()
UpdateReportPersistence
.readFrom(cacheStore)
.map(UpdateReportPersistence.fromCache)
.getOrElse(sys.error("Expected cache report during benchmark"))
val cacheEnd = System.currentTimeMillis()
val fullSize = new File(baseDir, "full-format.json").length() Right(
val cacheSize = new File(baseDir, "cache-format.json").length() BenchmarkResult(
iterationCount = iterations,
Right( fullFormatMs = fullEnd - fullStart,
BenchmarkResult( cacheFormatMs = cacheEnd - cacheStart,
iterationCount = iterations, fullSizeBytes = fullSize,
fullFormatMs = fullEnd - fullStart, cacheSizeBytes = cacheSize
cacheFormatMs = cacheEnd - cacheStart, )
fullSizeBytes = fullSize, )
cacheSizeBytes = cacheSize catch case e: Exception => Left(s"Benchmark failed: ${e.getMessage}")
) finally IO.delete(baseDir)
) } yield result
catch case e: Exception => Left(s"Benchmark failed: ${e.getMessage}")
finally IO.delete(baseDir)
def buildSampleReport( def buildSampleReport(
baseDir: File, baseDir: File,