移除过度复杂的组件缓存系统 #57

This commit is contained in:
YHH
2025-07-18 18:08:57 +08:00
parent af61067f08
commit 05455421fb
3 changed files with 546 additions and 353 deletions

View File

@@ -23,143 +23,7 @@ export class EntityComparer {
}
}
/**
* 组件缓存项
*/
interface ComponentCacheEntry<T extends Component = Component> {
component: T;
lastAccessed: number;
accessCount: number;
}
/**
* 组件缓存配置
*/
interface ComponentCacheConfig {
maxSize: number;
ttl: number; // 生存时间(毫秒)
enableLRU: boolean; // 是否启用LRU淘汰策略
}
/**
* 高性能组件缓存
*/
class ComponentCache {
private cache = new Map<ComponentType, ComponentCacheEntry>();
private accessOrder: ComponentType[] = [];
private config: ComponentCacheConfig;
constructor(config: ComponentCacheConfig = {
maxSize: 16,
ttl: 5000,
enableLRU: true
}) {
this.config = config;
}
public get<T extends Component>(type: ComponentType<T>): T | null {
const entry = this.cache.get(type);
if (!entry) {
return null;
}
// 检查TTL
if (Date.now() - entry.lastAccessed > this.config.ttl) {
this.cache.delete(type);
this.removeFromAccessOrder(type);
return null;
}
// 更新访问信息
entry.lastAccessed = Date.now();
entry.accessCount++;
// 更新LRU顺序
if (this.config.enableLRU) {
this.updateAccessOrder(type);
}
return entry.component as T;
}
public set<T extends Component>(type: ComponentType<T>, component: T): void {
// 检查缓存大小限制
if (this.cache.size >= this.config.maxSize && !this.cache.has(type)) {
this.evictLeastRecentlyUsed();
}
const entry: ComponentCacheEntry<T> = {
component,
lastAccessed: Date.now(),
accessCount: 1
};
this.cache.set(type, entry);
if (this.config.enableLRU) {
this.updateAccessOrder(type);
}
}
public delete(type: ComponentType): boolean {
const deleted = this.cache.delete(type);
if (deleted) {
this.removeFromAccessOrder(type);
}
return deleted;
}
public clear(): void {
this.cache.clear();
this.accessOrder.length = 0;
}
public has(type: ComponentType): boolean {
return this.cache.has(type);
}
private evictLeastRecentlyUsed(): void {
if (this.accessOrder.length > 0) {
const lruType = this.accessOrder[0];
this.cache.delete(lruType);
this.accessOrder.shift();
}
}
private updateAccessOrder(type: ComponentType): void {
this.removeFromAccessOrder(type);
this.accessOrder.push(type);
}
private removeFromAccessOrder(type: ComponentType): void {
const index = this.accessOrder.indexOf(type);
if (index !== -1) {
this.accessOrder.splice(index, 1);
}
}
public getStats(): {
size: number;
maxSize: number;
hitRate: number;
averageAccessCount: number;
} {
let totalAccess = 0;
let totalHits = 0;
for (const entry of this.cache.values()) {
totalAccess += entry.accessCount;
totalHits++;
}
return {
size: this.cache.size,
maxSize: this.config.maxSize,
hitRate: totalAccess > 0 ? totalHits / totalAccess : 0,
averageAccessCount: this.cache.size > 0 ? totalAccess / this.cache.size : 0
};
}
}
/**
* 游戏实体类
@@ -297,37 +161,15 @@ export class Entity {
*/
private _componentTypeToIndex = new Map<ComponentType, number>();
/**
* 组件缓存
*
* 高性能组件访问缓存。
*/
private _componentCache: ComponentCache;
/**
* 组件访问统计
*
* 记录组件访问的性能统计信息。
*/
private _componentAccessStats = new Map<ComponentType, {
accessCount: number;
lastAccessed: number;
cacheHits: number;
cacheMisses: number;
}>();
/**
* 构造函数
*
*
* @param name - 实体名称
* @param id - 实体唯一标识符
*/
constructor(name: string, id: number) {
this.name = name;
this.id = id;
// 初始化组件缓存
this._componentCache = new ComponentCache();
}
/**
@@ -488,7 +330,7 @@ export class Entity {
*/
private addComponentInternal<T extends Component>(component: T): T {
const componentType = component.constructor as ComponentType<T>;
// 注册组件类型(如果尚未注册)
if (!ComponentRegistry.isRegistered(componentType)) {
ComponentRegistry.register(componentType);
@@ -496,25 +338,14 @@ export class Entity {
// 设置组件的实体引用
component.entity = this;
// 添加到组件列表并建立索引映射
const index = this.components.length;
this.components.push(component);
this._componentTypeToIndex.set(componentType, index);
// 更新位掩码
this._componentMask |= ComponentRegistry.getBitMask(componentType);
// 添加到缓存
this._componentCache.set(componentType, component);
// 初始化访问统计
this._componentAccessStats.set(componentType, {
accessCount: 0,
lastAccessed: Date.now(),
cacheHits: 0,
cacheMisses: 0
});
return component;
}
@@ -575,36 +406,21 @@ export class Entity {
* @returns 组件实例或null
*/
public getComponent<T extends Component>(type: ComponentType<T>): T | null {
// 更新访问统计
this.updateComponentAccessStats(type);
// 首先检查位掩码,快速排除
// 首先检查位掩码快速排除O(1)
if (!ComponentRegistry.isRegistered(type)) {
this.recordCacheMiss(type);
return null;
}
const mask = ComponentRegistry.getBitMask(type);
if ((this._componentMask & mask) === BigInt(0)) {
this.recordCacheMiss(type);
return null;
}
// 尝试从缓存获取O(1)
const cachedComponent = this._componentCache.get(type);
if (cachedComponent) {
this.recordCacheHit(type);
return cachedComponent;
}
// 尝试从索引映射获取O(1)
const index = this._componentTypeToIndex.get(type);
if (index !== undefined && index < this.components.length) {
const component = this.components[index];
if (component && component.constructor === type) {
// 添加到缓存
this._componentCache.set(type, component);
this.recordCacheHit(type);
return component as T;
}
}
@@ -613,74 +429,26 @@ export class Entity {
if (this.scene && this.scene.componentStorageManager) {
const component = this.scene.componentStorageManager.getComponent(this.id, type);
if (component) {
// 更新本地缓存和索引
this._componentCache.set(type, component);
// 重建索引映射
this.rebuildComponentIndex();
this.recordCacheHit(type);
return component;
}
}
// 最后回退到线性搜索并重建索引
// 最后回退到线性搜索并重建索引O(n)但n很小且很少发生
for (let i = 0; i < this.components.length; i++) {
const component = this.components[i];
if (component instanceof type) {
// 重建索引映射
this._componentTypeToIndex.set(type, i);
this._componentCache.set(type, component);
this.recordCacheHit(type);
return component as T;
}
}
this.recordCacheMiss(type);
return null;
}
/**
* 更新组件访问统计
*
* @param type - 组件类型
*/
private updateComponentAccessStats(type: ComponentType): void {
let stats = this._componentAccessStats.get(type);
if (!stats) {
stats = {
accessCount: 0,
lastAccessed: Date.now(),
cacheHits: 0,
cacheMisses: 0
};
this._componentAccessStats.set(type, stats);
}
stats.accessCount++;
stats.lastAccessed = Date.now();
}
/**
* 记录缓存命中
*
* @param type - 组件类型
*/
private recordCacheHit(type: ComponentType): void {
const stats = this._componentAccessStats.get(type);
if (stats) {
stats.cacheHits++;
}
}
/**
* 记录缓存未命中
*
* @param type - 组件类型
*/
private recordCacheMiss(type: ComponentType): void {
const stats = this._componentAccessStats.get(type);
if (stats) {
stats.cacheMisses++;
}
}
/**
* 重建组件索引映射
@@ -745,12 +513,6 @@ export class Entity {
this.rebuildComponentIndex();
}
// 从缓存中移除
this._componentCache.delete(componentType);
// 清除访问统计
this._componentAccessStats.delete(componentType);
// 更新位掩码
if (ComponentRegistry.isRegistered(componentType)) {
this._componentMask &= ~ComponentRegistry.getBitMask(componentType);
@@ -810,10 +572,8 @@ export class Entity {
// 复制组件列表,避免在迭代时修改
const componentsToRemove = [...this.components];
// 清空所有缓存和索引
this._componentCache.clear();
// 清空索引和位掩码
this._componentTypeToIndex.clear();
this._componentAccessStats.clear();
this._componentMask = BigInt(0);
// 移除组件
@@ -880,83 +640,7 @@ export class Entity {
return removedComponents;
}
/**
* 获取组件缓存统计信息
*
* @returns 缓存统计信息
*/
public getComponentCacheStats(): {
cacheStats: ReturnType<ComponentCache['getStats']>;
accessStats: Map<string, {
accessCount: number;
lastAccessed: number;
cacheHits: number;
cacheMisses: number;
hitRate: number;
}>;
indexMappingSize: number;
totalComponents: number;
} {
const accessStats = new Map<string, {
accessCount: number;
lastAccessed: number;
cacheHits: number;
cacheMisses: number;
hitRate: number;
}>();
for (const [componentType, stats] of this._componentAccessStats) {
const total = stats.cacheHits + stats.cacheMisses;
accessStats.set(componentType.name, {
...stats,
hitRate: total > 0 ? stats.cacheHits / total : 0
});
}
return {
cacheStats: this._componentCache.getStats(),
accessStats,
indexMappingSize: this._componentTypeToIndex.size,
totalComponents: this.components.length
};
}
/**
* 预热组件缓存
*
* 将所有组件添加到缓存中,提升后续访问性能
*/
public warmUpComponentCache(): void {
for (let i = 0; i < this.components.length; i++) {
const component = this.components[i];
const componentType = component.constructor as ComponentType;
// 更新索引映射
this._componentTypeToIndex.set(componentType, i);
// 添加到缓存
this._componentCache.set(componentType, component);
}
}
/**
* 清理组件缓存
*
* 清除过期的缓存项,释放内存
*/
public cleanupComponentCache(): void {
// ComponentCache内部会自动处理TTL过期
// 这里我们可以强制清理一些不常用的缓存项
const now = Date.now();
const cleanupThreshold = 30000; // 30秒未访问的组件从缓存中移除
for (const [componentType, stats] of this._componentAccessStats) {
if (now - stats.lastAccessed > cleanupThreshold && stats.accessCount < 5) {
this._componentCache.delete(componentType);
}
}
}
/**
* 获取所有指定类型的组件
@@ -1279,32 +963,8 @@ export class Entity {
childCount: number;
childIds: number[];
depth: number;
componentCache: {
size: number;
maxSize: number;
hitRate: number;
averageAccessCount: number;
};
componentAccessStats: Array<{
componentType: string;
accessCount: number;
cacheHits: number;
cacheMisses: number;
hitRate: number;
lastAccessed: string;
}>;
indexMappingSize: number;
} {
const cacheStats = this.getComponentCacheStats();
const accessStatsArray = Array.from(cacheStats.accessStats.entries()).map(([type, stats]) => ({
componentType: type,
accessCount: stats.accessCount,
cacheHits: stats.cacheHits,
cacheMisses: stats.cacheMisses,
hitRate: stats.hitRate,
lastAccessed: new Date(stats.lastAccessed).toISOString()
}));
return {
name: this.name,
id: this.id,
@@ -1319,9 +979,7 @@ export class Entity {
childCount: this._children.length,
childIds: this._children.map(c => c.id),
depth: this.getDepth(),
componentCache: cacheStats.cacheStats,
componentAccessStats: accessStatsArray,
indexMappingSize: cacheStats.indexMappingSize
indexMappingSize: this._componentTypeToIndex.size
};
}
}

View File

@@ -0,0 +1,267 @@
import { Entity } from '../../src/ECS/Entity';
import { Component } from '../../src/ECS/Component';
// 测试组件类
class PerfTestComponent1 extends Component {
public value: number = 1;
}
class PerfTestComponent2 extends Component {
public value: number = 2;
}
class PerfTestComponent3 extends Component {
public value: number = 3;
}
class PerfTestComponent4 extends Component {
public value: number = 4;
}
class PerfTestComponent5 extends Component {
public value: number = 5;
}
class PerfTestComponent6 extends Component {
public value: number = 6;
}
class PerfTestComponent7 extends Component {
public value: number = 7;
}
class PerfTestComponent8 extends Component {
public value: number = 8;
}
describe('Entity - 性能测试', () => {
describe('典型游戏实体性能测试', () => {
test('3-5个组件的实体性能测试', () => {
const entity = new Entity('TypicalEntity', 1);
// 添加典型游戏实体的组件数量3-5个
entity.addComponent(new PerfTestComponent1());
entity.addComponent(new PerfTestComponent2());
entity.addComponent(new PerfTestComponent3());
entity.addComponent(new PerfTestComponent4());
entity.addComponent(new PerfTestComponent5());
const iterations = 10000;
const startTime = performance.now();
// 模拟典型的组件访问模式
for (let i = 0; i < iterations; i++) {
entity.getComponent(PerfTestComponent1);
entity.getComponent(PerfTestComponent3);
entity.getComponent(PerfTestComponent5);
}
const endTime = performance.now();
const duration = endTime - startTime;
console.log(`典型实体(5组件) ${iterations * 3}次组件获取耗时: ${duration.toFixed(2)}ms`);
// 优化后应该有很好的性能
expect(duration).toBeLessThan(50); // 应该在50ms内完成
});
test('内存使用优化验证', () => {
const entities: Entity[] = [];
const entityCount = 1000;
// 创建大量实体,每个实体有少量组件
for (let i = 0; i < entityCount; i++) {
const entity = new Entity(`Entity_${i}`, i);
entity.addComponent(new PerfTestComponent1());
entity.addComponent(new PerfTestComponent2());
entity.addComponent(new PerfTestComponent3());
entities.push(entity);
}
// 测试批量组件访问性能
const startTime = performance.now();
for (const entity of entities) {
entity.getComponent(PerfTestComponent1);
entity.getComponent(PerfTestComponent2);
entity.getComponent(PerfTestComponent3);
}
const endTime = performance.now();
const duration = endTime - startTime;
console.log(`${entityCount}个实体每个3个组件总计${entityCount * 3}次组件获取耗时: ${duration.toFixed(2)}ms`);
// 优化后的内存使用应该更高效
expect(duration).toBeLessThan(100); // 应该在100ms内完成
});
test('组件添加和移除性能测试', () => {
const entity = new Entity('TestEntity', 1);
const iterations = 1000;
const startTime = performance.now();
for (let i = 0; i < iterations; i++) {
// 添加组件
const comp1 = entity.addComponent(new PerfTestComponent1());
const comp2 = entity.addComponent(new PerfTestComponent2());
const comp3 = entity.addComponent(new PerfTestComponent3());
// 获取组件
entity.getComponent(PerfTestComponent1);
entity.getComponent(PerfTestComponent2);
entity.getComponent(PerfTestComponent3);
// 移除组件
entity.removeComponent(comp1);
entity.removeComponent(comp2);
entity.removeComponent(comp3);
}
const endTime = performance.now();
const duration = endTime - startTime;
console.log(`${iterations}次组件添加-获取-移除循环耗时: ${duration.toFixed(2)}ms`);
// 优化后应该有良好的添加/移除性能
expect(duration).toBeLessThan(200); // 应该在200ms内完成
});
});
describe('极端情况性能测试', () => {
test('单个组件高频访问性能', () => {
const entity = new Entity('SingleComponentEntity', 1);
entity.addComponent(new PerfTestComponent1());
const iterations = 100000;
const startTime = performance.now();
for (let i = 0; i < iterations; i++) {
entity.getComponent(PerfTestComponent1);
}
const endTime = performance.now();
const duration = endTime - startTime;
console.log(`单组件${iterations}次高频访问耗时: ${duration.toFixed(2)}ms`);
// 单组件访问应该非常快
expect(duration).toBeLessThan(80); // 应该在80ms内完成
});
test('多组件实体性能测试', () => {
const entity = new Entity('MultiComponentEntity', 1);
// 添加8个组件比典型情况多
entity.addComponent(new PerfTestComponent1());
entity.addComponent(new PerfTestComponent2());
entity.addComponent(new PerfTestComponent3());
entity.addComponent(new PerfTestComponent4());
entity.addComponent(new PerfTestComponent5());
entity.addComponent(new PerfTestComponent6());
entity.addComponent(new PerfTestComponent7());
entity.addComponent(new PerfTestComponent8());
const iterations = 5000;
const startTime = performance.now();
// 随机访问不同组件
for (let i = 0; i < iterations; i++) {
entity.getComponent(PerfTestComponent1);
entity.getComponent(PerfTestComponent4);
entity.getComponent(PerfTestComponent7);
entity.getComponent(PerfTestComponent2);
entity.getComponent(PerfTestComponent8);
entity.getComponent(PerfTestComponent3);
entity.getComponent(PerfTestComponent6);
entity.getComponent(PerfTestComponent5);
}
const endTime = performance.now();
const duration = endTime - startTime;
console.log(`多组件实体(8组件) ${iterations * 8}次随机访问耗时: ${duration.toFixed(2)}ms`);
// 即使是多组件,优化后的性能也应该良好
expect(duration).toBeLessThan(100); // 应该在100ms内完成
});
test('hasComponent性能测试', () => {
const entity = new Entity('HasComponentTestEntity', 1);
entity.addComponent(new PerfTestComponent1());
entity.addComponent(new PerfTestComponent3());
entity.addComponent(new PerfTestComponent5());
const iterations = 50000;
const startTime = performance.now();
for (let i = 0; i < iterations; i++) {
entity.hasComponent(PerfTestComponent1); // 存在
entity.hasComponent(PerfTestComponent2); // 不存在
entity.hasComponent(PerfTestComponent3); // 存在
entity.hasComponent(PerfTestComponent4); // 不存在
entity.hasComponent(PerfTestComponent5); // 存在
}
const endTime = performance.now();
const duration = endTime - startTime;
console.log(`${iterations * 5}次hasComponent检查耗时: ${duration.toFixed(2)}ms`);
// hasComponent应该通过位掩码快速完成
expect(duration).toBeLessThan(150); // 应该在150ms内完成
});
});
describe('内存效率测试', () => {
test('大量实体内存使用测试', () => {
const entities: Entity[] = [];
const entityCount = 5000;
const startTime = performance.now();
// 创建大量实体,模拟真实游戏场景
for (let i = 0; i < entityCount; i++) {
const entity = new Entity(`Entity_${i}`, i);
// 每个实体随机添加2-6个组件
const componentCount = 2 + (i % 5);
if (componentCount >= 1) entity.addComponent(new PerfTestComponent1());
if (componentCount >= 2) entity.addComponent(new PerfTestComponent2());
if (componentCount >= 3) entity.addComponent(new PerfTestComponent3());
if (componentCount >= 4) entity.addComponent(new PerfTestComponent4());
if (componentCount >= 5) entity.addComponent(new PerfTestComponent5());
if (componentCount >= 6) entity.addComponent(new PerfTestComponent6());
entities.push(entity);
}
const creationTime = performance.now() - startTime;
// 测试访问性能
const accessStartTime = performance.now();
for (const entity of entities) {
entity.getComponent(PerfTestComponent1);
if (entity.hasComponent(PerfTestComponent3)) {
entity.getComponent(PerfTestComponent3);
}
if (entity.hasComponent(PerfTestComponent5)) {
entity.getComponent(PerfTestComponent5);
}
}
const accessTime = performance.now() - accessStartTime;
console.log(`创建${entityCount}个实体耗时: ${creationTime.toFixed(2)}ms`);
console.log(`访问${entityCount}个实体的组件耗时: ${accessTime.toFixed(2)}ms`);
// 优化后应该有良好的批量处理性能
expect(creationTime).toBeLessThan(500); // 创建应该在500ms内完成
expect(accessTime).toBeLessThan(100); // 访问应该在100ms内完成
});
});
});

268
tests/ECS/Entity.test.ts Normal file
View File

@@ -0,0 +1,268 @@
import { Entity } from '../../src/ECS/Entity';
import { Component } from '../../src/ECS/Component';
// 测试组件类
class TestPositionComponent extends Component {
public x: number = 0;
public y: number = 0;
constructor(x: number = 0, y: number = 0) {
super();
this.x = x;
this.y = y;
}
}
class TestHealthComponent extends Component {
public health: number = 100;
constructor(health: number = 100) {
super();
this.health = health;
}
}
class TestVelocityComponent extends Component {
public vx: number = 0;
public vy: number = 0;
constructor(vx: number = 0, vy: number = 0) {
super();
this.vx = vx;
this.vy = vy;
}
}
class TestRenderComponent extends Component {
public visible: boolean = true;
constructor(visible: boolean = true) {
super();
this.visible = visible;
}
}
describe('Entity - 组件缓存优化测试', () => {
let entity: Entity;
beforeEach(() => {
// 创建新的实体
entity = new Entity('TestEntity', 1);
});
describe('基本功能测试', () => {
test('应该能够创建实体', () => {
expect(entity.name).toBe('TestEntity');
expect(entity.id).toBe(1);
expect(entity.components.length).toBe(0);
});
test('应该能够添加组件', () => {
const position = new TestPositionComponent(10, 20);
const addedComponent = entity.addComponent(position);
expect(addedComponent).toBe(position);
expect(entity.components.length).toBe(1);
expect(entity.components[0]).toBe(position);
expect(position.entity).toBe(entity);
});
test('应该能够获取组件', () => {
const position = new TestPositionComponent(10, 20);
entity.addComponent(position);
const retrieved = entity.getComponent(TestPositionComponent);
expect(retrieved).toBe(position);
expect(retrieved?.x).toBe(10);
expect(retrieved?.y).toBe(20);
});
test('应该能够检查组件存在性', () => {
const position = new TestPositionComponent(10, 20);
entity.addComponent(position);
expect(entity.hasComponent(TestPositionComponent)).toBe(true);
expect(entity.hasComponent(TestHealthComponent)).toBe(false);
});
test('应该能够移除组件', () => {
const position = new TestPositionComponent(10, 20);
entity.addComponent(position);
entity.removeComponent(position);
expect(entity.components.length).toBe(0);
expect(entity.hasComponent(TestPositionComponent)).toBe(false);
expect(position.entity).toBeNull();
});
});
describe('多组件管理测试', () => {
test('应该能够管理多个不同类型的组件', () => {
const position = new TestPositionComponent(10, 20);
const health = new TestHealthComponent(150);
const velocity = new TestVelocityComponent(5, -3);
entity.addComponent(position);
entity.addComponent(health);
entity.addComponent(velocity);
expect(entity.components.length).toBe(3);
expect(entity.hasComponent(TestPositionComponent)).toBe(true);
expect(entity.hasComponent(TestHealthComponent)).toBe(true);
expect(entity.hasComponent(TestVelocityComponent)).toBe(true);
});
test('应该能够正确获取多个组件', () => {
const position = new TestPositionComponent(10, 20);
const health = new TestHealthComponent(150);
const velocity = new TestVelocityComponent(5, -3);
entity.addComponent(position);
entity.addComponent(health);
entity.addComponent(velocity);
const retrievedPosition = entity.getComponent(TestPositionComponent);
const retrievedHealth = entity.getComponent(TestHealthComponent);
const retrievedVelocity = entity.getComponent(TestVelocityComponent);
expect(retrievedPosition).toBe(position);
expect(retrievedHealth).toBe(health);
expect(retrievedVelocity).toBe(velocity);
});
test('应该能够批量添加组件', () => {
const components = [
new TestPositionComponent(10, 20),
new TestHealthComponent(150),
new TestVelocityComponent(5, -3)
];
const addedComponents = entity.addComponents(components);
expect(addedComponents.length).toBe(3);
expect(entity.components.length).toBe(3);
expect(addedComponents[0]).toBe(components[0]);
expect(addedComponents[1]).toBe(components[1]);
expect(addedComponents[2]).toBe(components[2]);
});
test('应该能够移除所有组件', () => {
entity.addComponent(new TestPositionComponent(10, 20));
entity.addComponent(new TestHealthComponent(150));
entity.addComponent(new TestVelocityComponent(5, -3));
entity.removeAllComponents();
expect(entity.components.length).toBe(0);
expect(entity.hasComponent(TestPositionComponent)).toBe(false);
expect(entity.hasComponent(TestHealthComponent)).toBe(false);
expect(entity.hasComponent(TestVelocityComponent)).toBe(false);
});
});
describe('性能优化验证', () => {
test('位掩码应该正确工作', () => {
const position = new TestPositionComponent(10, 20);
const health = new TestHealthComponent(150);
entity.addComponent(position);
entity.addComponent(health);
// 位掩码应该反映组件的存在
expect(entity.hasComponent(TestPositionComponent)).toBe(true);
expect(entity.hasComponent(TestHealthComponent)).toBe(true);
expect(entity.hasComponent(TestVelocityComponent)).toBe(false);
});
test('索引映射应该正确维护', () => {
const position = new TestPositionComponent(10, 20);
const health = new TestHealthComponent(150);
const velocity = new TestVelocityComponent(5, -3);
entity.addComponent(position);
entity.addComponent(health);
entity.addComponent(velocity);
// 获取组件应该通过索引映射快速完成
const retrievedPosition = entity.getComponent(TestPositionComponent);
const retrievedHealth = entity.getComponent(TestHealthComponent);
const retrievedVelocity = entity.getComponent(TestVelocityComponent);
expect(retrievedPosition).toBe(position);
expect(retrievedHealth).toBe(health);
expect(retrievedVelocity).toBe(velocity);
});
test('组件获取性能应该良好', () => {
const position = new TestPositionComponent(10, 20);
const health = new TestHealthComponent(150);
const velocity = new TestVelocityComponent(5, -3);
const render = new TestRenderComponent(true);
entity.addComponent(position);
entity.addComponent(health);
entity.addComponent(velocity);
entity.addComponent(render);
// 测试大量获取操作的性能
const iterations = 1000;
const startTime = performance.now();
for (let i = 0; i < iterations; i++) {
entity.getComponent(TestPositionComponent);
entity.getComponent(TestHealthComponent);
entity.getComponent(TestVelocityComponent);
entity.getComponent(TestRenderComponent);
}
const endTime = performance.now();
const duration = endTime - startTime;
// 1000次 * 4个组件 = 4000次获取操作应该在合理时间内完成
expect(duration).toBeLessThan(100); // 应该在100ms内完成
});
});
describe('边界情况测试', () => {
test('获取不存在的组件应该返回null', () => {
const result = entity.getComponent(TestPositionComponent);
expect(result).toBeNull();
});
test('不应该允许添加重复类型的组件', () => {
const position1 = new TestPositionComponent(10, 20);
const position2 = new TestPositionComponent(30, 40);
entity.addComponent(position1);
expect(() => {
entity.addComponent(position2);
}).toThrow();
});
test('移除不存在的组件应该安全处理', () => {
const position = new TestPositionComponent(10, 20);
expect(() => {
entity.removeComponent(position);
}).not.toThrow();
});
test('调试信息应该正确反映实体状态', () => {
const position = new TestPositionComponent(10, 20);
const health = new TestHealthComponent(150);
entity.addComponent(position);
entity.addComponent(health);
const debugInfo = entity.getDebugInfo();
expect(debugInfo.name).toBe('TestEntity');
expect(debugInfo.id).toBe(1);
expect(debugInfo.componentCount).toBe(2);
expect(debugInfo.componentTypes).toContain('TestPositionComponent');
expect(debugInfo.componentTypes).toContain('TestHealthComponent');
expect(debugInfo.indexMappingSize).toBe(2);
});
});
});