From b01b0586a839cc0d36274489f13bf77eea065897 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Fri, 28 Nov 2025 20:30:58 +0100 Subject: feature & refactor: did a second refactor of the folders to a cap less one, did research on memory stress testign and i kind of forgot the rest --- src/main/scala/main/Http/Http.scala | 16 --- src/main/scala/main/Main.scala | 14 +-- src/main/scala/main/Ops/Ops.scala | 109 ------------------- src/main/scala/main/Ops/Resources.scala | 36 ------- src/main/scala/main/Tests/Tests.scala | 14 --- src/main/scala/main/Tools/Benchmark.scala | 46 -------- src/main/scala/main/Tools/Runner.scala | 42 -------- src/main/scala/main/domain/CpuOperations.scala | 118 +++++++++++++++++++++ src/main/scala/main/domain/MemoryOperations.scala | 40 +++++++ src/main/scala/main/infrastructure/Http.scala | 23 ++++ src/main/scala/main/infrastructure/Resources.scala | 36 +++++++ src/main/scala/main/services/Benchmark.scala | 23 ++++ src/main/scala/main/services/Stress.scala | 43 ++++++++ 13 files changed, 286 insertions(+), 274 deletions(-) delete mode 100644 src/main/scala/main/Http/Http.scala delete mode 100644 src/main/scala/main/Ops/Ops.scala delete mode 100644 src/main/scala/main/Ops/Resources.scala delete mode 100644 src/main/scala/main/Tests/Tests.scala delete mode 100644 src/main/scala/main/Tools/Benchmark.scala delete mode 100644 src/main/scala/main/Tools/Runner.scala create mode 100644 src/main/scala/main/domain/CpuOperations.scala create mode 100644 src/main/scala/main/domain/MemoryOperations.scala create mode 100644 src/main/scala/main/infrastructure/Http.scala create mode 100644 src/main/scala/main/infrastructure/Resources.scala create mode 100644 src/main/scala/main/services/Benchmark.scala create mode 100644 src/main/scala/main/services/Stress.scala diff --git a/src/main/scala/main/Http/Http.scala b/src/main/scala/main/Http/Http.scala deleted file mode 100644 index a56a563..0000000 --- a/src/main/scala/main/Http/Http.scala +++ /dev/null @@ -1,16 +0,0 @@ -import zio._ -import zio.http._ - -// https://ziohttp.com/ - -object GreetingServer extends ZIOAppDefault { - val routes = - Routes( - Method.GET / "stability" -> handler { (req: Request) => - val name = req.queryOrElse("", "World") - Response.text("stable!") - } - ) - - def run = Server.serve(routes).provide(Server.default) -} diff --git a/src/main/scala/main/Main.scala b/src/main/scala/main/Main.scala index 2ee9d9a..1406fc0 100644 --- a/src/main/scala/main/Main.scala +++ b/src/main/scala/main/Main.scala @@ -1,20 +1,12 @@ package main -import main.Ops.* -import main.Tests.* -import main.Tools.* -import main.Runner.* +import main.domain._ +import main.services._ +import main.infrastructure._ import oshi._ import zio._ -/* - * *> = "Then" (think arrow pointing to what matters) - * <&> = "Both" (symbol looks like things going in both directions) - * <* = "Keep left" (pointing to what you keep) - * &> = "Keep right" (pointing to what you keep) - * */ - object Torque extends ZIOAppDefault { def run: ZIO[ZIOAppArgs & Scope, Any, Any] = { diff --git a/src/main/scala/main/Ops/Ops.scala b/src/main/scala/main/Ops/Ops.scala deleted file mode 100644 index 214b824..0000000 --- a/src/main/scala/main/Ops/Ops.scala +++ /dev/null @@ -1,109 +0,0 @@ -package main.Ops - -import main.Tools.* - -import scala.util.hashing -import scala.util.hashing.MurmurHash3 -import scala.math._ -import scala.collection.immutable.ListSet -import scala.collection.mutable.ArrayBuffer - - -class Prime() { - - /* - * Calculate all primes up to limit - * This should stress the ALU in someway, - * doing this in a predictable manner, - * will hopefully keep the cpu pipeline busy - * and that way stress the branch predictor - * - * math.sqrt(n) => a prime number has 2 factors, one of the factors - * of the prime numbers has to be smaller then n - * after that we check if the number is whole number and thereby checking if its a prime - * - */ - - - /* - * 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 - */ - - def run(n: Int, result: Boolean): Unit = { - for i <- 0 to n do if isPrime(i) == result then println("true") else println("false") - } - - private def isPrime(n: Int): Boolean = { - if n <= 1 then false - else !(2 to math.sqrt(n).toInt).exists(i => n % i == 0) - } - -} - -class Hash { - - def run(word: String, loopSize: Int): Unit = { - - /* TODO: implement ALU friendly, so high speed hashing - * to continuously loop over voor stressing - * ALU - * - * While looking for hashing algorithmes to implement I stumbled on: - * https://scala-lang.org/api/3.x/scala/util/hashing/MurmurHash3$.html - * - * which is an implemntation of **smasher** http://github.com/aappleby/smhasher - * the exact type of hashing algorithm I was looking for - * - * In the scala description they state: "This algorithm is designed to generate - * well-distributed non-cryptographic hashes. It is designed to hash data in 32 bit chunks (ints). " - * - * (ints) -> ALU - * - */ - - for i <- 0 to loopSize do MurmurHash3.stringHash(word) - - } -} - -class CholeskyDecomposition { - - /* - * Floating point operation to stress the cpu - * Calculate the number of KFLOPS / FLOPS - * implementation of the Cholesky decomposition - * More information on the Cholesky decomposition at: - * https://en.wikipedia.org/wiki/Cholesky_decomposition - * - * Linpack uses the cholesky decomposition - * https://www.netlib.org/linpack/ - * - * https://www.geeksforgeeks.org/dsa/cholesky-decomposition-matrix-decomposition/ - * - * The Cholesky decomposition maps matrix A into the product of A = L · LH where L is the lower triangular matrix and LH is the transposed, - * complex conjugate or Hermitian, and therefore of upper triangular form (Fig. 13.6). - * This is true because of the special case of A being a square, conjugate symmetric matrix. - */ - - def run(matrix: Vector[Vector[Int]]): Unit = { - - val size: Int = matrix.size - val lower: ArrayBuffer[ArrayBuffer[Int]] = ArrayBuffer[ArrayBuffer[Int]]() - - for - i <- 0 to size - j <- 0 until i - do - if i == j then lower(i)(j) = getSquaredSummation(lower, i, j, matrix) else lower(j)(j) = getReversedSummation(lower, i, j, matrix) - - } - - private def getReversedSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { - math.sqrt(matrix(j)(j) - (0 until j).map { k => lower(i)(k) * lower(j)(k) }.sum).toInt - } - private def getSquaredSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { - ((matrix(i)(j) - (0 until j).map { k => math.pow(lower(j)(k), 2)}.sum) / lower(j)(j)).toInt - } -} - diff --git a/src/main/scala/main/Ops/Resources.scala b/src/main/scala/main/Ops/Resources.scala deleted file mode 100644 index cd9cade..0000000 --- a/src/main/scala/main/Ops/Resources.scala +++ /dev/null @@ -1,36 +0,0 @@ -package main.Resources - -import main.Ops.* -import main.Tests.* -import main.Tools.* -import oshi._ -import zio._ - -class Resources { - - val si: SystemInfo = new SystemInfo - val sensors = si.getHardware.getSensors - val cpu = si.getHardware.getProcessor - - def failSafe: Unit = while (true) do if sensors.getCpuTemperature > 80 then println("overheat") - - def getPlatform: ZIO[Any, Throwable, Unit] = { - ZIO.attempt { - println(si.getHardware) - }.catchAll { error => Console.printError(s"failed :$error")} - } - - - def getCpuFrequency: ZIO[Any, Throwable, Unit] = { - ZIO.attempt { - /* - * 227 oshi/hardware/CentralProcessor.java - * method takes long value as delay - * */ - // println("load: " + cpu.getSystemCpuLoad(1000) * 1000) - // println("logical cores: " + cpu.getLogicalProcessorCount()) - // println("cores: " + cpu.getPhysicalProcessorCount()) - // println("temperature: " + sensors.getCpuTemperature()) - } - } -} diff --git a/src/main/scala/main/Tests/Tests.scala b/src/main/scala/main/Tests/Tests.scala deleted file mode 100644 index 93c21a6..0000000 --- a/src/main/scala/main/Tests/Tests.scala +++ /dev/null @@ -1,14 +0,0 @@ -package main.Tests - -import main.Ops.* -import main.Tools.* -import zio._ -import scala.collection.immutable.ListSet - -class Test { - - def run(): Unit = { - - - } -} diff --git a/src/main/scala/main/Tools/Benchmark.scala b/src/main/scala/main/Tools/Benchmark.scala deleted file mode 100644 index 04b9861..0000000 --- a/src/main/scala/main/Tools/Benchmark.scala +++ /dev/null @@ -1,46 +0,0 @@ -package main.Tools - -import main.Ops.* -import main.Tests.* -import main.Tools.* -import oshi._ -import java.time.Instant - -class Benchmark { - /* - * Calculate the time between the start of the execution of the function and the end - * */ - def measureTime(work: => Unit): Long = { - - val start = System.nanoTime() - work - val end = System.nanoTime() - end - start - } - - // TODO: map this to an actual precision value - def measurePrecision(work: => Boolean, expectedResult: Boolean): Unit = if work == expectedResult then println(true) else println(false) -} - - -class PrimeRunner { - - - def run(threads: Int): Unit = { - - val pr = new Prime() - val br = new Benchmark() - - /* - * test cases - * - * 7919 true - * 2147483647 false - */ - - val time = pr.run(7919, true) - println(time) - - } -} - diff --git a/src/main/scala/main/Tools/Runner.scala b/src/main/scala/main/Tools/Runner.scala deleted file mode 100644 index 384ec5d..0000000 --- a/src/main/scala/main/Tools/Runner.scala +++ /dev/null @@ -1,42 +0,0 @@ -package main.Runner - -import main.Ops.* -import main.Tests.* -import main.Tools.* -import oshi._ -import zio._ - -class Runner { - - val p = new Prime - val cd = new CholeskyDecomposition - - - def runCholeskyTest: ZIO[Any, Throwable, Unit] = { - ZIO.attempt { - //TODO: declare a randomized array to pass into the runner - // cd.run(matrix) - }.catchAll { error => Console.printError(s"failed: $error")} - } - - def runPrimeTest: ZIO[Any, Throwable, Unit] = { - ZIO.attempt { - // p.run(390483094, true) - }.catchAll { error => Console.printError(s"failed: $error")} - } - - def runSequential: ZIO[Any, Throwable, Unit] = { - - Console.printLine("sequential test") *> - runCholeskyTest *> - runPrimeTest - } - - def runParallel: ZIO[Any, Throwable, Unit] = { - - Console.printLine("CholeskyTest") *> - (runCholeskyTest <&> runPrimeTest).unit - } - -} - diff --git a/src/main/scala/main/domain/CpuOperations.scala b/src/main/scala/main/domain/CpuOperations.scala new file mode 100644 index 0000000..ebc5c9b --- /dev/null +++ b/src/main/scala/main/domain/CpuOperations.scala @@ -0,0 +1,118 @@ +package main.domain + +import main.services._ + +import scala.util.hashing +import scala.util.hashing.MurmurHash3 +import scala.math._ +import scala.collection.immutable.ListSet +import scala.collection.mutable.ArrayBuffer + + +trait CPU { + + +} + + + /* + * Calculate all primes up to limit + * This should stress the ALU in someway, + * doing this in a predictable manner, + * will hopefully keep the cpu pipeline busy + * and that way stress the branch predictor + * + * math.sqrt(n) => a prime number has 2 factors, one of the factors + * of the prime numbers has to be smaller then n + * after that we check if the number is whole number and thereby checking if its a prime + * + */ + + + /* + * 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 + */ + +class Prime extends CPU { + + def run(n: Int): Unit = { + for i <- 0 to n do if isPrime(i) == true then println("true") else println("false") + } + + private def isPrime(n: Int): Boolean = { + if n <= 1 then false + else !(2 to math.sqrt(n).toInt).exists(i => n % i == 0) + } + +} + + +/* TODO: implement ALU friendly, so high speed hashing + * to continuously loop over voor stressing + * ALU + * + * While looking for hashing algorithmes to implement I stumbled on: + * https://scala-lang.org/api/3.x/scala/util/hashing/MurmurHash3$.html + * + * which is an implemntation of **smasher** http://github.com/aappleby/smhasher + * the exact type of hashing algorithm I was looking for + * + * In the scala description they state: "This algorithm is designed to generate + * well-distributed non-cryptographic hashes. It is designed to hash data in 32 bit chunks (ints). " + * + * (ints) -> ALU + * + */ + +class Hash extends CPU { + + def run(n: Int): Unit = { + + for i <- 0 to n do MurmurHash3.stringHash("a random string to hash") + + } +} + +/* + * Floating point operation to stress the cpu + * Calculate the number of KFLOPS / FLOPS + * implementation of the Cholesky decomposition + * More information on the Cholesky decomposition at: + * https://en.wikipedia.org/wiki/Cholesky_decomposition + * + * Linpack uses the cholesky decomposition + * https://www.netlib.org/linpack/ + * + * https://www.geeksforgeeks.org/dsa/cholesky-decomposition-matrix-decomposition/ + * + * The Cholesky decomposition maps matrix A into the product of A = L · LH where L is the lower triangular matrix and LH is the transposed, + * complex conjugate or Hermitian, and therefore of upper triangular form (Fig. 13.6). + * This is true because of the special case of A being a square, conjugate symmetric matrix. + */ + +class CholeskyDecomposition extends CPU { + + + def run(matrix: Vector[Vector[Int]]): Unit = { + + val size: Int = matrix.size + val lower: ArrayBuffer[ArrayBuffer[Int]] = ArrayBuffer[ArrayBuffer[Int]]() + + for + i <- 0 to size + j <- 0 until i + do + if i == j then lower(i)(j) = getSquaredSummation(lower, i, j, matrix) else lower(j)(j) = getReversedSummation(lower, i, j, matrix) + + } + + private def getReversedSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { + math.sqrt(matrix(j)(j) - (0 until j).map { k => lower(i)(k) * lower(j)(k) }.sum).toInt + } + private def getSquaredSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { + ((matrix(i)(j) - (0 until j).map { k => math.pow(lower(j)(k), 2)}.sum) / lower(j)(j)).toInt + } +} + + diff --git a/src/main/scala/main/domain/MemoryOperations.scala b/src/main/scala/main/domain/MemoryOperations.scala new file mode 100644 index 0000000..464d0bc --- /dev/null +++ b/src/main/scala/main/domain/MemoryOperations.scala @@ -0,0 +1,40 @@ +package main.domain + +import main.services._ + +import scala.util.hashing +import scala.util.hashing.MurmurHash3 +import scala.math._ +import scala.collection.immutable.ListSet +import scala.collection.mutable.ArrayBuffer + +/** + * + * Large memory object to allocate on the heap + * This is an implementation by me + * + * */ + +sealed Trait MemoryObject { + + +} + +class MemoryAllocater { + + /* + * + * for stressing the memory we want to do some heavy heap allocations back and forth + * + * */ + + def run(): Unit = println("stressing memory") + + + def moveToMemory(): Unit ={ + + } +} + + + diff --git a/src/main/scala/main/infrastructure/Http.scala b/src/main/scala/main/infrastructure/Http.scala new file mode 100644 index 0000000..cf9a6e1 --- /dev/null +++ b/src/main/scala/main/infrastructure/Http.scala @@ -0,0 +1,23 @@ +package main.infrastructure + +import zio._ +import zio.http._ + +/** + * HTTP Handler + * using ZIO + * + * + * source: https://ziohttp.com */ + +object Greeter extends ZIOAppDefault { + val routes = + Routes( + Method.GET / "stability" -> handler { (req: Request) => + val name = req.queryOrElse("", "World") + Response.text("stable!") + } + ) + + def run = Server.serve(routes).provide(Server.default) +} diff --git a/src/main/scala/main/infrastructure/Resources.scala b/src/main/scala/main/infrastructure/Resources.scala new file mode 100644 index 0000000..f4e88c2 --- /dev/null +++ b/src/main/scala/main/infrastructure/Resources.scala @@ -0,0 +1,36 @@ +package main.infrastructure + +import main.domain._ +import main.services._ +import main.infrastructure._ +import oshi._ +import zio._ + +class Resources { + + val si: SystemInfo = new SystemInfo + val sensors = si.getHardware.getSensors + val cpu = si.getHardware.getProcessor + + def failSafe: Unit = while (true) do if sensors.getCpuTemperature > 80 then println("overheat") + + def getPlatform: ZIO[Any, Throwable, Unit] = { + ZIO.attempt { + println(si.getHardware) + }.catchAll { error => Console.printError(s"failed :$error")} + } + + + def getCpuFrequency: ZIO[Any, Throwable, Unit] = { + ZIO.attempt { + /* + * 227 oshi/hardware/CentralProcessor.java + * method takes long value as delay + * */ + // println("load: " + cpu.getSystemCpuLoad(1000) * 1000) + // println("logical cores: " + cpu.getLogicalProcessorCount()) + // println("cores: " + cpu.getPhysicalProcessorCount()) + // println("temperature: " + sensors.getCpuTemperature()) + } + } +} diff --git a/src/main/scala/main/services/Benchmark.scala b/src/main/scala/main/services/Benchmark.scala new file mode 100644 index 0000000..59c81e9 --- /dev/null +++ b/src/main/scala/main/services/Benchmark.scala @@ -0,0 +1,23 @@ +package main.services + +import main.domain._ +import java.time.Instant +import oshi._ + +class Benchmark { + /* + * Calculate the time between the start of the execution of the function and the end + * */ + def measureTime(work: => Unit): Long = { + + val start = System.nanoTime() + work + val end = System.nanoTime() + end - start + } + + // TODO: map this to an actual precision value + def measurePrecision(work: => Boolean, expectedResult: Boolean): Unit = if work == expectedResult then println(true) else println(false) +} + + diff --git a/src/main/scala/main/services/Stress.scala b/src/main/scala/main/services/Stress.scala new file mode 100644 index 0000000..3709378 --- /dev/null +++ b/src/main/scala/main/services/Stress.scala @@ -0,0 +1,43 @@ +package main.services + +import main.domain._ +import main.services._ +import oshi._ +import zio._ + + +enum Status: + case PASS + case FAIL + +class Stress(val status: Status) { + + + def runCholeskyTest: ZIO[Any, Throwable, Unit] = { + ZIO.attempt { + //TODO: declare a randomized array to pass into the runner + // cd.run(matrix) + }.catchAll { error => Console.printError(s"failed: $error")} + } + + def runPrimeTest: ZIO[Any, Throwable, Unit] = { + ZIO.attempt { + // p.run(390483094, true) + }.catchAll { error => Console.printError(s"failed: $error")} + } + + def runSequential: ZIO[Any, Throwable, Unit] = { + + Console.printLine("sequential test") *> + runCholeskyTest *> + runPrimeTest + } + + def runParallel: ZIO[Any, Throwable, Unit] = { + + Console.printLine("CholeskyTest") *> + (runCholeskyTest <&> runPrimeTest).unit + } + +} + -- cgit v1.2.3-70-g09d2