Reland "iOS: Save perf results under Documents/perf_result.json"

This will require a manual roll to downstream projects, since
the //test:perf_test target was introduced.

This is a reland of 10a8e7a9b5261a7e3ce19900ba3511be3b5911f8
Original change's description:
> iOS: Save perf results under Documents/perf_result.json
>
> TBR=henrika@webrtc.org
>
> Bug: webrtc:7156
> Change-Id: Ib00992cce0007e0b5c9274340df1a892f810b0c5
> Reviewed-on: https://webrtc-review.googlesource.com/29202
> Commit-Queue: Edward Lemur <ehmaldonado@webrtc.org>
> Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
> Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#21244}

R=henrika@webrtc.org, phoglund@webrtc.org

Bug: webrtc:7156
Change-Id: I85fc7bc5fce0894af90017b71b9952b61b523424
Reviewed-on: https://webrtc-review.googlesource.com/37643
Reviewed-by: Patrik Höglund <phoglund@google.com>
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Commit-Queue: Edward Lemur <ehmaldonado@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21518}
diff --git a/test/ios/test_support.h b/test/ios/test_support.h
index 7bc2993..0178fca 100644
--- a/test/ios/test_support.h
+++ b/test/ios/test_support.h
@@ -17,7 +17,8 @@
 // This is necessary as iOS doesn't like processes without a gui
 // running for longer than a few seconds.
 void RunTestsFromIOSApp();
-void InitTestSuite(int (*test_suite)(void), int argc, char* argv[]);
+void InitTestSuite(int (*test_suite)(void), int argc, char* argv[],
+                   bool save_chartjson_result);
 
 }  // namespace test
 }  // namespace rtc
diff --git a/test/ios/test_support.mm b/test/ios/test_support.mm
index 603bc8c..5b7f2f9 100644
--- a/test/ios/test_support.mm
+++ b/test/ios/test_support.mm
@@ -11,8 +11,10 @@
 #import <UIKit/UIKit.h>
 
 #include "test/ios/test_support.h"
+#include "test/testsupport/perf_test.h"
 
 #import "sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.h"
+#include "sdk/objc/Framework/Classes/Common/helpers.h"
 
 // Springboard will kill any iOS app that fails to check in after launch within
 // a given time. Starting a UIApplication before invoking TestSuite::Run
@@ -31,6 +33,7 @@
 static int (*g_test_suite)(void) = NULL;
 static int g_argc;
 static char **g_argv;
+static bool g_save_chartjson_result;
 
 @interface UIApplication (Testing)
 - (void)_terminateWithStatus:(int)status;
@@ -75,6 +78,20 @@
 - (void)runTests {
   int exitStatus = g_test_suite();
 
+  if (g_save_chartjson_result) {
+    // Stores data into a json file under the app's document directory.
+    NSString* fileName = @"perf_result.json";
+    NSArray<NSString*>* outputDirectories = NSSearchPathForDirectoriesInDomains(
+        NSDocumentDirectory, NSUserDomainMask, YES);
+    if ([outputDirectories count] != 0) {
+      NSString* outputPath =
+          [outputDirectories[0] stringByAppendingPathComponent:fileName];
+
+      webrtc::test::WritePerfResults(
+          webrtc::ios::StdStringFromNSString(outputPath));
+    }
+  }
+
   // If a test app is too fast, it will exit before Instruments has has a
   // a chance to initialize and no test results will be seen.
   // TODO(crbug.com/137010): Figure out how much time is actually needed, and
@@ -93,10 +110,14 @@
 namespace rtc {
 namespace test {
 
-void InitTestSuite(int (*test_suite)(void), int argc, char *argv[]) {
+// Note: This is not thread safe, and must be called from the same thread as
+// runTests above.
+void InitTestSuite(int (*test_suite)(void), int argc, char *argv[],
+                   bool save_chartjson_result) {
   g_test_suite = test_suite;
   g_argc = argc;
   g_argv = argv;
+  g_save_chartjson_result = save_chartjson_result;
 }
 
 void RunTestsFromIOSApp() {