From 8c344b381254094c0db5504794997b86581b4ca3 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 11 Dec 2023 19:07:31 +0100 Subject: [PATCH] samples: add more metrics to software3D --- .../samples/software3d/swing/swingMain.kt | 2 +- .../teavm/samples/software3d/teavm/main.kt | 29 +++++++++++++++++++ samples/software3d/src/main/webapp/index.html | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/swing/swingMain.kt b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/swing/swingMain.kt index 57edfee00..d75e5f8f1 100644 --- a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/swing/swingMain.kt +++ b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/swing/swingMain.kt @@ -47,7 +47,7 @@ fun main() { } val (scene, updater) = geometry() - val taskCount = 1// Runtime.getRuntime().availableProcessors() + val taskCount = Runtime.getRuntime().availableProcessors() println("Running on $taskCount CPUs") val rasters = (0 until taskCount).map { Raster(SCENE_WIDTH, (SCENE_HEIGHT + taskCount - 1) / taskCount) } val renderers = rasters.mapIndexed { index, raster -> diff --git a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt index a4d5a2860..f51796bbf 100644 --- a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt +++ b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt @@ -16,6 +16,7 @@ package org.teavm.samples.software3d.teavm +import org.teavm.jso.browser.Window import org.teavm.jso.dom.html.HTMLDocument import org.teavm.jso.dom.html.HTMLElement import org.teavm.jso.dom.html.HTMLOptionElement @@ -34,20 +35,29 @@ fun main(args: Array) { fun runController() { val performanceIndicator = HTMLDocument.current().getElementById("performance-indicator") + val performanceIndicatorWorkerAverage = HTMLDocument.current().getElementById( + "performance-indicator-worker-average") var performanceIndicatorByWorkers: List = emptyList() val maxWorkers = Runtime.getRuntime().availableProcessors() var workerType = WorkerType.JS + val performanceByWorkers = LongArray(maxWorkers) + var workerCount = maxWorkers + val controller = Controller(SCENE_WIDTH, SCENE_HEIGHT) { index, value -> if (index == -1) { performanceIndicator.innerText = value.toString() } else { + performanceByWorkers[index] = value performanceIndicatorByWorkers.getOrNull(index)?.let { it.innerText = value.toString() } + val average = performanceByWorkers.slice(0 until workerCount).average().toLong() + performanceIndicatorWorkerAverage.innerText = average.toString() } } performanceIndicatorByWorkers = recreatePerformanceIndicators(maxWorkers) controller.startRendering(maxWorkers, workerType) + startTimer() val cpuSelector = HTMLDocument.current().getElementById("workers") as HTMLSelectElement for (i in 1..maxWorkers) { @@ -61,7 +71,9 @@ fun runController() { val newValue = cpuSelector.value.toInt() if (controller.tasks != newValue) { controller.startRendering(newValue, workerType) + startTimer() performanceIndicatorByWorkers = recreatePerformanceIndicators(newValue) + workerCount = newValue } } @@ -75,10 +87,27 @@ fun runController() { if (workerType != newValue) { workerType = newValue controller.startRendering(controller.tasks, workerType) + startTimer() } } } +var currentTimeout: Int? = null + +private fun startTimer() { + currentTimeout?.let { Window.clearTimeout(it) } + val timerDisplay = HTMLDocument.current().getElementById("timer") + val startTime = System.currentTimeMillis() + var seconds = 0 + fun updateTimer() { + val time = System.currentTimeMillis() - startTime + val nextTime = (time / 1000) * 1000 + 1000 + timerDisplay.innerText = seconds++.toString() + currentTimeout = Window.setTimeout({ updateTimer() }, (nextTime - time).toInt()) + } + updateTimer() +} + private fun recreatePerformanceIndicators(count: Int): List { val container = HTMLDocument.current().getElementById("performance-indicators-by-workers") while (container.hasChildNodes()) { diff --git a/samples/software3d/src/main/webapp/index.html b/samples/software3d/src/main/webapp/index.html index 75bad6de6..5c818feb4 100644 --- a/samples/software3d/src/main/webapp/index.html +++ b/samples/software3d/src/main/webapp/index.html @@ -31,6 +31,8 @@
Average frame rendering time, microseconds:
+
Average by worker:
+
Time passed:
By worker: