有没有谁可以解释一下为什么鸿蒙的IPC性能不及安卓的一半?

发布于 2021-11-23 10:00
浏览
1收藏

鸿蒙官宣的IPC性能相当强悍,吊打QNX,于是萌生想法,看一下在android上面,鸿蒙到底会比android的ipc强多少?

于是根据官方IDL示例文档(IDL)撸了一段最简单的ipc调用,就是透传一个32位int整数,拿回来之后加一,在ipc调用透传,循环一万次,最终看下时间,关键代码如下:

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");
    public MyIdlInterfaceProxy proxy;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Button button = (Button) findComponentById(ResourceTable.Id_button_Login);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.HIGH);
                Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        HiLog.info(LABEL_LOG, "async task1 run");
                        if (proxy != null) {
                            int sum = 0;
                            long t1 = System.currentTimeMillis();
                            try {
                                while (sum < 10000) {
                                    sum = proxy.demoFun(++sum);
                                    System.out.println("sum = " + sum);
                                }
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                            long t2 = System.currentTimeMillis();
                            System.out.println("time = " + (t2 - t1));
                        }
                    }
                });
                HiLog.info(LABEL_LOG, "after async task1");

            }
        });

        connectServer();
    }

    private IAbilityConnection conn = new IAbilityConnection() {
        @Override
        public void onAbilityConnectDone(ElementName element, IRemoteObject remote, int resultCode) {
            proxy = new MyIdlInterfaceProxy(remote);
            HiLog.debug(LABEL_LOG, "onAbilityConnectDone");
        }

        @Override
        public void onAbilityDisconnectDone(ElementName element, int resultCode) {
            proxy = null;
            System.out.println("------resultCode = " + resultCode);
        }
    };

    private void connectServer() {
        Intent intent = new Intent();
        ElementName elementName = new ElementName(
                "",
                "com.example.idldemo",
                "com.example.idldemo.ServiceAbility");
        intent.setElement(elementName);
        connectAbility(intent, conn);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

同理,在android的应用中使用AIDL方式进行相同的操作,相关代码如下:

public class MainActivity extends AppCompatActivity {
    private static String TAG = MainActivity.class.getSimpleName();
    public IMyAidlInterface binder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindSvr();
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (binder != null) {
                        long t1 = System.currentTimeMillis();
                        int sumTmp = 0;
                        while (sumTmp < 10000) {
                            sumTmp = binder.funSum(++sumTmp);
                            Log.d(TAG, "sum = " + sumTmp);
                        }
                        long t2 = System.currentTimeMillis();
                        Log.d(TAG, "time = " + (t2 - t1));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void bindSvr() {
        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "onServiceConnected");
                binder = IMyAidlInterface.Stub.asInterface(service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "onServiceDisconnected svr name " + name);
            }
        };

        Intent intent = new Intent();
        intent.setPackage("cn.example.aidldemo");
        intent.setAction("cn.example.aidldemo.server");
        bindService(intent, connection, Service.BIND_AUTO_CREATE);
    }
}

服务端的方法实现就是一个透传值:

@Override
public int demoFun(int _anInt) throws RemoteException {
     return _anInt;
}

测试环境为:

华为Mate30(TAS-AL00)

HarmoneyOS 2.0.0

都是debug包

运行10次取平均值(ms):

1 2 3 4 5 6 7 8 9 10 avg
android 969 901 888 898 920 905 968 913 968 714 904.4
harmoney 2440 2738 2795 2717 2776 2636 2241 2016 2260 2294 2491.3

[问号脸][wtf]

究竟为何呢?颠覆认知了啊? 这么打脸么?
是不是我哪里做的不对?

鸿蒙的普通binder和dbinder到底都是个什么鬼?

反正,希望高人指点一下吧,帮我解解惑,感谢!

已于2021-11-23 10:06:53修改
3
收藏 1
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐