Sunday, July 13, 2014

Measuring bundle start time over an osgi framework

For the kura project I am required to measure the performance of kura on equinox vs concierge framework. For this I need to analyze the bundle startup times, cpu usage and memory usage. In order to measure the time taken by each kura bundle to start over equinox framework vs the time take for concierge, I implemented a bundle that will act as a listener for bundle events. Given below is the source code.

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;

public class Activator implements BundleActivator, BundleListener {
   
    private long trackedStartTime = 0;

    public void start(BundleContext context) throws Exception {
        trackedStartTime = System.currentTimeMillis();
        System.out.println("Starting Bundle Listener - " + context.hashCode());
        context.addBundleListener(this);
    }

    public void stop(BundleContext context) throws Exception {
        System.out.println("Stopping Bundle Listener - " + context.hashCode());
        context.removeBundleListener(this);
    }

    public void bundleChanged(BundleEvent event) {
        if(event.getType() == BundleEvent.STARTED){//a bundle started. update last start time
            long bundleStartTime = getBundleStartTime();
            long bundleMemoryOnStart = getBundleMemoryForStartup();
            String symbolicName = event.getBundle().getSymbolicName();
            if(symbolicName.contains("kura")){//only print out kura bundle startup times
                System.out.println(symbolicName + " , STARTUP TIME : "+ bundleStartTime);
            }
        }

    }
   
    private long getBundleStartTime(){
        long currentTime = System.currentTimeMillis();
        long bundleStartTime =  (currentTime - trackedStartTime);
        trackedStartTime = currentTime;//set tracker time to this bundle start time
        return bundleStartTime;
    }

}


Reference : http://eclipsesource.com/blogs/2013/01/23/how-to-track-lifecycle-changes-of-osgi-bundles/