You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在OMNET++的VEINS示例仿真结果中获取吞吐量与延迟?

解决VEINS仿真中吞吐量与延迟指标缺失的问题

不需要额外添加模块,只需修改VEINS示例中的TraCIDemo11p模块代码,添加统计逻辑并将结果注册到OMNeT++的结果收集系统即可。具体操作步骤如下:

1. 在头文件中添加统计变量

打开TraCIDemo11p.h,在类的私有成员中添加用于统计的变量:

private:
    simtime_t totalDelay;          // 累计总延迟
    long totalReceivedBytes;       // 累计接收字节数
    long totalSentBytes;           // 累计发送字节数
    int receivedMsgCount;          // 接收消息总数
    simtime_t simulationStartTime; // 仿真启动时间

2. 初始化统计变量

打开TraCIDemo11p.cc,在initialize()函数中初始化上述变量:

void TraCIDemo11p::initialize(int stage) {
    DemoBaseApplLayer::initialize(stage);
    if (stage == 0) {
        // 保留原有初始化逻辑
        totalDelay = 0;
        totalReceivedBytes = 0;
        totalSentBytes = 0;
        receivedMsgCount = 0;
        simulationStartTime = simTime();
    }
}

3. 为消息添加时间戳

在populateWSM()函数中设置消息的发送时间戳,用于后续计算延迟:

void TraCIDemo11p::populateWSM(BaseFrame1609_4* wsm) {
    DemoBaseApplLayer::populateWSM(wsm);
    wsm->setTimestamp(simTime()); // 记录消息发送时间
}

4. 统计发送消息的字节数

在发送消息的逻辑中(以Beacon消息为例,位于handleSelfMsg()),累加发送字节数:

void TraCIDemo11p::handleSelfMsg(cMessage* msg) {
    if (msg == sendBeaconEvt) {
        TraCIDemo11pMessage* beacon = new TraCIDemo11pMessage();
        populateWSM(beacon);
        totalSentBytes += beacon->getByteLength(); // 统计发送字节数
        sendDown(beacon);
        scheduleAt(simTime() + beaconInterval, sendBeaconEvt);
    }
    // 保留其他自消息处理逻辑
}

5. 统计接收消息的延迟与字节数

在handleLowerMsg()函数中,计算消息延迟并累加接收字节数:

void TraCIDemo11p::handleLowerMsg(cMessage* msg) {
    TraCIDemo11pMessage* wsm = check_and_cast<TraCIDemo11pMessage*>(msg);
    
    // 计算单条消息延迟并累加
    simtime_t singleDelay = simTime() - wsm->getTimestamp();
    totalDelay += singleDelay;
    receivedMsgCount++;
    // 累加接收字节数
    totalReceivedBytes += wsm->getByteLength();
    
    // 保留原有消息处理逻辑
    delete msg;
}

6. 注册统计结果到OMNeT++

在finish()函数中计算最终指标,并将结果注册到OMNeT++的结果系统,这样就能在「浏览数据」中查看:

void TraCIDemo11p::finish() {
    DemoBaseApplLayer::finish();
    
    // 计算平均延迟
    simtime_t avgDelay = receivedMsgCount > 0 ? totalDelay / receivedMsgCount : 0;
    // 计算吞吐量(转换为bps,字节×8转比特,除以仿真时长)
    simtime_t simDuration = simTime() - simulationStartTime;
    double throughputRx = (totalReceivedBytes * 8) / simDuration.dbl();
    double throughputTx = (totalSentBytes * 8) / simDuration.dbl();
    
    // 注册标量结果
    recordScalar("average_delay_s", avgDelay);
    recordScalar("total_received_bytes", totalReceivedBytes);
    recordScalar("total_sent_bytes", totalSentBytes);
    recordScalar("throughput_rx_bps", throughputRx);
    recordScalar("throughput_tx_bps", throughputTx);
    recordScalar("received_message_count", receivedMsgCount);
}

7. 编译运行

修改完成后,重新编译VEINS项目并运行仿真。仿真结束后,在OMNeT++的结果窗口「浏览数据」中,即可找到上述注册的吞吐量、延迟等指标。

内容的提问来源于stack exchange,提问作者Prarthana Prabhakaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 00:15:35