Beispiel #1
0
    void PerformanceTest()
    {
        long test_time = 0;

        group1 = Group.Create(new ComponentsList <CompTest1, CompTest2, CompTest3, CompTest4, CompTest5, CompTest6>());
        group2 = Group.Create(new ComponentsList <CompTest1, CompTest2>(), new ComponentsList <CompTest3, CompTest4, CompTest5, CompTest6>());

        groups = new List <Group> {
            group1, group2
        };
        gameObjects = new List <GameObject>();


        for (int i = 0; i < 10; i++)
        {
            gameObjects.Add(new GameObject());
        }

        Debug.LogWarning("начат тест производительности фреймворка");
        //Stopwatch time = Stopwatch.StartNew();

        foreach (GameObject obj in gameObjects)
        {
            obj.AddComponent <TestEntity3>();
        }

        int dif = 100;

        test_time += TestContains(dif);
        test_time += TestIEnumerator(dif);
        test_time += TestAddRemoveComponent(dif);
        test_time += TestAddRemoveGroup(dif);

        for (int i = 0; i < gameObjects.Count; i++)
        {
            Destroy(gameObjects[i]);
        }

        Debug.Log("Тест закончен за " + test_time + " ms");

        long TestContains(int difficult)
        {
            Stopwatch time = Stopwatch.StartNew();

            for (int i = 0; i < difficult; i++)
            {
                foreach (Group group in groups)
                {
                    foreach (int ent in group)
                    {
                        group.Contains(ent);
                    }
                }
            }

            long total_time = time.ElapsedMilliseconds;

            Debug.Log("Contains test complete for " + total_time + " ms");

            return(total_time);
        }

        long TestIEnumerator(int difficult)
        {
            Stopwatch time = Stopwatch.StartNew();

            for (int i = 0; i < difficult; i++)
            {
                foreach (Group group in groups)
                {
                    foreach (int ent in group)
                    {
                        ;
                    }
                }
            }

            long total_time = time.ElapsedMilliseconds;

            Debug.Log("IEnumerator test complete for " + total_time + " ms");

            return(total_time);
        }

        long TestAddRemoveComponent(int difficult)
        {
            GameObject testObj = new GameObject();

            Stopwatch time = Stopwatch.StartNew();

            testObj.AddComponent <TestEntity4>();
            EntityBase entityBase = testObj.GetComponent <EntityBase>();

            for (int i = 0; i < difficult; i++)
            {
                entityBase.RemoveCmp <CompTest1>();
                entityBase.AddCmp <CompTest1>();
            }

            long total_time = time.ElapsedMilliseconds;

            Debug.Log("AddRemoveComponent test complete for " + total_time + " ms");

            Destroy(testObj);

            return(total_time);
        }

        long TestAddRemoveGroup(int difficult)
        {
            GameObject testObj = new GameObject();

            Stopwatch time = Stopwatch.StartNew();

            testObj.AddComponent <TestEntity4>();
            EntityBase entityBase = testObj.GetComponent <EntityBase>();

            for (int i = 0; i < difficult; i++)
            {
                entityBase.RemoveCmp <CompTest3>();
                entityBase.RemoveCmp <CompTest4>();
                entityBase.RemoveCmp <CompTest5>();
                entityBase.RemoveCmp <CompTest6>();

                entityBase.AddCmp <CompTest3>();
                entityBase.AddCmp <CompTest4>();
                entityBase.AddCmp <CompTest5>();
                entityBase.AddCmp <CompTest6>();
            }

            long total_time = time.ElapsedMilliseconds;

            Debug.Log("AddRemoveGroup test complete for " + total_time + " ms");

            Destroy(testObj);

            return(total_time);
        }
    }