Write protos as binary.

We need to write protos as "wb" and not "w", otherwise we get CRLF
on Windows which corrupts the proto.

Bug: chromium:1029452
Change-Id: Iabf841405134d7bc2523ac48219ca7cb9d8214c1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/170320
Commit-Queue: Patrik Höglund <phoglund@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30772}
This commit is contained in:
Patrik Höglund
2020-03-12 09:39:40 +01:00
committed by Commit Bot
parent f092e4d0ff
commit b8e69efcee
7 changed files with 41 additions and 15 deletions

View File

@ -110,7 +110,7 @@ std::string PerfResultsOutputFile() {
void LogTestResults() {
std::string perf_results_output_file = PerfResultsOutputFile();
webrtc::test::WritePerfResults(perf_results_output_file);
EXPECT_TRUE(webrtc::test::WritePerfResults(perf_results_output_file));
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();

View File

@ -861,7 +861,7 @@ TEST(IsacFixTest, Kenny) {
(runtime * 10000) / length_file, "us", false);
if (chartjson_result_file) {
webrtc::test::WritePerfResults(chartjson_result_file);
EXPECT_TRUE(webrtc::test::WritePerfResults(chartjson_result_file));
}
fclose(inp);

View File

@ -164,7 +164,9 @@ int main(int argc, char* argv[]) {
std::string chartjson_result_file =
absl::GetFlag(FLAGS_chartjson_result_file);
if (!chartjson_result_file.empty()) {
webrtc::test::WritePerfResults(chartjson_result_file);
if (!webrtc::test::WritePerfResults(chartjson_result_file)) {
return 1;
}
}
std::string aligned_output_file = absl::GetFlag(FLAGS_aligned_output_file);
if (!aligned_output_file.empty()) {

View File

@ -33,7 +33,7 @@
static int (*g_test_suite)(void) = NULL;
static int g_argc;
static char **g_argv;
static bool g_save_chartjson_result;
static bool g_write_perf_output;
static absl::optional<std::vector<std::string>> g_metrics_to_plot;
@interface UIApplication (Testing)
@ -76,8 +76,10 @@ static absl::optional<std::vector<std::string>> g_metrics_to_plot;
int exitStatus = g_test_suite();
if (g_save_chartjson_result) {
if (g_write_perf_output) {
// Stores data into a json file under the app's document directory.
// TODO(https://crbug.com/1029452): Change ext to .pb when histograms are
// the default.
NSString* fileName = @"perf_result.json";
NSArray<NSString*>* outputDirectories = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
@ -85,8 +87,9 @@ static absl::optional<std::vector<std::string>> g_metrics_to_plot;
NSString* outputPath =
[outputDirectories[0] stringByAppendingPathComponent:fileName];
webrtc::test::WritePerfResults(
[NSString stdStringForString:outputPath]);
if (!webrtc::test::WritePerfResults([NSString stdStringForString:outputPath])) {
exit(1);
}
}
}
if (g_metrics_to_plot) {
@ -121,7 +124,7 @@ void InitTestSuite(int (*test_suite)(void),
g_test_suite = test_suite;
g_argc = argc;
g_argv = argv;
g_save_chartjson_result = save_chartjson_result;
g_write_perf_output = save_chartjson_result;
g_metrics_to_plot = std::move(metrics_to_plot);
}

View File

@ -181,7 +181,9 @@ class TestMainImpl : public TestMain {
std::string chartjson_result_file =
absl::GetFlag(FLAGS_isolated_script_test_perf_output);
if (!chartjson_result_file.empty()) {
webrtc::test::WritePerfResults(chartjson_result_file);
if (!webrtc::test::WritePerfResults(chartjson_result_file)) {
return 1;
}
}
if (metrics_to_plot) {
webrtc::test::PrintPlottableResults(*metrics_to_plot);

View File

@ -220,11 +220,29 @@ void PrintPlottableResults(const std::vector<std::string>& desired_graphs) {
GetPlottableCounterPrinter().Print(desired_graphs);
}
void WritePerfResults(const std::string& output_path) {
bool WritePerfResults(const std::string& output_path) {
std::string results = GetPerfResults();
std::fstream output(output_path, std::fstream::out);
output << results;
output.close();
FILE* output;
if (absl::GetFlag(FLAGS_write_histogram_proto_json)) {
output = fopen(output_path.c_str(), "wb");
} else {
output = fopen(output_path.c_str(), "w");
}
if (output == NULL) {
printf("Failed to write to %s.\n", output_path.c_str());
return false;
}
size_t written =
fwrite(results.c_str(), sizeof(char), results.size(), output);
fclose(output);
if (written != results.size()) {
long expected = results.size();
printf("Wrote %zu, tried to write %lu\n", written, expected);
return false;
}
return true;
}
void PrintResult(const std::string& measurement,

View File

@ -105,8 +105,9 @@ std::string GetPerfResults();
// they will be skipped.
void PrintPlottableResults(const std::vector<std::string>& desired_graphs);
// Call GetPerfResults() and write its output to a file.
void WritePerfResults(const std::string& output_path);
// Call GetPerfResults() and write its output to a file. Returns false if we
// failed to write to the file.
bool WritePerfResults(const std::string& output_path);
// By default, perf results are printed to stdout. Set the FILE* to where they
// should be printing instead.