1 module tests.access_perf; 2 3 import tests.runner; 4 5 import bubel.ecs.core; 6 import bubel.ecs.manager; 7 import bubel.ecs.entity; 8 9 version(GNU) 10 { 11 pragma(inline, true) T[n] staticArray(T, size_t n)(auto ref T[n] a) 12 { 13 return a; 14 } 15 } 16 else import std.array : staticArray; 17 18 import core.stdc.stdio; 19 20 struct CLong 21 { 22 mixin ECS.Component; 23 24 alias value this; 25 26 long value = 10; 27 } 28 29 struct CInt 30 { 31 mixin ECS.Component; 32 33 alias value this; 34 35 int value = 10; 36 } 37 38 struct CUInt 39 { 40 mixin ECS.Component; 41 42 alias value this; 43 44 uint value = 12; 45 } 46 47 struct CBig 48 { 49 mixin ECS.Component; 50 uint[32] data; 51 } 52 53 EntityTemplate* tmpl; 54 55 void beforeEveryTest() 56 { 57 gEntityManager.initialize(0); 58 59 gEntityManager.beginRegister(); 60 61 gEntityManager.registerComponent!CLong; 62 gEntityManager.registerComponent!CInt; 63 gEntityManager.registerComponent!CUInt; 64 gEntityManager.registerComponent!CBig; 65 66 gEntityManager.endRegister(); 67 68 tmpl = gEntityManager.allocateTemplate([becsID!CLong, becsID!CInt, becsID!CUInt, becsID!CBig].staticArray); 69 foreach(i; 0 .. 100_000)gEntityManager.addEntity(tmpl); 70 } 71 72 void afterEveryTest() 73 { 74 if(tmpl)gEntityManager.freeTemplate(tmpl); 75 tmpl = null; 76 gEntityManager.destroy(); 77 } 78 79 @("DirectAccess100k1comp") 80 unittest 81 { 82 foreach(i;0..25000) 83 { 84 Entity* entity = gEntityManager.getEntity(EntityID(i*4+1,0)); 85 CUInt* comp1 = entity.getComponent!CUInt; 86 comp1.value = 4; 87 } 88 } 89 90 @("DirectAccess100k4comp") 91 unittest 92 { 93 foreach(i;0..25000) 94 { 95 Entity* entity = gEntityManager.getEntity(EntityID(i*4+1,0)); 96 CUInt* comp1 = entity.getComponent!CUInt; 97 comp1.value = 4; 98 CInt* comp2 = entity.getComponent!CInt; 99 comp2.value = 3; 100 CLong* comp3 = entity.getComponent!CLong; 101 comp3.value = 1; 102 CBig* comp4 = entity.getComponent!CBig; 103 comp4.data[0] = 2; 104 } 105 } 106 107 @("DirectAccess100k1compWithMeta") 108 unittest 109 { 110 foreach(i;0..25000) 111 { 112 Entity* entity = gEntityManager.getEntity(EntityID(i*4+1,0)); 113 EntityMeta meta = entity.getMeta(); 114 CUInt* comp1 = meta.getComponent!CUInt; 115 comp1.value = 4; 116 } 117 } 118 119 @("DirectAccess100k4compWithMeta") 120 unittest 121 { 122 foreach(i;0..25000) 123 { 124 Entity* entity = gEntityManager.getEntity(EntityID(i*4+1,0)); 125 EntityMeta meta = entity.getMeta(); 126 CUInt* comp1 = meta.getComponent!CUInt; 127 comp1.value = 4; 128 CInt* comp2 = meta.getComponent!CInt; 129 comp2.value = 3; 130 CLong* comp3 = meta.getComponent!CLong; 131 comp3.value = 1; 132 CBig* comp4 = meta.getComponent!CBig; 133 comp4.data[0] = 2; 134 } 135 }