rudimentary framerate benchmarking (turning it on will make the game run significantly slower)

This commit is contained in:
minjaesong
2022-10-15 01:09:51 +09:00
parent 8533f92274
commit 93af194c8a
6 changed files with 44 additions and 4 deletions

View File

@@ -112,6 +112,8 @@ object DefaultConfig {
"fx_newlight" to false,
"debug_key_deltat_benchmark" to Input.Keys.SLASH
// settings regarding debugger

View File

@@ -19,6 +19,7 @@ import net.torvald.terrarum.modulebasegame.ui.UITooltip
import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.savegame.VirtualDisk
import net.torvald.terrarum.ui.ConsoleWindow
import net.torvald.util.CircularArray
import net.torvald.util.SortedArrayList
import org.khelekore.prtree.*
import java.io.File
@@ -78,6 +79,8 @@ open class IngameInstance(val batch: FlippingSpriteBatch, val isMultiplayer: Boo
open var uiTooltip: UITooltip = UITooltip()
open var notifier: Notification = Notification()
val deltaTeeBenchmarks = CircularArray<Float>(1024, true)
init {
consoleHandler.setPosition(0, 0)
notifier.setPosition(

View File

@@ -12,7 +12,6 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.JsonReader
import com.jme3.math.FastMath
import net.torvald.unsafe.UnsafeHelper
import net.torvald.gdx.graphics.Cvec
import net.torvald.random.HQRNG
import net.torvald.terrarum.App.*
@@ -33,6 +32,7 @@ import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.worlddrawer.WorldCamera
import net.torvald.terrarumsansbitmap.gdx.TerrarumSansBitmap
import net.torvald.unsafe.UnsafeHelper
import net.torvald.util.CircularArray
import java.io.File
import java.io.PrintStream
@@ -114,7 +114,7 @@ object Terrarum : Disposable {
private val javaHeapCircularArray = CircularArray<Int>(64, true)
private val nativeHeapCircularArray = CircularArray<Int>(64, true)
private val updateRateCircularArray = CircularArray<Double>(16, true)
private val updateRateCircularArray = CircularArray<Double>(64, true)
val memJavaHeap: Int
get() {
@@ -133,7 +133,7 @@ object Terrarum : Disposable {
val updateRateStr: String
get() {
updateRateCircularArray.appendHead(updateRate)
return String.format("%.2f", updateRateCircularArray.maxOrNull() ?: 0.0)
return String.format("%.2f", updateRateCircularArray.average())
}
lateinit var testTexture: Texture

View File

@@ -153,6 +153,26 @@ object TerrarumPostProcessor : Disposable {
App.fontGame.draw(it, thisIsDebugStr, 5f, App.scr.height - 24f)
}
}
if (KeyToggler.isOn(App.getConfigInt("debug_key_deltat_benchmark"))) {
batch.color = Toolkit.Theme.COL_ACTIVE
batch.inUse {
// we're going to assume the data are normally distributed
val benchstr = if (INGAME.deltaTeeBenchmarks.elemCount < INGAME.deltaTeeBenchmarks.size) {
"ΔF: Gathering data (${INGAME.deltaTeeBenchmarks.elemCount}/${INGAME.deltaTeeBenchmarks.size})"
}
else {
val tallies = INGAME.deltaTeeBenchmarks.toList()
val average = tallies.average()
val stdev = (tallies.sumOf { (it - average).sqr() } / tallies.size).sqrt()
val low5 = average + stdev * -1.64
val low1 = average + stdev * -2.33
"ΔF: Avr ${average.format(1)}; 95% ${low5.format(1)}; 99% ${low1.format(1)}; σ ${stdev.format(1)}"
}
val tw = App.fontGame.getWidth(benchstr)
App.fontGame.draw(it, benchstr, Toolkit.drawWidth - tw - 5f, App.scr.height - 24f)
}
}
}
}
@@ -160,6 +180,8 @@ object TerrarumPostProcessor : Disposable {
}
private val rng = HQRNG()
private fun Double.format(digits: Int) = "%.${digits}f".format(this)
private val swizzler = intArrayOf(
1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1,
1,0,0,0, 0,1,0,0, 0,0,0,1, 0,0,1,0,

View File

@@ -581,7 +581,7 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
// some sketchy test code here
KeyToggler.forceSet(App.getConfigInt("debug_key_deltat_benchmark"), false)
}// END enter
@@ -819,6 +819,10 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
WORLD_UPDATE_TIMER += 1
if (KeyToggler.isOn(App.getConfigInt("debug_key_deltat_benchmark"))) {
deltaTeeBenchmarks.appendHead(1f / Gdx.graphics.deltaTime)
}
}

View File

@@ -80,6 +80,9 @@ class CircularArray<T>(val size: Int, val overwriteOnOverflow: Boolean): Iterabl
}
}
/**
* To just casually add items to the list, use [appendHead], **please!**
*/
fun appendTail(item: T) {
// even if overflowing is enabled, appending at tail causes head element to be altered, therefore such action
// must be blocked by throwing overflow error
@@ -183,6 +186,12 @@ class CircularArray<T>(val size: Int, val overwriteOnOverflow: Boolean): Iterabl
return accumulator
}
fun toList(): List<T> {
val list = ArrayList<T>()
iterator().forEach { list.add(it) }
return list.toList()
}
private inline fun Int.wrap() = this fmod size
override fun toString(): String {