HarmonyOS鸿蒙Next中emitter.emit方法发送数据耗时大

HarmonyOS鸿蒙Next中emitter.emit方法发送数据耗时大

问题描述:使用emitter.emit发送event的时候,接口调用耗时不稳定,有时候可能会达到0.7ms,甚至更高!两次emitter.emit之间间隔较长时间的时候,会比较高概率出现耗时久的情况。

问题代码:

// 在ArkJS线程注册emitter
class Test {
  public register() {
    emitter.on("UiEventEmitter", (eventData: emitter.EventData) => {
      console.debug(`EventUITask receive unity api invke. method=${eventData.data?.method}`);
      // do something
    })
  }

  // 测试代码
  public test()  {
    let map = new Map<string, string>();
    map.set("EventTestKey1", "EventTestValue1");
    map.set("EventTestKey2", "EventTestValue2");
    let properties: Any = JSON.stringify(map);
    let start = systemDateTime.getTime(true);
    console.debug(`EventLogUnityBridge start=${start}`)
    Sender.event("EventTest", properties);
  }
}

// ArkJS线程:事件发送代码
class Sender {

  public static event(eventName: string, properties: string|null): boolean {
    let start = systemDateTime.getTime(true);
    emitter.emit<SelfEventData>("UiEventEmitter", new SelfEventData(eventName, properties));
    let time = systemDateTime.getTime(true);
    console.debug(`EventLogUnityBridge, start=${start}, time=${time - start}`);
    return true;
  }

}

class SelfEventData implements emitter.EventData {
  data: Any = {
    eventName: "",
    properties: ""
  };

  constructor(eventName: string, properties: string|null) {
    this.data.eventName = eventName;
    this.data.properties = properties;
  }
}

多次测试耗时情况如下:

05-12 19:27:45.622   43383-43383   A0FF02/com.hyp...mo/HGEventLog  com.hyper...log.demo  D     EventLog, EventLogUnityBridge, start=1747049265621730600, time=647424
05-12 19:27:51.749   43383-43383   A0FF02/com.hyp...mo/HGEventLog  com.hyper...log.demo  D     EventLog, EventLogUnityBridge, start=1747049271748679700, time=697856
05-12 19:27:55.130   43383-43383   A0FF02/com.hyp...mo/HGEventLog  com.hyper...log.demo  D     EventLog, EventLogUnityBridge, start=1747049275130354200, time=90624
05-12 19:28:00.614   43383-43383   A0FF02/com.hyp...mo/HGEventLog  com.hyper...log.demo  D     EventLog, EventLogUnityBridge, start=1747049280613628000, time=639232
05-12 19:28:04.592   43383-43383   A0FF02/com.hyp...mo/HGEventLog  com.hyper...log.demo  D     EventLog, EventLogUnityBridge, start=1747049284592576000, time=195840
05-12 19:28:10.864   43383-43383   A0FF02/com.hyp...mo/HGEventLog  com.hyper...log.demo  D     EventLog, EventLogUnityBridge, start=1747049290863994000, time=426496

更多关于HarmonyOS鸿蒙Next中emitter.emit方法发送数据耗时大的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

后面有没有优化计划呢?比如增加EventData对象池,避免每次发送EventData的时候,都需要创建新对象?

更多关于HarmonyOS鸿蒙Next中emitter.emit方法发送数据耗时大的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,emitter.emit方法用于发送事件,若发现耗时较大,可能由于事件处理逻辑复杂或事件监听器过多导致。建议检查事件处理代码,优化逻辑,减少不必要的监听器注册。此外,确保事件发送频率合理,避免高频发送。

从日志数据来看,emitter.emit的耗时确实存在波动(90μs-700μs)。这主要与HarmonyOS的事件机制实现有关:

  1. 事件队列处理机制:当两次emit间隔较长时,系统可能已进入低功耗状态,唤醒处理队列需要额外时间

  2. 序列化开销:您的SelfEventData包含JSON序列化数据,大数据量时会增加处理时间

  3. 线程调度延迟:emit是跨线程操作,ArkJS线程与UI线程的调度优先级会影响响应时间

优化建议:

  1. 对高频事件使用批处理机制,减少emit调用次数

  2. 简化EventData结构,避免在事件中传递大对象

  3. 对于时效性要求高的事件,考虑使用共享内存+信号量机制

  4. 在业务允许的情况下,适当降低事件发送频率

这种级别的延迟(<1ms)对大多数UI场景影响不大,如果是高频关键路径,建议改用更底层的IPC机制。

回到顶部