Change clock() in benchmarks to clock_gettime()
For more accurate benchmarks.
Bug: 263765185
Test: atest MicrodroidTestApp AuthFsBenchmarks
Change-Id: Iebb2517fea4cb9e12f3996254cdfd82f349a35de
diff --git a/authfs/tests/benchmarks/src/measure_io.cpp b/authfs/tests/benchmarks/src/measure_io.cpp
index e1f2fb8..e766664 100644
--- a/authfs/tests/benchmarks/src/measure_io.cpp
+++ b/authfs/tests/benchmarks/src/measure_io.cpp
@@ -55,7 +55,10 @@
}
char buf[kBlockSizeBytes];
- clock_t start = clock();
+ struct timespec start;
+ if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) {
+ err(EXIT_FAILURE, "failed to clock_gettime");
+ }
for (auto i = 0; i < block_count; ++i) {
auto bytes = is_read ? pread(fd, buf, kBlockSizeBytes, offsets[i])
: pwrite(fd, buf, kBlockSizeBytes, offsets[i]);
@@ -69,7 +72,11 @@
// Writes all the buffered modifications to the open file.
assert(syncfs(fd) == 0);
}
- double elapsed_seconds = ((double)clock() - start) / CLOCKS_PER_SEC;
+ struct timespec finish;
+ if (clock_gettime(CLOCK_MONOTONIC, &finish) == -1) {
+ err(EXIT_FAILURE, "failed to clock_gettime");
+ }
+ double elapsed_seconds = finish.tv_sec - start.tv_sec + (finish.tv_nsec - start.tv_nsec) / 1e9;
double rate = (double)file_size_mb / elapsed_seconds;
std::cout << std::setprecision(12) << rate << std::endl;
diff --git a/tests/benchmark/src/jni/io_vsock_host_jni.cpp b/tests/benchmark/src/jni/io_vsock_host_jni.cpp
index dd32e29..47d5325 100644
--- a/tests/benchmark/src/jni/io_vsock_host_jni.cpp
+++ b/tests/benchmark/src/jni/io_vsock_host_jni.cpp
@@ -20,6 +20,7 @@
#include <jni.h>
#include <time.h>
+using android::base::ErrnoError;
using android::base::Error;
using android::base::Result;
using android::base::WriteStringToFd;
@@ -29,12 +30,18 @@
Result<double> measure_send_rate(int fd, int num_bytes_to_send) {
std::string data;
data.assign(num_bytes_to_send, 'a');
- clock_t start = clock();
+ struct timespec start;
+ if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) {
+ return ErrnoError() << "failed to clock_gettime";
+ }
if (!WriteStringToFd(data, fd)) {
return Error() << "Cannot send data to client";
}
- clock_t end = clock();
- double elapsed_seconds = (double)(end - start) / CLOCKS_PER_SEC;
+ struct timespec finish;
+ if (clock_gettime(CLOCK_MONOTONIC, &finish) == -1) {
+ return ErrnoError() << "failed to clock_gettime";
+ }
+ double elapsed_seconds = finish.tv_sec - start.tv_sec + (finish.tv_nsec - start.tv_nsec) / 1e9;
LOG(INFO) << "Host:Finished sending data in " << elapsed_seconds << " seconds.";
double send_rate = (double)num_bytes_to_send / kNumBytesPerMB / elapsed_seconds;
return {send_rate};
diff --git a/tests/benchmark/src/native/benchmarkbinary.cpp b/tests/benchmark/src/native/benchmarkbinary.cpp
index 56963e6..6cfc71d 100644
--- a/tests/benchmark/src/native/benchmarkbinary.cpp
+++ b/tests/benchmark/src/native/benchmarkbinary.cpp
@@ -118,7 +118,10 @@
}
char buf[kBlockSizeBytes];
- clock_t start = clock();
+ struct timespec start;
+ if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) {
+ return ErrnoError() << "failed to clock_gettime";
+ }
unique_fd fd(open(filename.c_str(), O_RDONLY | O_CLOEXEC));
if (fd.get() == -1) {
return ErrnoError() << "Read: opening " << filename << " failed";
@@ -131,7 +134,12 @@
return ErrnoError() << "failed to read";
}
}
- double elapsed_seconds = ((double)clock() - start) / CLOCKS_PER_SEC;
+ struct timespec finish;
+ if (clock_gettime(CLOCK_MONOTONIC, &finish) == -1) {
+ return ErrnoError() << "failed to clock_gettime";
+ }
+ double elapsed_seconds =
+ finish.tv_sec - start.tv_sec + (finish.tv_nsec - start.tv_nsec) / 1e9;
double file_size_mb = (double)file_size_bytes / kNumBytesPerMB;
return {file_size_mb / elapsed_seconds};
}