Android Open Source - EventBus Perf Test Event Bus






From Project

Back to project page EventBus.

License

The source code is released under:

Apache License

If you think the Android project EventBus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.greenrobot.eventperf.testsubject;
//from  ww w.  j  a v  a 2 s .  co m
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;

import android.content.Context;
import de.greenrobot.event.EventBus;
import de.greenrobot.eventperf.Test;
import de.greenrobot.eventperf.TestEvent;
import de.greenrobot.eventperf.TestParams;

public abstract class PerfTestEventBus extends Test {

    private final EventBus eventBus;
    private final ArrayList<Object> subscribers;
    private final Class<?> subscriberClass;
    private final int eventCount;
    private final int expectedEventCount;

    public PerfTestEventBus(Context context, TestParams params) {
        super(context, params);
        eventBus = EventBus.builder().eventInheritance(params.isEventInheritance()).build();
        subscribers = new ArrayList<Object>();
        eventCount = params.getEventCount();
        expectedEventCount = eventCount * params.getSubscriberCount();
        subscriberClass = getSubscriberClassForThreadMode();
    }

    @Override
    public void prepareTest() {
        try {
            Constructor<?> constructor = subscriberClass.getConstructor(PerfTestEventBus.class);
            for (int i = 0; i < params.getSubscriberCount(); i++) {
                Object subscriber = constructor.newInstance(this);
                subscribers.add(subscriber);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Class<?> getSubscriberClassForThreadMode() {
        switch (params.getThreadMode()) {
            case MainThread:
                return SubscribeClassEventBusMain.class;
            case BackgroundThread:
                return SubscribeClassEventBusBackground.class;
            case Async:
                return SubscriberClassEventBusAsync.class;
            case PostThread:
                return SubscribeClassEventBusDefault.class;
            default:
                throw new RuntimeException("Unknown: " + params.getThreadMode());
        }
    }

    public static class Post extends PerfTestEventBus {
        public Post(Context context, TestParams params) {
            super(context, params);
        }

        @Override
        public void prepareTest() {
            super.prepareTest();
            super.registerSubscribers();
        }

        public void runTest() {
            TestEvent event = new TestEvent();
            long timeStart = System.nanoTime();
            for (int i = 0; i < super.eventCount; i++) {
                super.eventBus.post(event);
                if (canceled) {
                    break;
                }
            }
            long timeAfterPosting = System.nanoTime();
            waitForReceivedEventCount(super.expectedEventCount);
            long timeAllReceived = System.nanoTime();

            primaryResultMicros = (timeAfterPosting - timeStart) / 1000;
            primaryResultCount = super.expectedEventCount;
            long deliveredMicros = (timeAllReceived - timeStart) / 1000;
            int deliveryRate = (int) (primaryResultCount / (deliveredMicros / 1000000d));
            otherTestResults = "Post and delivery time: " + deliveredMicros + " micros<br/>" + //
                    "Post and delivery rate: " + deliveryRate + "/s";
        }

        @Override
        public String getDisplayName() {
            String inheritance = params.isEventInheritance() ? ", event inheritance" : ", no event inheritance";
            return "EventBus Post Events, " + params.getThreadMode() + inheritance;
        }
    }

    public static class RegisterAll extends PerfTestEventBus {
        public RegisterAll(Context context, TestParams params) {
            super(context, params);
        }

        public void runTest() {
            super.registerUnregisterOneSubscribers();
            long timeNanos = super.registerSubscribers();
            primaryResultMicros = timeNanos / 1000;
            primaryResultCount = params.getSubscriberCount();
        }

        @Override
        public String getDisplayName() {
            return "EventBus Register, no unregister";
        }
    }

    public static class RegisterOneByOne extends PerfTestEventBus {
        protected Method clearCachesMethod;

        public RegisterOneByOne(Context context, TestParams params) {
            super(context, params);
        }

        public void runTest() {
            long time = 0;
            if (clearCachesMethod == null) {
                // Skip first registration unless just the first registration is tested
                super.registerUnregisterOneSubscribers();
            }
            for (Object subscriber : super.subscribers) {
                if (clearCachesMethod != null) {
                    try {
                        clearCachesMethod.invoke(null);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                long beforeRegister = System.nanoTime();
                super.eventBus.register(subscriber);
                long timeRegister = System.nanoTime() - beforeRegister;
                time += timeRegister;
                super.eventBus.unregister(subscriber);
                if (canceled) {
                    return;
                }
            }

            primaryResultMicros = time / 1000;
            primaryResultCount = params.getSubscriberCount();
        }

        @Override
        public String getDisplayName() {
            return "EventBus Register";
        }
    }

    public static class RegisterFirstTime extends RegisterOneByOne {

        public RegisterFirstTime(Context context, TestParams params) {
            super(context, params);
            try {
                Class<?> clazz = Class.forName("de.greenrobot.event.SubscriberMethodFinder");
                clearCachesMethod = clazz.getDeclaredMethod("clearCaches");
                clearCachesMethod.setAccessible(true);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public String getDisplayName() {
            return "EventBus Register, first time";
        }

    }

    public class SubscribeClassEventBusDefault {
        public void onEvent(TestEvent event) {
            eventsReceivedCount.incrementAndGet();
        }

        public void dummy() {
        }

        public void dummy2() {
        }

        public void dummy3() {
        }

        public void dummy4() {
        }

        public void dummy5() {
        }
    }

    public class SubscribeClassEventBusMain {
        public void onEventMainThread(TestEvent event) {
            eventsReceivedCount.incrementAndGet();
        }

        public void dummy() {
        }

        public void dummy2() {
        }

        public void dummy3() {
        }

        public void dummy4() {
        }

        public void dummy5() {
        }
    }

    public class SubscribeClassEventBusBackground {
        public void onEventBackgroundThread(TestEvent event) {
            eventsReceivedCount.incrementAndGet();
        }

        public void dummy() {
        }

        public void dummy2() {
        }

        public void dummy3() {
        }

        public void dummy4() {
        }

        public void dummy5() {
        }
    }

    public class SubscriberClassEventBusAsync {
        public void onEventAsync(TestEvent event) {
            eventsReceivedCount.incrementAndGet();
        }

        public void dummy() {
        }

        public void dummy2() {
        }

        public void dummy3() {
        }

        public void dummy4() {
        }

        public void dummy5() {
        }
    }

    private long registerSubscribers() {
        long time = 0;
        for (Object subscriber : subscribers) {
            long timeStart = System.nanoTime();
            eventBus.register(subscriber);
            long timeEnd = System.nanoTime();
            time += timeEnd - timeStart;
            if (canceled) {
                return 0;
            }
        }
        return time;
    }

    private void registerUnregisterOneSubscribers() {
        if (!subscribers.isEmpty()) {
            Object subscriber = subscribers.get(0);
            eventBus.register(subscriber);
            eventBus.unregister(subscriber);
        }
    }

}




Java Source Code List

de.greenrobot.event.AsyncPoster.java
de.greenrobot.event.BackgroundPoster.java
de.greenrobot.event.EventBusBuilder.java
de.greenrobot.event.EventBusException.java
de.greenrobot.event.EventBus.java
de.greenrobot.event.HandlerPoster.java
de.greenrobot.event.NoSubscriberEvent.java
de.greenrobot.event.PendingPostQueue.java
de.greenrobot.event.PendingPost.java
de.greenrobot.event.SubscriberExceptionEvent.java
de.greenrobot.event.SubscriberMethodFinder.java
de.greenrobot.event.SubscriberMethod.java
de.greenrobot.event.Subscription.java
de.greenrobot.event.ThreadMode.java
de.greenrobot.event.util.AsyncExecutor.java
de.greenrobot.event.util.ErrorDialogConfig.java
de.greenrobot.event.util.ErrorDialogFragmentFactory.java
de.greenrobot.event.util.ErrorDialogFragments.java
de.greenrobot.event.util.ErrorDialogManager.java
de.greenrobot.event.util.ExceptionToResourceMapping.java
de.greenrobot.event.util.HasExecutionScope.java
de.greenrobot.event.util.ThrowableFailureEvent.java
de.greenrobot.eventperf.TestEvent.java
de.greenrobot.eventperf.TestFinishedEvent.java
de.greenrobot.eventperf.TestParams.java
de.greenrobot.eventperf.TestRunnerActivity.java
de.greenrobot.eventperf.TestRunner.java
de.greenrobot.eventperf.TestSetupActivity.java
de.greenrobot.eventperf.Test.java
de.greenrobot.eventperf.testsubject.PerfTestEventBus.java
de.greenrobot.eventperf.testsubject.PerfTestOtto.java