- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我似乎在使用 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/
我在一个类中有两个方法: private static InputStream getSongStream(String ip, String id){ try { U
我创建了一个扩展 InputStream 的新类并且必须 @Override read()。我正在尝试使用方法 read(int b),但是当我使用它时,它会转到方法read() 和我不能使用参数,我
我正在尝试编写一个函数,该函数将接受带有压缩文件数据的 InputStream 并返回另一个带有解压缩数据的 InputStream。 压缩后的文件将只包含一个文件,因此不需要创建目录等... 我尝试
我想知道是否有任何理想的方式可以将多个 InputStream 链接到 Java(或 Scala)中的一个连续 InputStream 中。 我需要它来解析我从 FTP 服务器通过网络加载的平面文件。
我想做的是:打开大文件的 InputStream,按 10MB block 读取它,上传一个 block ,读取下一个 block 。 val chunkCount = Math.ceil(total
我不知道怎么理解: { if (inputStream **!= null**) { inputStream.close(); 来自那个例子: public c
我想知道 InputStream 是否为空,但不使用 read() 方法。有没有办法不读取就知道它是否为空? 最佳答案 不,你不能。 InputStream 设计用于处理远程资源,因此在实际读取它之前
我制作了一个蓝牙输入流监听器,只需询问 if(InputStream.isAvailable > 0) 即可检查是否有传入数据,然后创建一个循环将传入数据存储到 int[] 直到没有更多并且它工作完美
这是我的代码流程,文件内容丢失,我认为可能是 IOUtils.toByteArray() 行有问题,请指导这里实际出了什么问题。 文件内容丢失: InputStream stream = someCl
我从 HTTP 请求的响应开始: InputStream responseInputStream = response.getEntityInputStream() 我需要对该响应进行 gzip 压缩
用户将一个大文件上传到我的网站,我想对该文件进行 gzip 压缩并将其存储在 blob 中。所以我有一个未压缩的 InputStream,而 blob 需要一个 InputStream。我知道如何使用
我调用了一个返回压缩文件的服务。我从响应中将数据作为 InputStream(由 javax.activation.DataHandler.getInputStream(); 提供)提供。 我想做的是
我正在尝试压缩一个 InputStream 并返回一个 InputStream: public InputStream compress (InputStream in){ // Read "in
我最近在 Kotlin 中看到了将 InputStream 的全部内容读入 String 的代码,例如: // input is of type InputStream val baos = Byte
我正在尝试使用以下代码从 IHTTPSession.getInputStream() 读取 InputStream,但它每次都给出 Socket TimeOut Exception。 private
如 How to use Jersey interceptors to get request body 中所述,我正在修改 ContainerRequestFilter 中的 EntityInput
我正在编写一个需要与蓝牙 2.1 设备交换数据的应用程序。我已经做过好几次了,但这次发生了一些奇怪的事情。 Log.d("TAG", "connectToDevice"); if(ma
我只是在犹豫这是好主意还是坏主意: InputStreamReader in = new InputStreamReader(socket.getInputStream()); BufferedRea
我正在开发一个 Android 应用程序,它的 View 包含多个图库。图库的内容(位图)是来自 Internet 的红色。 对于第一个画廊,一切正常,但是当尝试下载第二个画廊的第一张图片时,Bitm
在Dart中,我想读取BMP,所以可能是BIG文件。 我这样做是这样的: var inputStream = imageFile.openInputStream(); inputStream.onDa
我是一名优秀的程序员,十分优秀!