From f0ffa527b3d3027561b80be60cd8d7c3246c45b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Bulej?= Date: Mon, 27 May 2024 12:54:26 +0200 Subject: [PATCH] Validate actual values instead of hash Forks should not be owned by anyone and the philosophers should have consumed the expected number of meals. Also removes the extraneous run method. --- .../renaissance/scala/stm/Philosophers.scala | 25 ++++++++++++------- .../scala/stm/RealityShowPhilosophers.scala | 14 +++-------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/Philosophers.scala b/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/Philosophers.scala index 22478684..2b086d40 100644 --- a/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/Philosophers.scala +++ b/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/Philosophers.scala @@ -1,11 +1,10 @@ package org.renaissance.scala.stm -import scala.collection.JavaConverters._ import org.renaissance.Benchmark import org.renaissance.Benchmark._ import org.renaissance.BenchmarkContext import org.renaissance.BenchmarkResult -import org.renaissance.BenchmarkResult.Validators +import org.renaissance.BenchmarkResult.Assert import org.renaissance.License @Name("philosophers") @@ -34,20 +33,28 @@ final class Philosophers extends Benchmark { */ private var mealCountParam: Int = _ - private var expectedHash: String = _ - - override def setUpBeforeAll(c: BenchmarkContext) = { + override def setUpBeforeAll(c: BenchmarkContext): Unit = { threadCountParam = c.parameter("thread_count").toPositiveInteger mealCountParam = c.parameter("meal_count").toPositiveInteger - val expectedOutput = Array.fill(threadCountParam)(mealCountParam).toSeq.asJava; - expectedHash = Validators.computeHash(expectedOutput); + } + + private def validate(forkOwners: Seq[Option[String]], mealsEaten: Seq[Int]): Unit = { + // All forks should be available, i.e., not owned by anyone. + for (i <- 0 until threadCountParam) { + Assert.assertEquals(None, forkOwners(i), s"owner of fork %i") + } + + // All philosophers should have eaten the expected number of meals. + for (i <- 0 until threadCountParam) { + Assert.assertEquals(mealCountParam, mealsEaten(i), s"meals eaten by philosopher $i") + } } override def run(c: BenchmarkContext): BenchmarkResult = { - val mealsEaten = RealityShowPhilosophers.run(mealCountParam, threadCountParam) + val (forkOwners, mealsEaten) = RealityShowPhilosophers.run(mealCountParam, threadCountParam) () => { - Validators.hashing(expectedHash, mealsEaten.toSeq.asJava).validate() + validate(forkOwners, mealsEaten) } } diff --git a/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/RealityShowPhilosophers.scala b/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/RealityShowPhilosophers.scala index 7597510c..820a0c7c 100644 --- a/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/RealityShowPhilosophers.scala +++ b/benchmarks/scala-stm/src/main/scala/org/renaissance/scala/stm/RealityShowPhilosophers.scala @@ -1,7 +1,7 @@ package org.renaissance.scala.stm import scala.annotation.tailrec -import scala.collection._ +import scala.collection.mutable import scala.concurrent.stm._ /** @@ -85,7 +85,7 @@ object RealityShowPhilosophers { } } - def eatenMeals(philosopherCount: Int, meals: Int): Array[Int] = { + def run(mealCount: Int, philosopherCount: Int): (Seq[Option[String]], Seq[Int]) = { val names = for (i <- 0 until philosopherCount) yield { s"philosopher-$i" } @@ -93,7 +93,7 @@ object RealityShowPhilosophers { new Fork } val pthreads = Array.tabulate(names.size) { i => - new PhilosopherThread(names(i), meals, forks(i), forks((i + 1) % forks.length)) + new PhilosopherThread(names(i), mealCount, forks(i), forks((i + 1) % forks.length)) } val camera = new CameraThread(1000 / 60, forks, pthreads) camera.start() @@ -101,13 +101,7 @@ object RealityShowPhilosophers { for (t <- pthreads) t.join() camera.join() atomic { implicit txn => - pthreads.map { p => - p.mealsEaten.get(txn) - } + (forks.map(_.owner.get), pthreads.map(_.mealsEaten.get)) } } - - def run(meals: Int, philosopherCount: Int) = { - eatenMeals(philosopherCount, meals) - } }