1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:48:11 +00:00

disk_benchmark: TRY more stuff :^)

This commit is contained in:
Kenneth Myhra 2022-03-25 22:36:18 +01:00 committed by Tim Flynn
parent c0f15ebcbb
commit f38076e596

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -11,11 +12,10 @@
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/ElapsedTimer.h> #include <LibCore/ElapsedTimer.h>
#include <LibCore/System.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
@ -39,7 +39,7 @@ static Result average_result(const Vector<Result>& results)
return average; return average;
} }
static Optional<Result> benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache); static ErrorOr<Result> benchmark(const String& filename, int file_size, ByteBuffer& buffer, bool allow_cache);
ErrorOr<int> serenity_main(Main::Arguments arguments) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
@ -85,10 +85,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
while (timer.elapsed() < time_per_benchmark * 1000) { while (timer.elapsed() < time_per_benchmark * 1000) {
out("."); out(".");
fflush(stdout); fflush(stdout);
auto result = benchmark(filename, file_size, block_size, buffer_result.value(), allow_cache); auto result = TRY(benchmark(filename, file_size, buffer_result.value(), allow_cache));
if (!result.has_value()) results.append(result);
return 1;
results.append(result.release_value());
usleep(100); usleep(100);
} }
auto average = average_result(results); auto average = average_result(results);
@ -101,23 +99,22 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return 0; return 0;
} }
Optional<Result> benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache) ErrorOr<Result> benchmark(const String& filename, int file_size, ByteBuffer& buffer, bool allow_cache)
{ {
int flags = O_CREAT | O_TRUNC | O_RDWR; int flags = O_CREAT | O_TRUNC | O_RDWR;
if (!allow_cache) if (!allow_cache)
flags |= O_DIRECT; flags |= O_DIRECT;
int fd = open(filename.characters(), flags, 0644); int fd = TRY(Core::System::open(filename.characters(), flags, 0644));
if (fd == -1) {
perror("open");
exit(1);
}
auto fd_cleanup = ScopeGuard([fd, filename] { auto fd_cleanup = ScopeGuard([fd, filename] {
if (close(fd) < 0) auto void_or_error = Core::System::close(fd);
perror("close"); if (void_or_error.is_error())
if (unlink(filename.characters()) < 0) warnln("{}", void_or_error.release_error());
perror("unlink");
void_or_error = Core::System::unlink(filename);
if (void_or_error.is_error())
warnln("{}", void_or_error.release_error());
}); });
Result result; Result result;
@ -126,29 +123,18 @@ Optional<Result> benchmark(const String& filename, int file_size, int block_size
ssize_t total_written = 0; ssize_t total_written = 0;
while (total_written < file_size) { while (total_written < file_size) {
auto nwritten = write(fd, buffer.data(), block_size); auto nwritten = TRY(Core::System::write(fd, buffer));
if (nwritten < 0) {
perror("write");
return {};
}
total_written += nwritten; total_written += nwritten;
} }
result.write_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000; result.write_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000;
if (lseek(fd, 0, SEEK_SET) < 0) { TRY(Core::System::lseek(fd, 0, SEEK_SET));
perror("lseek");
return {};
}
timer.start(); timer.start();
ssize_t total_read = 0; ssize_t total_read = 0;
while (total_read < file_size) { while (total_read < file_size) {
auto nread = read(fd, buffer.data(), block_size); auto nread = TRY(Core::System::read(fd, buffer));
if (nread < 0) {
perror("read");
return {};
}
total_read += nread; total_read += nread;
} }