diff options
author | Kenneth Myhra <kennethmyhra@gmail.com> | 2022-03-25 22:36:18 +0100 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-03-27 08:33:24 -0400 |
commit | f38076e5968b1ad5fe2b7882bff846848275fff6 (patch) | |
tree | 4ddf501b8d97c777671d8c182517719d6a528e6e /Userland | |
parent | c0f15ebcbbb33da96a437fcf998d2ea917850db5 (diff) | |
download | serenity-f38076e5968b1ad5fe2b7882bff846848275fff6.zip |
disk_benchmark: TRY more stuff :^)
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Utilities/disk_benchmark.cpp | 48 |
1 files changed, 17 insertions, 31 deletions
diff --git a/Userland/Utilities/disk_benchmark.cpp b/Userland/Utilities/disk_benchmark.cpp index 74239f1b73..d3dd9f9628 100644 --- a/Userland/Utilities/disk_benchmark.cpp +++ b/Userland/Utilities/disk_benchmark.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@gmail.com> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -11,11 +12,10 @@ #include <AK/Vector.h> #include <LibCore/ArgsParser.h> #include <LibCore/ElapsedTimer.h> +#include <LibCore/System.h> #include <LibMain/Main.h> #include <fcntl.h> -#include <getopt.h> #include <stdio.h> -#include <stdlib.h> #include <sys/stat.h> #include <unistd.h> @@ -39,7 +39,7 @@ static Result average_result(const Vector<Result>& results) 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) { @@ -85,10 +85,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) while (timer.elapsed() < time_per_benchmark * 1000) { out("."); fflush(stdout); - auto result = benchmark(filename, file_size, block_size, buffer_result.value(), allow_cache); - if (!result.has_value()) - return 1; - results.append(result.release_value()); + auto result = TRY(benchmark(filename, file_size, buffer_result.value(), allow_cache)); + results.append(result); usleep(100); } auto average = average_result(results); @@ -101,23 +99,22 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) 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; if (!allow_cache) flags |= O_DIRECT; - int fd = open(filename.characters(), flags, 0644); - if (fd == -1) { - perror("open"); - exit(1); - } + int fd = TRY(Core::System::open(filename.characters(), flags, 0644)); auto fd_cleanup = ScopeGuard([fd, filename] { - if (close(fd) < 0) - perror("close"); - if (unlink(filename.characters()) < 0) - perror("unlink"); + auto void_or_error = Core::System::close(fd); + if (void_or_error.is_error()) + warnln("{}", void_or_error.release_error()); + + void_or_error = Core::System::unlink(filename); + if (void_or_error.is_error()) + warnln("{}", void_or_error.release_error()); }); Result result; @@ -126,29 +123,18 @@ Optional<Result> benchmark(const String& filename, int file_size, int block_size ssize_t total_written = 0; while (total_written < file_size) { - auto nwritten = write(fd, buffer.data(), block_size); - if (nwritten < 0) { - perror("write"); - return {}; - } + auto nwritten = TRY(Core::System::write(fd, buffer)); total_written += nwritten; } result.write_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000; - if (lseek(fd, 0, SEEK_SET) < 0) { - perror("lseek"); - return {}; - } + TRY(Core::System::lseek(fd, 0, SEEK_SET)); timer.start(); ssize_t total_read = 0; while (total_read < file_size) { - auto nread = read(fd, buffer.data(), block_size); - if (nread < 0) { - perror("read"); - return {}; - } + auto nread = TRY(Core::System::read(fd, buffer)); total_read += nread; } |