feature[benchmark]: time measurement benchmark

not a good way of testing things, but it's a way and the first way
This commit is contained in:
Abdellah El Morabit 2025-11-20 18:45:53 +01:00
parent 4bb2ff6828
commit 421c6c3c93
4 changed files with 37 additions and 24 deletions

View File

@ -4,24 +4,23 @@ object Torque {
import java.time.Instant
import com.nsrddyn.alu.*
import com.nsrddyn.tools.Benchmark
@main def main(args: String*): Unit =
val pr = new Prime()
val br = new Benchmark()
// ANSI ESCAPE CODE: clear screen
println("\u001b[2J\u001b[H")
println("--- TORQUE STRESS TESTING UTILITY ---")
val now: Instant = Instant.now()
println(now)
// val value = 2147483647
val value = 200000
val pr = new Prime()
/*
val intMax = 2147483647
pr.run(intMax)
*/
val intMax = 2147483647
println(pr.measure())
val time = br.measure(pr.run(value))
println(time)
}

View File

@ -1,9 +1,9 @@
package com.nsrddyn.alu
import com.nsrddyn.tools.Measurable
import com.nsrddyn.tools.Benchmark
class Prime() extends Measurable:
class Prime() extends Benchmark:
/*
* Calculate all primes up to limit
@ -24,17 +24,22 @@ class Prime() extends Measurable:
* We want the function to be less optimized so that the CPU has more work == more stress
*/
def isPrime(n: Int): Boolean = {
val start = measure()
if n <= 1 then false
else !(2 to math.sqrt(n).toInt).exists(i => n % i == 0)
}
def run(n: Int): Unit = for i <- 0 to n do isPrime(i)
def measure(): Long = {
// TODO: implement measure methode to measure the time that it takes to find that prime number
System.nanoTime()
def measure(): Long ={
val start = System.nanoTime()
System.nanoTime()
val end = System.nanoTime()
start - end
}

View File

@ -0,0 +1,14 @@
package com.nsrddyn.tools
class Benchmark {
// add a reference to a function
def measure(work: => Unit): Long = {
val start = System.nanoTime()
work
val end = System.nanoTime()
end - start
}
}

View File

@ -1,5 +0,0 @@
package com.nsrddyn.tools
trait Measurable {
def measure(): Long
}