duck

Unity simple performance test framework

Jul 7th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5.  
  6.  
  7. public class PerformanceTests : MonoBehaviour {
  8.    
  9.  
  10.     public float testDuration = 5;
  11.     public float iterationsPerFrame = 10000;
  12.  
  13.     List<int> results;
  14.    
  15.     IEnumerator Start () {
  16.        
  17.         Stopwatch stopwatch = new Stopwatch();
  18.        
  19.         IComparisonTest test = new CommunicationTest();
  20.        
  21.         for (int caseNum=0; caseNum < test.CaseDescriptions.Length; ++caseNum)
  22.         {
  23.             int numCompleted = 0;
  24.             stopwatch.Reset();
  25.             float testEndTime = Time.time + testDuration;
  26.             test.Initialise();
  27.            
  28.             UnityEngine.Debug.Log("Testing case "+caseNum+" : "+test.CaseDescriptions[caseNum]);
  29.            
  30.             // pre-test run
  31.             test.Test(caseNum);
  32.            
  33.             while (Time.time < testEndTime)
  34.             {
  35.                 for (int i = 0; i < iterationsPerFrame; ++i)
  36.                 {
  37.                     stopwatch.Start();
  38.                     test.Test(caseNum);
  39.                     stopwatch.Stop();
  40.                     numCompleted++;
  41.                 }    
  42.                 yield return null;
  43.             }
  44.            
  45.             UnityEngine.Debug.Log("Time "+(((float)stopwatch.ElapsedTicks) / numCompleted) +" ticks per test ");
  46.             test.Finish();
  47.         }
  48.        
  49.        
  50.     }
  51.    
  52. }
  53.  
  54.  
  55.  
  56.  
  57. public interface IComparisonTest {
  58.    
  59.     string[] CaseDescriptions {
  60.         get;
  61.     }
  62.  
  63.     void Initialise();
  64.     void Finish();
  65.    
  66.     void Test (int caseNumber);
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment