Looking for some tips on performance testing

Hey all!

I started doing some performance testing between the different ways available to call a function from another class. I wanted to know how much slower SendMessage() is then a direct call, and also tested the speed of the custom messaging system I’m using from the wiki.

To do the testing, I’m using CodeProfiler

void Start ()
{
    	bike = Managers.Bike;
    	bikeEngine = bike.GetComponent<BikeEngine>();
}

void Update()
{
        CodeProfiler.Begin("SendMessage");
    	for( int i = 0; i < 800; i++ )
    		bikeEngine.SendMessage( "TestSpeed", SendMessageOptions.DontRequireReceiver );
    	CodeProfiler.End("SendMessage");
    		
    	CodeProfiler.Begin("Broadcast");
    	for( int i = 0; i < 3000; i++ )
    		Messenger.Broadcast( "TestSpeed", MessengerMode.DONT_REQUIRE_LISTENER );
   		CodeProfiler.End("Broadcast");
    		
   		CodeProfiler.Begin("DirectCall");
    	for( int i = 0; i < 3000; i++ )
    		bikeEngine.TestSpeed();
    	CodeProfiler.End("DirectCall");
    
}

With the code above, I get something like:

SendMessage 75.5% 45.10ms…

Broadcast 8.25% 4.5ms…

DirectCall 1.06% 1.0ms…

Now I do another test, and I only remove the first test block (the SendMessage one), and here are the aprx. results:

Broadcast 23.5% 20.512ms…

DirectCall 1.06% 1.0ms…

Why did “Broadcast” change? Are the % and ms based on 100%, so if in the first test, SendMessage was taking 75+%, the rest was being divided between the other calls? So without SendMessage, Broadcast appears to take more time but really is the same as before? Those numbers really confused me…if someone could shed some light, I will sleep a lot better tonight ah!

Thanks,
Stephane

The problem with your test, is that everything is done within one frame. I guess, some “SendMessage” and “Broadcast” are just the same message in the end.

Try again but with only one message per frame, by using a coroutine:

IEnumerator Start()
{
    // Initialization
    bike = Managers.Bike;
    bikeEngine = bike.GetComponent<BikeEngine>();

    // SendMessage
    CodeProfiler.Begin("SendMessage");
    for( int i = 0; i < 800; i++ )
    {
        bikeEngine.SendMessage( "TestSpeed", SendMessageOptions.DontRequireReceiver );
        yield return null;
    }
    CodeProfiler.End("SendMessage");

    // Broadcast
    CodeProfiler.Begin("Broadcast");
    for( int i = 0; i < 3000; i++ )
    {
        Messenger.Broadcast( "TestSpeed", MessengerMode.DONT_REQUIRE_LISTENER );
        yield return null;
    }
    CodeProfiler.End("Broadcast");

    // DirectCall
    CodeProfiler.Begin("DirectCall");
    for( int i = 0; i < 3000; i++ )
    {
        bikeEngine.TestSpeed();
        yield return null;
    }
    CodeProfiler.End("DirectCall");    
}