Optimize vsock benchmark fixture
ReadFdToString reads only 1kb at a time + performs an extra copy.
Test: atest "com.android.microdroid.benchmark.MicrodroidBenchmarks#testVsockTransferFromHostToVM"
Change-Id: I4c3e13e3eecf59dd132100299d4eb600fd586e84
diff --git a/tests/benchmark/src/native/io_vsock.cpp b/tests/benchmark/src/native/io_vsock.cpp
index 8edc7c8..b1a4c96 100644
--- a/tests/benchmark/src/native/io_vsock.cpp
+++ b/tests/benchmark/src/native/io_vsock.cpp
@@ -59,15 +59,25 @@
}
LOG(INFO) << "VM:Connection from CID " << client_sa.svm_cid << " on port "
<< client_sa.svm_port;
- std::string data;
- if (!ReadFdToString(client_fd, &data)) {
- return Error() << "Cannot get data from the host.";
+
+ ssize_t total = 0;
+ char buf[4096];
+ for (;;) {
+ ssize_t n = TEMP_FAILURE_RETRY(read(client_fd.get(), buf, sizeof(buf)));
+ if (n < 0) {
+ return Error() << "Cannot get data from the host.";
+ }
+ if (n == 0) {
+ break;
+ }
+ total += n;
}
- if (data.length() != num_bytes_to_receive) {
- return Error() << "Received data length(" << data.length() << ") is not equal to "
+
+ if (total != num_bytes_to_receive) {
+ return Error() << "Received data length(" << total << ") is not equal to "
<< num_bytes_to_receive;
}
LOG(INFO) << "VM:Finished reading data.";
return {};
}
-} // namespace io_vsock
\ No newline at end of file
+} // namespace io_vsock