From b237e05d373bf43efb80e995b856990457713527 Mon Sep 17 00:00:00 2001 From: nasr Date: Mon, 1 Dec 2025 02:39:15 +0100 Subject: feature: tried added fiber implementation when executing the tests from the view --- src/main/scala/main/Main.scala | 6 -- src/main/scala/main/domain/CpuOperations.scala | 1 + src/main/scala/main/domain/MemoryOperations.scala | 1 + src/main/scala/main/infrastructure/Http.scala | 10 +++ src/main/scala/main/infrastructure/Resources.scala | 11 ---- .../scala/main/infrastructure/routes/Routes.scala | 13 ++-- src/main/scala/main/services/Stress.scala | 77 +++++++++++++++++----- 7 files changed, 82 insertions(+), 37 deletions(-) diff --git a/src/main/scala/main/Main.scala b/src/main/scala/main/Main.scala index eaca7ab..e1660d6 100644 --- a/src/main/scala/main/Main.scala +++ b/src/main/scala/main/Main.scala @@ -14,7 +14,6 @@ import main.services._ import main.infrastructure._ import main.view._ import oshi._ -import zio.http._ import java.lang.foreign._ import zio._ @@ -40,11 +39,6 @@ object Torque extends ZIOAppDefault with Runner { } - def serve = { - Server.serve(routes) - .provide(Server.default) - .exitCode - } /** * diff --git a/src/main/scala/main/domain/CpuOperations.scala b/src/main/scala/main/domain/CpuOperations.scala index ebc5c9b..d39d76a 100644 --- a/src/main/scala/main/domain/CpuOperations.scala +++ b/src/main/scala/main/domain/CpuOperations.scala @@ -32,6 +32,7 @@ trait CPU { /* * TODO: I did the countrary of what i wanted to accieve with the is prime function * We want the function to be less optimized so that the CPU has more work == more stress + * TODO: Assert if the prime number was found or not */ class Prime extends CPU { diff --git a/src/main/scala/main/domain/MemoryOperations.scala b/src/main/scala/main/domain/MemoryOperations.scala index 548f071..5c58996 100644 --- a/src/main/scala/main/domain/MemoryOperations.scala +++ b/src/main/scala/main/domain/MemoryOperations.scala @@ -23,6 +23,7 @@ class MemoryAllocater { private val address: String = null private val arena: Arena = Arena.global() + // TODO: get user to define the size to allocate private val memorySegment = arena.allocate(1024 * 1024 * 1024) /** diff --git a/src/main/scala/main/infrastructure/Http.scala b/src/main/scala/main/infrastructure/Http.scala index 1f1084b..2f63452 100644 --- a/src/main/scala/main/infrastructure/Http.scala +++ b/src/main/scala/main/infrastructure/Http.scala @@ -1,6 +1,8 @@ package main.infrastructure import zio._ +import zio.http._ +import main.domain._ /** @@ -10,3 +12,11 @@ import zio._ * * source: https://ziohttp.com */ + def httpHandler(cpu: CpuInfo, ram: RamInfo, platform: PlatformInfo): Unit = { + + + Server.serve(routes) + .provide(Server.default) + .exitCode + + } diff --git a/src/main/scala/main/infrastructure/Resources.scala b/src/main/scala/main/infrastructure/Resources.scala index 8d05b27..10b0466 100644 --- a/src/main/scala/main/infrastructure/Resources.scala +++ b/src/main/scala/main/infrastructure/Resources.scala @@ -9,9 +9,7 @@ import zio._ class Resources { /** - * * Creating instances for gathering the data - * */ private val sysInfo: SystemInfo = new SystemInfo private val hardware = sysInfo.getHardware @@ -19,12 +17,9 @@ class Resources { private val sensors = hardware.getSensors private val cpu = hardware.getProcessor - /** - * * Platform Mehtods * */ - def getPlatform: ZIO[Any, Throwable, Unit] = { ZIO.attempt { println(sysInfo.getHardware) @@ -33,9 +28,7 @@ class Resources { /** - * * CPU methods - * * */ def getCpuInfo: ZIO[Any, Throwable, Unit] = { ZIO.attempt { @@ -53,13 +46,9 @@ class Resources { } } - /** - * * Memory specific methods - * * */ - def getRamInfo: ZIO[Any, Throwable, Unit] = { ZIO.attempt { diff --git a/src/main/scala/main/infrastructure/routes/Routes.scala b/src/main/scala/main/infrastructure/routes/Routes.scala index 304e46e..a021ffd 100644 --- a/src/main/scala/main/infrastructure/routes/Routes.scala +++ b/src/main/scala/main/infrastructure/routes/Routes.scala @@ -1,16 +1,21 @@ package main.infrastructure - import zio._ import zio.http._ +import main.domain._ -val routes = Routes ( +/** + * + * API routes to show the results of a stress test + * + * */ +val routes = Routes ( Method.GET / Root -> handler(Response.text("Greetings at your services")), Method.GET / "cpu" -> handler(Response.text("Greetings at your services")), - Method.GET / "ram" -> handler(Response.text("Greetings at your services")) - + Method.GET / "ram" -> handler(Response.text("Greetings at your services")), + Method.GET / "platform" -> handler(Response.text("Greetings at your services")) ) diff --git a/src/main/scala/main/services/Stress.scala b/src/main/scala/main/services/Stress.scala index d1d27c8..0b4769c 100644 --- a/src/main/scala/main/services/Stress.scala +++ b/src/main/scala/main/services/Stress.scala @@ -13,6 +13,7 @@ abstract class Stress { } +// TODO: program is exiting without completing the tests, my guess is it has something to do with ZIO Fiber forks class StressCpu() extends Stress { @@ -24,18 +25,20 @@ class StressCpu() extends Stress { def runCholeskyTest: ZIO[Any, Throwable, Unit] = { + val cd = new CholeskyDecomposition ZIO .attempt { - // TODO: declare a randomized array to pass into the runner - // cd.run(matrix) + val matrix: Vector[Vector[Int]] = Vector(Vector(32480934, 3240994, 20394402), Vector(324234, 2342354, 5432432), Vector(340983, 12038, 9834)) + cd.run(matrix) } .catchAll { error => Console.printError(s"failed: $error") } } def runPrimeTest: ZIO[Any, Throwable, Unit] = { + val p = new Prime ZIO .attempt { - // p.run(390483094, true) + p.run(390483094) } .catchAll { error => Console.printError(s"failed: $error") } } @@ -57,34 +60,76 @@ class StressCpu() extends Stress { class StressRam() extends Stress { + private val memoryAllocater = new MemoryAllocater + private val rand = new scala.util.Random + // TODO: set the status upon a pass or fail + // private var status: Status = false + + /** + * + * Idea of a run sequential for memory is allocating and deallocating a size + * lots of times back and forth + * + * */ override def runSequential: ZIO[Any, Throwable, Unit] = { - ZIO - .attempt { - // TODO: - } - .catchAll { error => Console.printError(s"failed: $error") } + val list: List[Int] = List(2030994, 493084, 43434, 43904 ,2103092, 230932) + val offset: Int = 4 + + ZIO.attempt { + + memoryAllocater.setValues(list, offset) + memoryAllocater.deallocateMemory + + }.catchAll { error => Console.printError(s"failed: sequential memory error")} } override def runParallel: ZIO[Any, Throwable, Unit] = { - ZIO.attempt {}.catchAll { error => Console.printError(s"failed: $error") } + + /** + * TODO: set the limit to the amount of CPU cores + * I wanted to set affinities per core but not there yet + * source: https://zio.dev/overview/basic-concurrency/ + * */ + ZIO.foreachPar(1 to 8) { + /** + * no need to save wich worked were on at the moment + *so were throwing it away with _ + */ + _ => ZIO.attemptBlocking { + + val list = List.fill(6)(rand.nextInt(1000000) + 100000) + /** + * still setting an offset of 4 because + * we are filling then memory with 4 byte integers + */ + val offset = 4 + + memoryAllocater.setValues(list, offset) + memoryAllocater.deallocateMemory + + + }.catchAll { error => Console.printError(s"failed: $error") } + }.unit } } trait Runner { - val c: StressCpu = new StressCpu - val r: StressRam = new StressRam + private val c: StressCpu = new StressCpu + private val r: StressRam = new StressRam def heavyCpuRun: Task[Unit] = { - for { - _ <- c.runParallel - _ <- c.runSequential - _ <- c.runParallel - } yield() + for { + + _ <- c.runParallel + _ <- c.runSequential + _ <- c.runParallel + + } yield() } def lightCpuRun: Task[Unit] = { -- cgit v1.2.3-70-g09d2