mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-07 19:57:45 +03:00
disk_benchmark: Modernize this code a little bit
Use the new formatting helpers and such.
This commit is contained in:
parent
19083fd760
commit
1318b9391d
Notes:
sideshowbarker
2024-07-18 21:54:57 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1318b9391d5
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -25,6 +25,7 @@
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/ScopeGuard.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Vector.h>
|
||||
@ -33,13 +34,12 @@
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct Result {
|
||||
u64 write_bps;
|
||||
u64 read_bps;
|
||||
u64 write_bps {};
|
||||
u64 read_bps {};
|
||||
};
|
||||
|
||||
static Result average_result(const Vector<Result>& results)
|
||||
@ -59,18 +59,18 @@ static Result average_result(const Vector<Result>& results)
|
||||
|
||||
static void exit_with_usage(int rc)
|
||||
{
|
||||
fprintf(stderr, "Usage: disk_benchmark [-h] [-d directory] [-t time_per_benchmark] [-f file_size1,file_size2,...] [-b block_size1,block_size2,...]\n");
|
||||
warnln("Usage: disk_benchmark [-h] [-d directory] [-t time_per_benchmark] [-f file_size1,file_size2,...] [-b block_size1,block_size2,...]");
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
static Result benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache);
|
||||
static Optional<Result> benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char* directory = strdup(".");
|
||||
String directory = ".";
|
||||
int time_per_benchmark = 10;
|
||||
Vector<int> file_sizes;
|
||||
Vector<int> block_sizes;
|
||||
Vector<size_t> file_sizes;
|
||||
Vector<size_t> block_sizes;
|
||||
bool allow_cache = false;
|
||||
|
||||
int opt;
|
||||
@ -83,17 +83,17 @@ int main(int argc, char** argv)
|
||||
allow_cache = true;
|
||||
break;
|
||||
case 'd':
|
||||
directory = strdup(optarg);
|
||||
directory = optarg;
|
||||
break;
|
||||
case 't':
|
||||
time_per_benchmark = atoi(optarg);
|
||||
break;
|
||||
case 'f':
|
||||
for (auto size : String(optarg).split(','))
|
||||
for (const auto& size : String(optarg).split(','))
|
||||
file_sizes.append(atoi(size.characters()));
|
||||
break;
|
||||
case 'b':
|
||||
for (auto size : String(optarg).split(','))
|
||||
for (const auto& size : String(optarg).split(','))
|
||||
block_sizes.append(atoi(size.characters()));
|
||||
break;
|
||||
}
|
||||
@ -116,32 +116,31 @@ int main(int argc, char** argv)
|
||||
continue;
|
||||
|
||||
auto buffer = ByteBuffer::create_uninitialized(block_size);
|
||||
|
||||
Vector<Result> results;
|
||||
|
||||
printf("Running: file_size=%d block_size=%d\n", file_size, block_size);
|
||||
outln("Running: file_size={} block_size={}", file_size, block_size);
|
||||
Core::ElapsedTimer timer;
|
||||
timer.start();
|
||||
while (timer.elapsed() < time_per_benchmark * 1000) {
|
||||
printf(".");
|
||||
out(".");
|
||||
fflush(stdout);
|
||||
results.append(benchmark(filename, file_size, block_size, buffer, allow_cache));
|
||||
auto result = benchmark(filename, file_size, block_size, buffer, allow_cache);
|
||||
if (!result.has_value())
|
||||
return 1;
|
||||
results.append(result.release_value());
|
||||
usleep(100);
|
||||
}
|
||||
auto average = average_result(results);
|
||||
printf("\nFinished: runs=%zu time=%dms write_bps=%llu read_bps=%llu\n", results.size(), timer.elapsed(), average.write_bps, average.read_bps);
|
||||
outln("Finished: runs={} time={}ms write_bps={} read_bps={}", results.size(), timer.elapsed(), average.write_bps, average.read_bps);
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (isatty(0)) {
|
||||
printf("Press any key to exit...\n");
|
||||
fgetc(stdin);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache)
|
||||
Optional<Result> benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache)
|
||||
{
|
||||
int flags = O_CREAT | O_TRUNC | O_RDWR;
|
||||
if (!allow_cache)
|
||||
@ -153,56 +152,46 @@ Result benchmark(const String& filename, int file_size, int block_size, ByteBuff
|
||||
exit(1);
|
||||
}
|
||||
|
||||
auto cleanup_and_exit = [fd, filename]() {
|
||||
close(fd);
|
||||
unlink(filename.characters());
|
||||
exit(1);
|
||||
};
|
||||
auto fd_cleanup = ScopeGuard([fd, filename] {
|
||||
if (close(fd) < 0)
|
||||
perror("close");
|
||||
if (unlink(filename.characters()) < 0)
|
||||
perror("unlink");
|
||||
});
|
||||
|
||||
Result res;
|
||||
Result result;
|
||||
|
||||
Core::ElapsedTimer timer;
|
||||
|
||||
timer.start();
|
||||
int nwrote = 0;
|
||||
for (int j = 0; j < file_size; j += block_size) {
|
||||
int n = write(fd, buffer.data(), block_size);
|
||||
if (n < 0) {
|
||||
|
||||
ssize_t total_written = 0;
|
||||
for (ssize_t j = 0; j < file_size; j += block_size) {
|
||||
auto nwritten = write(fd, buffer.data(), block_size);
|
||||
if (nwritten < 0) {
|
||||
perror("write");
|
||||
cleanup_and_exit();
|
||||
return {};
|
||||
}
|
||||
nwrote += n;
|
||||
total_written += nwritten;
|
||||
}
|
||||
|
||||
res.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) {
|
||||
perror("lseek");
|
||||
cleanup_and_exit();
|
||||
return {};
|
||||
}
|
||||
|
||||
timer.start();
|
||||
int nread = 0;
|
||||
while (nread < file_size) {
|
||||
int n = read(fd, buffer.data(), block_size);
|
||||
if (n < 0) {
|
||||
ssize_t total_read = 0;
|
||||
while (total_read < file_size) {
|
||||
auto nread = read(fd, buffer.data(), block_size);
|
||||
if (nread < 0) {
|
||||
perror("read");
|
||||
cleanup_and_exit();
|
||||
return {};
|
||||
}
|
||||
nread += n;
|
||||
total_read += nread;
|
||||
}
|
||||
|
||||
res.read_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000;
|
||||
|
||||
if (close(fd) != 0) {
|
||||
perror("close");
|
||||
cleanup_and_exit();
|
||||
}
|
||||
|
||||
if (unlink(filename.characters()) != 0) {
|
||||
perror("unlink");
|
||||
cleanup_and_exit();
|
||||
}
|
||||
|
||||
return res;
|
||||
result.read_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000;
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user