在Python/J中创建文件时获得每秒的写入速度

2024-05-20 22:05:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个大文件,比如说5/10gb。但我需要显示每秒钟的写入速度。有些像这样

783441920 bytes (783 MB, 747 MiB) copied, 1 s, 783 MB/s
1088824320 bytes (1.1 GB, 1.0 GiB) copied, 2 s, 544 MB/s
1390494720 bytes (1.4 GB, 1.3 GiB) copied, 3 s, 462 MB/s
1602211840 bytes (1.6 GB, 1.5 GiB) copied, 4 s, 399 MB/s
1678894080 bytes (1.7 GB, 1.6 GiB) copied, 5 s, 312 MB/s
1850752000 bytes (1.9 GB, 1.7 GiB) copied, 6 s, 308 MB/s
2023480320 bytes (2.0 GB, 1.9 GiB) copied, 7 s, 288 MB/s
2195302400 bytes (2.2 GB, 2.0 GiB) copied, 8 s, 274 MB/s
2363176960 bytes (2.4 GB, 2.2 GiB) copied, 9 s, 262 MB/s

但是我找不到任何方法来获得每一秒的速度(我不能使用dd command,我必须用python或java手动创建文件)


Tags: 文件方法bytesmb手动java速度command
2条回答

在Java中,没有任何方法可以获得写速度,必须自己实现。下面是一个写入一个5gb文件并大约每秒输出一次进程的示例

它写入1KB的数据块,统计写入的数据块,如果从开始到最后一次输出超过1秒,则打印实际的统计数据

int blockCount = 1024 * 1024 * 5;
int blockSize = 1024;
// sums up to 5 GB
byte[] bytes = new byte[blockSize];
new SecureRandom().nextBytes(bytes); // generates some random data
FileOutputStream fos = new FileOutputStream(new File("/path/to/your/file"));
long blocksWritten = 0;
long blocksWrittenLastSecond = 0;
long timer = System.currentTimeMillis();
for (int i = 0; i < blockCount; i++) {
    fos.write(bytes);
    blocksWrittenLastSecond++;
    long now = System.currentTimeMillis();
    long timerDelay = now - timer;
    if (timerDelay >= 1000 || i == blockCount - 1) {
        blocksWritten += blocksWrittenLastSecond;
        long bytesWrittenTotal = blockSize * blocksWritten;
        long bytesWrittenLastSecond = blockSize * blocksWrittenLastSecond;
        double currentMegaBytesPerSecond = (double) (bytesWrittenLastSecond / (1024 * 1024)) / (timerDelay / 1000d);
        System.out.println(bytesWrittenTotal + " bytes written with " + Math.round(currentMegaBytesPerSecond) + " mb/s");
        // reset for next second
        blocksWrittenLastSecond = 0;
        timer = System.currentTimeMillis();
    }
}
fos.close();

这是旧硬盘的输出:

262943744 bytes written with 250 mb/s
638312448 bytes written with 357 mb/s
1021830144 bytes written with 365 mb/s
1395726336 bytes written with 356 mb/s
1425204224 bytes written with 10 mb/s
1459958784 bytes written with 6 mb/s
1512032256 bytes written with 49 mb/s
1876321280 bytes written with 347 mb/s
2250982400 bytes written with 357 mb/s
2563824640 bytes written with 298 mb/s
2879109120 bytes written with 300 mb/s
3191470080 bytes written with 297 mb/s
3417269248 bytes written with 215 mb/s
3644478464 bytes written with 215 mb/s
3674432512 bytes written with 6 mb/s
3776074752 bytes written with 22 mb/s
4130227200 bytes written with 337 mb/s
4482337792 bytes written with 335 mb/s
4830609408 bytes written with 332 mb/s
5181122560 bytes written with 334 mb/s
5368709120 bytes written with 221 mb/s

另一种选择是使用第二个线程每秒轮询写入的字节并执行输出。希望有帮助

Java不提供任何getWriteSpeed()之类的东西。但假设您使用FileOutputStream写入该文件,请尝试不要一次写入所有数据,而是将数据分解为1024字节的数据块,并通过System.currentTimeMillis()获取整个写入操作之前和每次写入()之后的时间(如有必要,还包括flush())

您的写入速度将是(chunkswrited*1024)/((currentTime-writeStartTime)/1000d,单位为字节/秒

然后每秒钟你就可以输出这个值。 要检测新的秒数,您可以保存currentTime%1000的结果(除以1000的余数),并检查每个写入周期是否小于上一个周期

相关问题 更多 >