mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 00:32:35 -08:00
Velocity optimisations
This commit is contained in:
1 parent
191f1004f1
commit
0b235e6669
5 files changed
+61
-60
No files matched your search
@@ -9,7 +9,6 @@ import dev.slimevr.buildTestSkeleton
|
||||
import dev.slimevr.buildTestUserConfig
|
||||
import dev.slimevr.buildTestVrServer
|
||||
import dev.slimevr.context.Context
|
||||
import dev.slimevr.skeleton.Skeleton
|
||||
import dev.slimevr.solarxr.datafeed.DataFeedInitBehaviour
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
@@ -22,13 +21,9 @@ import solarxr_protocol.data_feed.PollDataFeed
|
||||
import solarxr_protocol.data_feed.StartDataFeed
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
|
||||
private class TestConn(val bridge: SolarXRBridge, val skeleton: Skeleton)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
private fun TestScope.testConn(onSend: suspend (ByteArray) -> Unit): TestConn {
|
||||
private fun TestScope.testConn(onSend: suspend (ByteArray) -> Unit): SolarXRBridge {
|
||||
val server = buildTestVrServer(backgroundScope)
|
||||
val skeleton = buildTestSkeleton(backgroundScope)
|
||||
val settings = buildTestSettings(backgroundScope)
|
||||
@@ -61,26 +56,11 @@ private fun TestScope.testConn(onSend: suspend (ByteArray) -> Unit): TestConn {
|
||||
// launchIn registers the handler synchronously, but the dispatcher's own drain loop still has to
|
||||
// start before anything emitted here can reach it.
|
||||
runCurrent()
|
||||
return TestConn(bridge, skeleton)
|
||||
return bridge
|
||||
}
|
||||
|
||||
private fun config(intervalMs: Int) = DataFeedConfig(minimumTimeSinceLast = intervalMs.toUShort())
|
||||
|
||||
/** Drives [Skeleton.computed] the way the 500Hz loop does, so the feed's gate has a clock to ride */
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
private fun TestScope.tickSkeleton(conn: TestConn, duration: Duration, tick: Duration = 2.milliseconds) {
|
||||
// Let a freshly started feed subscribe and take the replayed frame before the clock moves
|
||||
runCurrent()
|
||||
val frame = conn.skeleton.currentComputed
|
||||
var elapsed = Duration.ZERO
|
||||
while (elapsed < duration) {
|
||||
advanceTimeBy(tick)
|
||||
conn.skeleton.computed.tryEmit(frame)
|
||||
runCurrent()
|
||||
elapsed += tick
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class DataFeedTest {
|
||||
|
||||
@@ -89,10 +69,10 @@ class DataFeedTest {
|
||||
var sendCount = 0
|
||||
val conn = testConn { sendCount++ }
|
||||
|
||||
conn.bridge.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
conn.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
|
||||
// fires at t=0, t=100, t=200
|
||||
tickSkeleton(conn, 250.milliseconds)
|
||||
advanceTimeBy(250)
|
||||
assertEquals(3, sendCount)
|
||||
}
|
||||
|
||||
@@ -101,11 +81,11 @@ class DataFeedTest {
|
||||
var sendCount = 0
|
||||
val conn = testConn { sendCount++ }
|
||||
|
||||
conn.bridge.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100), config(200))))
|
||||
conn.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100), config(200))))
|
||||
|
||||
// 100ms feed: t=0, t=100, t=200 -> 3 sends
|
||||
// 200ms feed: t=0, t=200 -> 2 sends
|
||||
tickSkeleton(conn, 250.milliseconds)
|
||||
advanceTimeBy(250)
|
||||
assertEquals(5, sendCount)
|
||||
}
|
||||
|
||||
@@ -114,9 +94,9 @@ class DataFeedTest {
|
||||
var sendCount = 0
|
||||
val conn = testConn { sendCount++ }
|
||||
|
||||
conn.bridge.dataFeedDispatcher.emit(PollDataFeed(config = config(100)))
|
||||
conn.dataFeedDispatcher.emit(PollDataFeed(config = config(100)))
|
||||
|
||||
tickSkeleton(conn, 500.milliseconds)
|
||||
advanceTimeBy(500)
|
||||
assertEquals(1, sendCount)
|
||||
}
|
||||
|
||||
@@ -125,14 +105,14 @@ class DataFeedTest {
|
||||
var sendCount = 0
|
||||
val conn = testConn { sendCount++ }
|
||||
|
||||
conn.bridge.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
tickSkeleton(conn, 250.milliseconds)
|
||||
conn.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
advanceTimeBy(250)
|
||||
assertEquals(3, sendCount)
|
||||
|
||||
conn.bridge.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
conn.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
sendCount = 0
|
||||
|
||||
tickSkeleton(conn, 250.milliseconds)
|
||||
advanceTimeBy(250)
|
||||
assertEquals(3, sendCount)
|
||||
}
|
||||
|
||||
@@ -141,14 +121,14 @@ class DataFeedTest {
|
||||
var sendCount = 0
|
||||
val conn = testConn { sendCount++ }
|
||||
|
||||
conn.bridge.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
tickSkeleton(conn, 250.milliseconds)
|
||||
conn.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = listOf(config(100))))
|
||||
advanceTimeBy(250)
|
||||
assertEquals(3, sendCount)
|
||||
|
||||
conn.bridge.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = emptyList()))
|
||||
conn.dataFeedDispatcher.emit(StartDataFeed(dataFeeds = emptyList()))
|
||||
sendCount = 0
|
||||
|
||||
tickSkeleton(conn, 500.milliseconds)
|
||||
advanceTimeBy(500)
|
||||
assertEquals(0, sendCount)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user