gpt4 book ai didi

java - 使用 InputStream 和 Scanner 获得令人困惑的性能

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:54 25 4
gpt4 key购买 nike

我似乎在使用 InputStream 和扫描仪时遇到了奇怪的性能问题。

我在日志文件上使用tail -F,本质上是将其实时流式传输到我的代码。当我这样做时,我在 60 秒内得到大约 7,500,000 行(每秒 125,000 行)。很公平。

现在,由于日志文件的写入速度实际上超过 125,000 行/秒,我 grep 日志文件以缩小结果范围。

当我在 Linux shell 中运行更新后的 tail -F 命令时,我在 60 秒内获得了大约 40,000 行(667 行/秒)。远低于我在 60 秒内未执行 grep 时的 141,000 行。

但是,当我通过代码运行完全相同的命令时,我在 60 秒内得到了 16,000 行(266 行/秒)!所以在某个地方我丢失了一半的行,我正在挠头想知道为什么。

代码:

  public Boolean call() {
//send command with no wait.
final PrintStream out = new PrintStream(eCli.getOutputStream());

//Notice the command terminator "\n" which is needed here for the command to be executed.
out.print(tailCommandBuilder(logDir,fileName, filters));
out.print((char)4);
out.print('\n');
final Scanner s = new Scanner(eCli.getInputStream());
out.flush();
//InputStream in = new BufferedInputStream(eCli.getInputStream());
StopWatch sw = new StopWatch();
sw.reset();
sw.start();
log.info("Stopwatch started.");
double lineCounter = 0;
//final BufferedReader buffReader = new BufferedReader(new InputStreamReader(in));
try {
LOOP:
while (keepRunning.get()) {

if (s.hasNextLine()) {
String st = s.nextLine();
lineCounter++;
} else {
keepRunning.getAndSet(false);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("Error in IORead: " + e.getMessage() + e.getStackTrace());

} finally {
sw.stop();
log.info("Total Time: " + sw.getTime());
s.close();
log.warn("Total Lines Read: " + lineCounter);
eCli.clearBuffer();
eCli.disconnect();
}
return true;
}

编辑:

如前所述,eCli 是“扩展 Cli”类的一个实例,它实现了命令行界面类型实例。它使用InputStream/OutputStream进行通信。

我不将 --line-buffering 与 grep 一起使用。

最佳答案

我想我正在缩小这里的答案范围。

我已经彻底修改了前端代码,但 grep 时的日志捕获百分比实际上只从 50% 增加到 66%。唯一的区别是新实现从 InputStream 中提取数据的速度。

日志似乎随着他们的输入而“爆发”。我认为某个地方需要增加一个缓冲区来处理这种信息突发并允许日志达到平均值。

需要在 BufferedReader、InputStream 或 Shell 中增加缓冲区。

关于java - 使用 InputStream 和 Scanner 获得令人困惑的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30176389/

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