Last active
December 19, 2016 15:48
-
-
Save izmailoff/4aaa085a6240be167cfad511f6036d1e to your computer and use it in GitHub Desktop.
Estimate number of cores/threads your system can scale to aka cpu count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
exec scala -Dscala.concurrent.context.minThreads=16 -Dscala.concurrent.context.maxThreads=64 "$0" "$@" | |
!# | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
import java.util.concurrent.Executors | |
import scala.concurrent._ | |
def time[R](block: => R): Long = { | |
val t0 = System.nanoTime() | |
val result = block | |
val t1 = System.nanoTime() | |
t1 - t0 | |
} | |
def slowFunction = { | |
var i = 0L | |
while(i < Long.MaxValue / 1024 / 1024 / 1024) { | |
i += 1 | |
//math.sqrt(i) | |
} | |
} | |
val maxCpuCount = 64 | |
val errorMargin = 1.1 | |
val warmupTimes = 10 | |
//implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(maxCpuCount)) | |
//implicit val ec = ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor) | |
def estimate_cpu_count(concurrencyLevel: Int = 1, firstTimingNanos: Long = 0): Int = { | |
val elapsed = time(Await.result(Future.sequence(List.fill(concurrencyLevel)(Future{slowFunction})),Duration.Inf)) | |
val expected = (if(firstTimingNanos == 0) elapsed else firstTimingNanos) | |
val maxExpected = (expected * errorMargin).toLong | |
println(s"threads: $concurrencyLevel, (elapsed)$elapsed >? ${maxExpected}(max expected for this level)," + | |
s" elapsed/max: ${elapsed*100/maxExpected}%") | |
if(elapsed > maxExpected) | |
concurrencyLevel | |
else | |
estimate_cpu_count(concurrencyLevel + 1, expected) | |
} | |
def warmup() = (1 to warmupTimes).foreach(_ => slowFunction) | |
println(s"Warmup started, running $warmupTimes times before the actual test...") | |
warmup() | |
println("Warmup finished.") | |
println("Estimating CPU count...") | |
val estimatedCores = estimate_cpu_count() | |
val runtimeCores = Runtime.getRuntime.availableProcessors | |
println(s"Estimated cores: $estimatedCores, runtime cores: $runtimeCores") |
hyperthreading is enabled, 1 CPU, 4 cores, 8 threads, i7 various generations
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another machine (both i7 but different gen):