gpt4 book ai didi

用于计算每秒数据包数的 Java 代码

转载 作者:行者123 更新时间:2023-12-01 11:01:01 24 4
gpt4 key购买 nike

我有一种方法可以在 SDN 中从交换机接收数据包到泛光灯 Controller ,这意味着每个新传入的数据包都会触发该方法。我想计算该方法中每秒的数据包数。

这是我的尝试;它是正确的?

int CLoad,avergeLoad =0;
final String switchId = sw.getStringId();
CLoad = CLoad + 1;
avergeLoad = CLoad;
loadTable.put(switchId, avergeLoad);
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleAtFixedRate(new Runnable()
{
public void run()
{
avergeLoad =(avergeLoad + CLoad)/2;
loadTable.put(switchId, avergeLoad);
CLoad=0;
}
}, 40, 1000, TimeUnit.SECONDS);

最佳答案

您可以使用Metrics meter

Meters measure the rate of the events in a few different ways. The mean rate is the average rate of events. It’s generally useful for trivia, but as it represents the total rate for your application’s entire lifetime (e.g., the total number of requests handled, divided by the number of seconds the process has been running), it doesn’t offer a sense of recency. Luckily, meters also record three different exponentially-weighted moving average rates: the 1-, 5-, and 15-minute moving averages.

Maven:

<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.1.2</version>
</dependency>

示例代码:

//Setup the reporter
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);

//Initialize Metrics Metter
final Meter getRequests = registry.meter(name(WebProxy.class, "get-requests", "requests"));

// Mark when event occurs
getRequests.mark();

引用:

  1. Getting started
  2. Metrics meter

关于用于计算每秒数据包数的 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com