public async Task Run(TestResultDelegate resultCallback = null)
        {
            foreach (MethodInfo method in testMethods.Keys)
            {
                object     testObject = testMethods[method];
                TestResult testResult = new TestResult(method, null, TestResult.Result.Passed);

                try
                {
                    if (testObject is Node node)
                    {
                        sceneTree.Root.AddChild(node);
                    }

                    if (setupMethods.ContainsKey(method.DeclaringType))
                    {
                        setupMethods[method.DeclaringType].Invoke(testObject, new object[] {});
                    }

                    object obj = method.Invoke(testObject, new object[] {});

                    if (obj is IEnumerator coroutine)
                    {
                        while (coroutine.MoveNext())
                        {
                            await Task.Delay(10);
                        }
                    }
                }
                catch (Exception e)
                {
                    testResult = new TestResult(method, e.InnerException ?? e, TestResult.Result.Failed);
                }
                finally
                {
                    testResults.Add(testResult);

                    if (teardownMethods.ContainsKey(method.DeclaringType))
                    {
                        teardownMethods[method.DeclaringType].Invoke(testObject, new object[] {});
                    }

                    if (testObject is Node node)
                    {
                        node.QueueFree();
                    }
                }

                if (resultCallback != null)
                {
                    resultCallback(testResult);
                }

                await Task.Delay(1);
            }
        }
Example #2
0
        private IEnumerator TestHarness(ReadMode readMode, string path, TestType testType, int attempts, TestResultDelegate callback)
        {
            var stopwatch = new Stopwatch();

            string[] assetNames = null;

            var streamingAssetsUrl = Path.Combine(StreamingAssetsPath, path).Replace('\\', '/');

            long bytesRead        = 0;
            long maxMemoryPeak    = 0;
            long totalMemoryPeaks = 0;

            for (int i = 0; i < attempts; ++i)
            {
                WWW www = null;

                yield return(Resources.UnloadUnusedAssets());

                GC.Collect();
                GC.WaitForPendingFinalizers();
                yield return(null);

                var memoryUnityBefore = Profiler.GetTotalAllocatedMemoryLong();
                //var memoryMonoBefore = Profiler.GetMonoUsedSizeLong();
                stopwatch.Start();

                if (readMode == ReadMode.WWW)
                {
                    www = new WWW(streamingAssetsUrl);

                    {
                        yield return(www);

                        Profiler.BeginSample(testType.ToString());

                        switch (testType)
                        {
                        case TestType.CheckIfExists:
                            if (!string.IsNullOrEmpty(www.error))
                            {
                                throw new System.Exception(www.error);
                            }
                            break;

                        case TestType.LoadBytes:
                            bytesRead += www.bytes.Length;
                            break;

                        default:
                            throw new NotSupportedException();
                        }
                        Profiler.EndSample();
                    }
                }
                else if (readMode == ReadMode.BSA)
                {
                    Profiler.BeginSample(testType.ToString());

                    switch (testType)
                    {
                    case TestType.CheckIfExists:
                        if (!BetterStreamingAssets.FileExists(path))
                        {
                            throw new System.InvalidOperationException();
                        }
                        break;

                    case TestType.LoadBytes:
                        bytesRead += BetterStreamingAssets.ReadAllBytes(path).Length;
                        break;
                    }

                    Profiler.EndSample();
                }
                else if (readMode == ReadMode.Direct)
                {
                    var p = streamingAssetsUrl;
                    Profiler.BeginSample(testType.ToString());

                    switch (testType)
                    {
                    case TestType.CheckIfExists:
                        if (!File.Exists(p))
                        {
                            throw new System.InvalidOperationException();
                        }
                        break;

                    case TestType.LoadBytes:
                        bytesRead += File.ReadAllBytes(p).Length;
                        break;
                    }

                    Profiler.EndSample();
                }
                stopwatch.Stop();

                var memoryPeak = Math.Max(0, Profiler.GetTotalAllocatedMemoryLong() - memoryUnityBefore);
                // + Math.Max(0, Profiler.GetMonoUsedSizeLong() - memoryMonoBefore);

                maxMemoryPeak     = System.Math.Max(memoryPeak, maxMemoryPeak);
                totalMemoryPeaks += memoryPeak;

                yield return(null);

                if (www != null)
                {
                    www.Dispose();
                }

                yield return(null);
            }

            yield return(Resources.UnloadUnusedAssets());

            GC.Collect();
            GC.WaitForPendingFinalizers();
            yield return(null);

            callback(new TimeSpan(stopwatch.ElapsedTicks / attempts), bytesRead / attempts, totalMemoryPeaks / attempts, maxMemoryPeak, assetNames);
        }