From 5efb5deb4cca3ebcec00c85563053f65a711936f Mon Sep 17 00:00:00 2001 From: EnderShadow8 Date: Sat, 29 Jan 2022 21:48:12 +0800 Subject: [PATCH] Add query_stress benchmark --- src/bench.js | 1 + src/cases/wolf-ecs/add_remove.js | 49 ++++++------- src/cases/wolf-ecs/entity_cycle.js | 53 +++++++------- src/cases/wolf-ecs/frag_iter.js | 6 +- src/cases/wolf-ecs/packed_1.js | 56 +++++++------- src/cases/wolf-ecs/packed_5.js | 112 ++++++++++++++-------------- src/cases/wolf-ecs/query_stress.js | 86 ++++++++++++++++++++++ src/cases/wolf-ecs/simple_iter.js | 114 ++++++++++++++--------------- 8 files changed, 279 insertions(+), 198 deletions(-) create mode 100644 src/cases/wolf-ecs/query_stress.js diff --git a/src/bench.js b/src/bench.js index 6ea28af..3f56b21 100644 --- a/src/bench.js +++ b/src/bench.js @@ -26,6 +26,7 @@ const BENCHMARKS = { frag_iter: 100, entity_cycle: 1_000, add_remove: 1_000, + query_stress: 100, }; let libraries = []; diff --git a/src/cases/wolf-ecs/add_remove.js b/src/cases/wolf-ecs/add_remove.js index b343450..e3db260 100644 --- a/src/cases/wolf-ecs/add_remove.js +++ b/src/cases/wolf-ecs/add_remove.js @@ -1,43 +1,40 @@ -import {ECS, types} from "wolf-ecs" +import { ECS, types } from "wolf-ecs"; -export default function(n) { - const ecs = new ECS() +export default function (n) { + const ecs = new ECS(); - const A = ecs.defineComponent() - const B = ecs.defineComponent() + const A = ecs.defineComponent(); + const B = ecs.defineComponent(); - const qA = ecs.createQuery(A) + const qA = ecs.createQuery(A); function add() { - const lB = B - for(let i = 0; i < qA.length; i++) { - const arch = qA[i] - for(let j = arch.length - 1; j >= 0; j--) { - ecs.addComponent(arch[j], lB) + const lB = B; + for (let i = 0; i < qA.length; i++) { + const arch = qA[i]; + for (let j = arch.length - 1; j >= 0; j--) { + ecs.addComponent(arch[j], lB); } } } - const qB = ecs.createQuery(B) + const qB = ecs.createQuery(B); function remove() { - const lB = B - for(let i = 0; i < qB.length; i++) { - const arch = qB[i] - for(let j = arch.length - 1; j >= 0; j--) { - ecs.removeComponent(arch[j], lB) + const lB = B; + for (let i = 0; i < qB.length; i++) { + const arch = qB[i]; + for (let j = arch.length - 1; j >= 0; j--) { + ecs.removeComponent(arch[j], lB); } } } - for(let i = 0; i < n; i++) { - ecs.createEntity() - ecs.addComponent(i, A) + for (let i = 0; i < n; i++) { + ecs.createEntity(); + ecs.addComponent(i, A); } - add() - remove() - return () => { - add() - remove() - } + add(); + remove(); + }; } diff --git a/src/cases/wolf-ecs/entity_cycle.js b/src/cases/wolf-ecs/entity_cycle.js index a6dbada..6277acf 100644 --- a/src/cases/wolf-ecs/entity_cycle.js +++ b/src/cases/wolf-ecs/entity_cycle.js @@ -1,45 +1,42 @@ -import {ECS, types} from "wolf-ecs" +import { ECS, types } from "wolf-ecs"; -export default function(n) { - const ecs = new ECS() +export default function (n) { + const ecs = new ECS(); - const A = ecs.defineComponent() - const B = ecs.defineComponent() + const A = ecs.defineComponent(); + const B = ecs.defineComponent(); - const qA = ecs.createQuery(A) + const qA = ecs.createQuery(A); function create() { - const lB = B - for(let i = 0, l = qA.length; i < l; i++) { - const arch = qA[i] - for(let j = 0, l = arch.length; j < l; j++) { - const id = ecs.createEntity() - ecs.addComponent(id, lB) - const id2 = ecs.createEntity() - ecs.addComponent(id2, lB) + const lB = B; + for (let i = 0, l = qA.length; i < l; i++) { + const arch = qA[i]; + for (let j = 0, l = arch.length; j < l; j++) { + const id = ecs.createEntity(); + ecs.addComponent(id, lB); + const id2 = ecs.createEntity(); + ecs.addComponent(id2, lB); } } } - const qB = ecs.createQuery(B) + const qB = ecs.createQuery(B); function destroy() { - for(let i = 0, l = qB.length; i < l; i++) { - const arch = qB[i] - for(let j = arch.length - 1; j >= 0; j--) { - ecs.destroyEntity(arch[j]) + for (let i = 0, l = qB.length; i < l; i++) { + const arch = qB[i]; + for (let j = arch.length - 1; j >= 0; j--) { + ecs.destroyEntity(arch[j]); } } } - for(let i = 0; i < n; i++) { - ecs.createEntity() - ecs.addComponent(i, A) + for (let i = 0; i < n; i++) { + ecs.createEntity(); + ecs.addComponent(i, A); } - create() - destroy() - return () => { - create() - destroy() - } + create(); + destroy(); + }; } diff --git a/src/cases/wolf-ecs/frag_iter.js b/src/cases/wolf-ecs/frag_iter.js index 25d313e..59e15c7 100644 --- a/src/cases/wolf-ecs/frag_iter.js +++ b/src/cases/wolf-ecs/frag_iter.js @@ -1,6 +1,6 @@ -import {ECS, types} from "wolf-ecs" +import { ECS, types } from "wolf-ecs"; -export default function(n) { +export default function (n) { const ecs = new ECS(); const cmps = []; @@ -45,4 +45,4 @@ export default function(n) { dataSystem(); zSystem(); }; -}; +} diff --git a/src/cases/wolf-ecs/packed_1.js b/src/cases/wolf-ecs/packed_1.js index f0f49fc..37b5f76 100644 --- a/src/cases/wolf-ecs/packed_1.js +++ b/src/cases/wolf-ecs/packed_1.js @@ -1,40 +1,40 @@ -import {ECS, types} from "wolf-ecs" +import { ECS, types } from "wolf-ecs"; -export default function(n) { - const ecs = new ECS() +export default function (n) { + const ecs = new ECS(); - const A = ecs.defineComponent(types.u32) - const B = ecs.defineComponent(types.u32) - const C = ecs.defineComponent(types.u32) - const D = ecs.defineComponent(types.u32) - const E = ecs.defineComponent(types.u32) + const A = ecs.defineComponent(types.u32); + const B = ecs.defineComponent(types.u32); + const C = ecs.defineComponent(types.u32); + const D = ecs.defineComponent(types.u32); + const E = ecs.defineComponent(types.u32); - const q = ecs.createQuery(A) + const q = ecs.createQuery(A); function sys() { - const lA = A - for(let i = 0, l = q.length; i < l; i++) { - const arch = q[i] - for(let j = 0, l = arch.length; j < l; j++) { - lA[arch[j]] *= 2 + const lA = A; + for (let i = 0, l = q.length; i < l; i++) { + const arch = q[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lA[arch[j]] *= 2; } } } - for(let i = 0; i < n; i++) { - ecs.createEntity() - ecs.addComponent(i, A) - A[i] = 1 - ecs.addComponent(i, B) - B[i] = 1 - ecs.addComponent(i, C) - C[i] = 1 - ecs.addComponent(i, D) - D[i] = 1 - ecs.addComponent(i, E) - E[i] = 1 + for (let i = 0; i < n; i++) { + ecs.createEntity(); + ecs.addComponent(i, A); + A[i] = 1; + ecs.addComponent(i, B); + B[i] = 1; + ecs.addComponent(i, C); + C[i] = 1; + ecs.addComponent(i, D); + D[i] = 1; + ecs.addComponent(i, E); + E[i] = 1; } return () => { - sys() - } + sys(); + }; } diff --git a/src/cases/wolf-ecs/packed_5.js b/src/cases/wolf-ecs/packed_5.js index 25ad92d..9f254fb 100644 --- a/src/cases/wolf-ecs/packed_5.js +++ b/src/cases/wolf-ecs/packed_5.js @@ -1,88 +1,88 @@ -import {ECS, types} from "wolf-ecs" +import { ECS, types } from "wolf-ecs"; -export default function(n) { - const ecs = new ECS() +export default function (n) { + const ecs = new ECS(); - const A = ecs.defineComponent(types.u32) - const B = ecs.defineComponent(types.u32) - const C = ecs.defineComponent(types.u32) - const D = ecs.defineComponent(types.u32) - const E = ecs.defineComponent(types.u32) + const A = ecs.defineComponent(types.u32); + const B = ecs.defineComponent(types.u32); + const C = ecs.defineComponent(types.u32); + const D = ecs.defineComponent(types.u32); + const E = ecs.defineComponent(types.u32); - const qA = ecs.createQuery(A) + const qA = ecs.createQuery(A); function sysA() { - const lA = A - for(let i = 0, l = qA.length; i < l; i++) { - const arch = qA[i] - for(let j = 0, l = arch.length; j < l; j++) { - lA[arch[j]] *= 2 + const lA = A; + for (let i = 0, l = qA.length; i < l; i++) { + const arch = qA[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lA[arch[j]] *= 2; } } } - const qB = ecs.createQuery(B) + const qB = ecs.createQuery(B); function sysB() { - const lB = B - for(let i = 0, l = qB.length; i < l; i++) { - const arch = qB[i] - for(let j = 0, l = arch.length; j < l; j++) { - lB[arch[j]] *= 2 + const lB = B; + for (let i = 0, l = qB.length; i < l; i++) { + const arch = qB[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lB[arch[j]] *= 2; } } } - const qC = ecs.createQuery(C) + const qC = ecs.createQuery(C); function sysC() { - const lC = C - for(let i = 0, l = qC.length; i < l; i++) { - const arch = qC[i] - for(let j = 0, l = arch.length; j < l; j++) { - lC[arch[j]] *= 2 + const lC = C; + for (let i = 0, l = qC.length; i < l; i++) { + const arch = qC[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lC[arch[j]] *= 2; } } } - const qD = ecs.createQuery(D) + const qD = ecs.createQuery(D); function sysD() { - const lD = D - for(let i = 0, l = qD.length; i < l; i++) { - const arch = qD[i] - for(let j = 0, l = arch.length; j < l; j++) { - lD[arch[j]] *= 2 + const lD = D; + for (let i = 0, l = qD.length; i < l; i++) { + const arch = qD[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lD[arch[j]] *= 2; } } } - const qE = ecs.createQuery(E) + const qE = ecs.createQuery(E); function sysE() { - const lE = E - for(let i = 0, l = qE.length; i < l; i++) { - const arch = qE[i] - for(let j = 0, l = arch.length; j < l; j++) { - lE[arch[j]] *= 2 + const lE = E; + for (let i = 0, l = qE.length; i < l; i++) { + const arch = qE[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lE[arch[j]] *= 2; } } } - for(let i = 0; i < n; i++) { - ecs.createEntity() - ecs.addComponent(i, A) - A[i] = 1 - ecs.addComponent(i, B) - B[i] = 1 - ecs.addComponent(i, C) - C[i] = 1 - ecs.addComponent(i, D) - D[i] = 1 - ecs.addComponent(i, E) - E[i] = 1 + for (let i = 0; i < n; i++) { + ecs.createEntity(); + ecs.addComponent(i, A); + A[i] = 1; + ecs.addComponent(i, B); + B[i] = 1; + ecs.addComponent(i, C); + C[i] = 1; + ecs.addComponent(i, D); + D[i] = 1; + ecs.addComponent(i, E); + E[i] = 1; } return () => { - sysA() - sysB() - sysC() - sysD() - sysE() - } + sysA(); + sysB(); + sysC(); + sysD(); + sysE(); + }; } diff --git a/src/cases/wolf-ecs/query_stress.js b/src/cases/wolf-ecs/query_stress.js new file mode 100644 index 0000000..ef43101 --- /dev/null +++ b/src/cases/wolf-ecs/query_stress.js @@ -0,0 +1,86 @@ +import { ECS, types } from "wolf-ecs"; + +export default function (n) { + const ecs = new ECS(); + + const cmps = []; + for (let i = 0; i < 7; i++) { + cmps.push(ecs.defineComponent(types.u32)); + } + + const qA = ecs.createQuery(cmps[0]); + const qBC = ecs.createQuery(cmps[1], cmps[2]); + const qBCD = ecs.createQuery(cmps[1], cmps[2], cmps[3]); + const qDEFG = ecs.createQuery(cmps[3], cmps[4], cmps[5], cmps[6]); + + function sysA() { + const lA = cmps[0]; + for (let i = 0, l = qA.length; i < l; i++) { + const arch = qA[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lA[arch[j]] *= 2; + } + } + } + + function sysBC() { + const lB = cmps[1]; + const lC = cmps[2]; + for (let i = 0, l = qBC.length; i < l; i++) { + const arch = qBC[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lB[arch[j]] *= 2; + lC[arch[j]] *= 2; + } + } + } + + function sysBCD() { + const lB = cmps[1]; + const lC = cmps[2]; + const lD = cmps[3]; + for (let i = 0, l = qBCD.length; i < l; i++) { + const arch = qBCD[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lB[arch[j]] *= 2; + lC[arch[j]] *= 2; + lD[arch[j]] *= 2; + } + } + } + + function sysDEFG() { + const lD = cmps[3]; + const lE = cmps[4]; + const lF = cmps[5]; + const lG = cmps[6]; + for (let i = 0, l = qDEFG.length; i < l; i++) { + const arch = qDEFG[i]; + for (let j = 0, l = arch.length; j < l; j++) { + lD[arch[j]] *= 2; + lE[arch[j]] *= 2; + lF[arch[j]] *= 2; + lG[arch[j]] *= 2; + } + } + } + + for (let _ = 0; _ < n; _++) { + for (let i = 0; i < 128; i++) { + let e = ecs.createEntity(); + for (let j = 0; j < 7; j++) { + if (i & (1 << j)) { + // Create entities with every possible component combination (n * 2 ^ 7 ents in total) + ecs.addComponent(e, cmps[j]); + } + } + } + } + + return () => { + sysA(); + sysBC(); + sysBCD(); + sysDEFG(); + }; +} diff --git a/src/cases/wolf-ecs/simple_iter.js b/src/cases/wolf-ecs/simple_iter.js index c46a211..c0216a7 100644 --- a/src/cases/wolf-ecs/simple_iter.js +++ b/src/cases/wolf-ecs/simple_iter.js @@ -1,83 +1,83 @@ -import {ECS, types} from "wolf-ecs" +import { ECS, types } from "wolf-ecs"; -export default function(n) { - const ecs = new ECS() +export default function (n) { + const ecs = new ECS(); - const A = ecs.defineComponent(types.u32) - const B = ecs.defineComponent(types.u32) - const C = ecs.defineComponent(types.u32) - const D = ecs.defineComponent(types.u32) - const E = ecs.defineComponent(types.u32) + const A = ecs.defineComponent(types.u32); + const B = ecs.defineComponent(types.u32); + const C = ecs.defineComponent(types.u32); + const D = ecs.defineComponent(types.u32); + const E = ecs.defineComponent(types.u32); - const qAB = ecs.createQuery(A, B) - const qCD = ecs.createQuery(C, D) - const qCE = ecs.createQuery(C, E) + const qAB = ecs.createQuery(A, B); + const qCD = ecs.createQuery(C, D); + const qCE = ecs.createQuery(C, E); function sysAB() { - const lA = A - const lB = B - for(let i = 0, l = qAB.length; i < l; i++) { - const arch = qAB[i] - for(let j = 0, l = arch.length; j < l; j++) { - const temp = lA[arch[j]] - lA[arch[j]] = lB[arch[j]] - lB[arch[j]] = temp + const lA = A; + const lB = B; + for (let i = 0, l = qAB.length; i < l; i++) { + const arch = qAB[i]; + for (let j = 0, l = arch.length; j < l; j++) { + const temp = lA[arch[j]]; + lA[arch[j]] = lB[arch[j]]; + lB[arch[j]] = temp; } } } function sysCD() { - const lC = C - const lD = D - for(let i = 0, l = qCD.length; i < l; i++) { - const arch = qCD[i] - for(let j = 0, l = arch.length; j < l; j++) { - const temp = lC[arch[j]] - lC[arch[j]] = lD[arch[j]] - lD[arch[j]] = temp + const lC = C; + const lD = D; + for (let i = 0, l = qCD.length; i < l; i++) { + const arch = qCD[i]; + for (let j = 0, l = arch.length; j < l; j++) { + const temp = lC[arch[j]]; + lC[arch[j]] = lD[arch[j]]; + lD[arch[j]] = temp; } } } function sysCE() { - const lC = C - const lE = E - for(let i = 0, l = qCE.length; i < l; i++) { - const arch = qCE[i] - for(let j = 0, l = arch.length; j < l; j++) { - const temp = lC[arch[j]] - lC[arch[j]] = lE[arch[j]] - lE[arch[j]] = temp + const lC = C; + const lE = E; + for (let i = 0, l = qCE.length; i < l; i++) { + const arch = qCE[i]; + for (let j = 0, l = arch.length; j < l; j++) { + const temp = lC[arch[j]]; + lC[arch[j]] = lE[arch[j]]; + lE[arch[j]] = temp; } } } - for(let i = 0; i < n; i++) { - const ab = ecs.createEntity() - ecs.addComponent(ab, A) - ecs.addComponent(ab, B) + for (let i = 0; i < n; i++) { + const ab = ecs.createEntity(); + ecs.addComponent(ab, A); + ecs.addComponent(ab, B); - const abc = ecs.createEntity() - ecs.addComponent(abc, A) - ecs.addComponent(abc, B) - ecs.addComponent(abc, C) + const abc = ecs.createEntity(); + ecs.addComponent(abc, A); + ecs.addComponent(abc, B); + ecs.addComponent(abc, C); - const abcd = ecs.createEntity() - ecs.addComponent(abcd, A) - ecs.addComponent(abcd, B) - ecs.addComponent(abcd, C) - ecs.addComponent(abcd, D) + const abcd = ecs.createEntity(); + ecs.addComponent(abcd, A); + ecs.addComponent(abcd, B); + ecs.addComponent(abcd, C); + ecs.addComponent(abcd, D); - const abce = ecs.createEntity() - ecs.addComponent(abce, A) - ecs.addComponent(abce, B) - ecs.addComponent(abce, C) - ecs.addComponent(abce, E) + const abce = ecs.createEntity(); + ecs.addComponent(abce, A); + ecs.addComponent(abce, B); + ecs.addComponent(abce, C); + ecs.addComponent(abce, E); } return () => { - sysAB() - sysCD() - sysCE() - } + sysAB(); + sysCD(); + sysCE(); + }; }