Skip to content

Commit

Permalink
refactor: Add back benchmarks module
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Oct 31, 2024
1 parent e4f44e6 commit 9952033
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 76 deletions.
5 changes: 5 additions & 0 deletions geary-benchmarks/src/main/kotlin/GearyBenchmark.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary

abstract class GearyBenchmark : Geary by geary(TestEngineModule).start()
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package com.mineinabyss.geary.benchmarks

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.oneMil
import com.mineinabyss.geary.benchmarks.helpers.tenMil
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.systems.query.Query
import com.mineinabyss.geary.systems.builders.system
import com.mineinabyss.geary.systems.query.query
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State

@State(Scope.Benchmark)
class VelocitySystemBenchmark {
class VelocitySystemBenchmark : GearyBenchmark() {
data class Velocity(val x: Float, val y: Float)
data class Position(var x: Float, var y: Float)

fun createVelocitySystem() = geary.system(object : Query() {
fun createVelocitySystem() = system(object : Query(this) {
val velocity by get<Velocity>()
var position by get<Position>()
}).exec {
it.position.x += it.velocity.x
it.position.y += it.velocity.y
}
fun createVelocitySystemNoDelegates() = geary.system(

fun createVelocitySystemNoDelegates() = system(
query<Velocity, Position>()
).exec { (velocity, position) ->
position.x += velocity.x
Expand All @@ -37,8 +36,6 @@ class VelocitySystemBenchmark {

@Setup
fun setUp() {
geary(TestEngineModule)

repeat(tenMil) {
entity {
set(Velocity(it.toFloat() / oneMil, it.toFloat() / oneMil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.systems.builders.observe
import com.mineinabyss.idofront.di.DI
import com.mineinabyss.geary.modules.observe
import org.openjdk.jmh.annotations.*

@State(Scope.Benchmark)
class EventCalls {
private class TestEvent

var targets = emptyList<Entity>()
var geary: Geary = geary(TestEngineModule).start()

@Setup(Level.Invocation)
fun setupPerInvocation() {
geary(TestEngineModule)
targets = (1..oneMil).map { entity().apply { set(it) } }
geary = geary(TestEngineModule).start()
targets = (1..oneMil).map { geary.entity().apply { set(it) } }
createListener()
}

@TearDown(Level.Invocation)
fun teardown() {
DI.clear()
}

var count = 0

fun createListener() = geary.observe<TestEvent>().exec {
Expand All @@ -40,7 +35,6 @@ class EventCalls {
targets[it].emit<TestEvent>()
}
}

}

fun main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package com.mineinabyss.geary.benchmarks.instantiation
import co.touchlab.kermit.Logger
import co.touchlab.kermit.Severity
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.helpers.toGeary
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.idofront.di.DI
import org.openjdk.jmh.annotations.*

@State(Scope.Benchmark)
Expand All @@ -16,18 +15,15 @@ class ManyComponentsBenchmark {
Logger.setMinSeverity(Severity.Warn)
}

var geary: Geary = geary(TestEngineModule).start()

@Setup(Level.Invocation)
fun setupPerInvocation() {
geary(TestEngineModule)
}

@TearDown(Level.Invocation)
fun teardown() {
DI.clear()
geary = geary(TestEngineModule).start()
}

@Benchmark
fun createTenThousandEntitiesWithUniqueComponentEach() {
fun createTenThousandEntitiesWithUniqueComponentEach() = with(geary) {
repeat(10000) {
entity {
addRelation<String>(it.toLong().toGeary())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,34 @@ import co.touchlab.kermit.Severity
import com.mineinabyss.geary.benchmarks.helpers.*
import com.mineinabyss.geary.helpers.componentId
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.idofront.di.DI
import org.openjdk.jmh.annotations.*

@State(Scope.Benchmark)
class NewEntityBenchmark {
var geary: Geary = geary(TestEngineModule).start()

@Setup
fun setLoggingLevel() {
Logger.setMinSeverity(Severity.Warn)
}

@Setup(Level.Invocation)
fun setupPerInvocation() {
geary(TestEngineModule)
}

@TearDown(Level.Invocation)
fun teardown() {
DI.clear()
geary = geary(TestEngineModule).start()
}

@Benchmark
fun create1MilEntitiesWith0Components() {
fun create1MilEntitiesWith0Components() = with(geary) {
repeat(oneMil) {
entity()
}
}

@Benchmark
fun create1MilEntitiesWith1ComponentNoEvent() {
fun create1MilEntitiesWith1ComponentNoEvent() = with(geary) {
repeat(oneMil) {
entity {
set(Comp1(0), noEvent = true)
Expand All @@ -44,7 +41,7 @@ class NewEntityBenchmark {
}

@Benchmark
fun create1MilEntitiesWith1ComponentYesEvent() {
fun create1MilEntitiesWith1ComponentYesEvent() = with(geary) {
repeat(oneMil) {
entity {
set(Comp1(0))
Expand All @@ -53,7 +50,7 @@ class NewEntityBenchmark {
}

@Benchmark
fun create1MilEntitiesWith6Components() {
fun create1MilEntitiesWith6Components() = with(geary) {
repeat(oneMil) {
entity {
set(Comp1(0), noEvent = true)
Expand All @@ -67,7 +64,7 @@ class NewEntityBenchmark {
}

@Benchmark
fun create1MilEntitiesWith6ComponentsWithoutComponentIdCalls() {
fun create1MilEntitiesWith6ComponentsWithoutComponentIdCalls() = with(geary) {
val comp1Id = componentId<Comp1>()
val comp2Id = componentId<Comp2>()
val comp3Id = componentId<Comp3>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.geary.benchmarks.misc

import GearyBenchmark
import co.touchlab.kermit.Logger
import co.touchlab.kermit.Severity
import com.mineinabyss.geary.benchmarks.helpers.*
Expand All @@ -13,7 +14,8 @@ import org.openjdk.jmh.annotations.State
import kotlin.reflect.typeOf

@State(Scope.Benchmark)
class ComponentIdTest {
class ComponentIdTest : GearyBenchmark() {

@Setup
fun setup() {
Logger.setMinSeverity(Severity.Warn)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.mineinabyss.geary.benchmarks.unpacking

import com.mineinabyss.geary.benchmarks.helpers.*
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.systems.builders.cache
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.systems.query.GearyQuery

class Query1 : GearyQuery() {
class Query1(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>()
}

class Query1Defaulting : GearyQuery() {
class Query1Defaulting(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>().orDefault { Comp1(0) }
override fun ensure() = this { has<Comp1>()}
override fun ensure() = this { has<Comp1>() }
}

class Query2 : GearyQuery() {
class Query2(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>()
val comp2 by get<Comp2>()
}

class Query6 : GearyQuery() {
class Query6(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>()
val comp2 by get<Comp2>()
val comp3 by get<Comp3>()
Expand All @@ -29,7 +28,7 @@ class Query6 : GearyQuery() {
}


class Query6WithoutDelegate : GearyQuery() {
class Query6WithoutDelegate(world: Geary) : GearyQuery(world) {
val comp1 = get<Comp1>()
val comp2 = get<Comp2>()
val comp3 = get<Comp3>()
Expand All @@ -47,8 +46,8 @@ class Query6WithoutDelegate : GearyQuery() {
}
}

fun systemOf1() = geary.cache(Query1())
fun systemOf1Defaulting() = geary.cache(Query1Defaulting())
fun systemOf2() = geary.cache(Query2())
fun systemOf6() = geary.cache(Query6())
fun systemOf6WithoutDelegate() = geary.cache(Query6WithoutDelegate())
fun Geary.systemOf1() = cache(::Query1)
fun Geary.systemOf1Defaulting() = cache(::Query1Defaulting)
fun Geary.systemOf2() = cache(::Query2)
fun Geary.systemOf6() = cache(::Query6)
fun Geary.systemOf6WithoutDelegate() = cache(::Query6WithoutDelegate)
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package com.mineinabyss.geary.benchmarks.unpacking

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.Comp1
import com.mineinabyss.geary.benchmarks.helpers.tenMil
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State

@State(Scope.Benchmark)
class Unpack1Benchmark {
class Unpack1Benchmark : GearyBenchmark() {

@Setup
fun setUp() {
geary(TestEngineModule)

repeat(tenMil) {
entity {
set(Comp1(1))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package com.mineinabyss.geary.benchmarks.unpacking

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.Comp1
import com.mineinabyss.geary.benchmarks.helpers.Comp2
import com.mineinabyss.geary.benchmarks.helpers.tenMil
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State

@State(Scope.Benchmark)
class Unpack2Benchmark {
class Unpack2Benchmark : GearyBenchmark() {
@Setup
fun setUp() {
geary(TestEngineModule) {
}

repeat(tenMil) {
entity {
set(Comp1(1))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.mineinabyss.geary.benchmarks.unpacking

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.*
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State

@State(Scope.Benchmark)
class Unpack6Benchmark {
class Unpack6Benchmark : GearyBenchmark() {

@Setup
fun setUp() {
geary(TestEngineModule)

repeat(tenMil) {
entity {
set(Comp1(0))
Expand Down Expand Up @@ -65,7 +62,6 @@ class Unpack6Benchmark {
@Benchmark
fun unpack6of6CompNoDelegate() {
systemOf6WithoutDelegate().forEach {

it.comp1.get(it)
it.comp2.get(it)
it.comp3.get(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.geary.datatypes

import androidx.collection.MutableLongList
import com.mineinabyss.geary.modules.Geary

typealias EntityIdArray = ULongArray
Expand All @@ -8,6 +9,10 @@ fun EntityIdArray.toEntityArray(world: Geary): EntityArray {
return EntityArray(world, this)
}

fun MutableLongList.toEntityArray(world: Geary): EntityArray {
return EntityArray(world, ULongArray(size) { get(it).toULong() })
}

class EntityArray(
val world: Geary,
val ids: EntityIdArray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun Entity.removeChild(child: Entity) {

/** Removes all of this entity's children, also unlinking this parent from them. */
fun Entity.clearChildren() {
children.fastForEach { remove(it.id) }
children.forEach { remove(it.id) }
}

/** Gets the first parent of this entity */
Expand Down
Loading

0 comments on commit 9952033

Please sign in to comment.