BSA_TestSceneGUI.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // Better Streaming Assets, Piotr Gwiazdowski <gwiazdorrr+github at gmail.com>, 2017
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using System.IO;
  8. using UnityEngine.Profiling;
  9. using System.Collections;
  10. using Stopwatch = System.Diagnostics.Stopwatch;
  11. using UnityEngine.Networking;
  12. #if UNITY_EDITOR
  13. using UnityEditor;
  14. #endif
  15. namespace Better.StreamingAssets
  16. {
  17. public class BSA_TestSceneGUI : MonoBehaviour
  18. {
  19. public UnityEngine.UI.Text InProgressText;
  20. public string EditorApkPath = "BetterStreamingAssetsTest.apk";
  21. public int RepetitionCount = 10;
  22. public bool LogToFile = false;
  23. private class CoroutineHost : MonoBehaviour { }
  24. private class TestInfo
  25. {
  26. public ReadMode readMode;
  27. public TestType testType;
  28. public string path;
  29. public int attempts;
  30. public Exception error;
  31. public TimeSpan duration;
  32. public long bytesRead;
  33. public long memoryPeak;
  34. public long maxMemoryPeak;
  35. }
  36. private delegate void TestResultDelegate(TimeSpan avgDuration, long avgBytesRead, long avgMemoryPeak, long maxMemoryPeak, string[] assetNames);
  37. [Flags]
  38. private enum ReadMode
  39. {
  40. BSA = 1 << 0,
  41. WWW = 1 << 1,
  42. Direct = 1 << 5,
  43. UnityWebRequest = 1 << 6
  44. }
  45. [Flags]
  46. private enum TestType
  47. {
  48. CheckIfExists = 1 << 0,
  49. LoadBytes = 1 << 1,
  50. }
  51. private string m_status = string.Empty;
  52. private TestType m_testModes = TestType.CheckIfExists;
  53. private ReadMode m_readModes = ReadMode.WWW;
  54. private CoroutineHost coroutineHost;
  55. private Vector2 m_assetsScroll;
  56. private Vector2 m_resultsScroll;
  57. private string[] m_allStreamingAssets = new string[0];
  58. private List<TestInfo> m_results = new List<TestInfo>();
  59. private HashSet<string> m_selectedPaths = new HashSet<string>();
  60. void OnEnable()
  61. {
  62. InProgressText.enabled = false;
  63. }
  64. void OnDisable()
  65. {
  66. InProgressText.enabled = true;
  67. }
  68. void OnGUI()
  69. {
  70. using (new GUILayout.AreaScope(new Rect(0, 0, Screen.width, Screen.height)))
  71. {
  72. if (string.IsNullOrEmpty(BetterStreamingAssets.Root))
  73. {
  74. #if UNITY_EDITOR
  75. using (new GUILayout.HorizontalScope())
  76. {
  77. GUILayout.Label("APK path");
  78. EditorApkPath = GUILayout.TextField(EditorApkPath);
  79. }
  80. if (GUILayout.Button("Use APK (Android like)"))
  81. {
  82. BetterStreamingAssets.InitializeWithExternalApk(EditorApkPath);
  83. Initialize();
  84. }
  85. if (GUILayout.Button("Use Assets/StreamingAssets directory (iOS/Standalone like)"))
  86. {
  87. BetterStreamingAssets.Initialize();
  88. Initialize();
  89. }
  90. return;
  91. #else
  92. BetterStreamingAssets.Initialize();
  93. Initialize();
  94. #endif
  95. }
  96. if (m_allStreamingAssets.Length == 0)
  97. {
  98. GUILayout.Label("No streaming assets found in " + BetterStreamingAssets.Root);
  99. return;
  100. }
  101. GUILayout.Label("Using " + BetterStreamingAssets.Root);
  102. GUILayout.Label("Discovered streaming assets:");
  103. using (var scope = new GUILayout.ScrollViewScope(m_assetsScroll, GUILayout.MaxHeight(300)))
  104. {
  105. m_assetsScroll = scope.scrollPosition;
  106. foreach (var path in m_allStreamingAssets)
  107. {
  108. var wasSelected = m_selectedPaths.Contains(path);
  109. if (GUILayout.Toggle(wasSelected, path))
  110. {
  111. if (!wasSelected)
  112. m_selectedPaths.Add(path);
  113. }
  114. else
  115. {
  116. if (wasSelected)
  117. m_selectedPaths.Remove(path);
  118. }
  119. }
  120. }
  121. const float VerticalSpace = 10.0f;
  122. GUILayout.Space(VerticalSpace);
  123. using (new GUILayout.HorizontalScope())
  124. {
  125. GUILayout.Label("Repetition count: " + RepetitionCount, GUILayout.Width(150.0f));
  126. RepetitionCount = Mathf.RoundToInt(GUILayout.HorizontalSlider((float)RepetitionCount, 1.0f, 20.0f));
  127. }
  128. LogToFile = GUILayout.Toggle(LogToFile, "Log results to file");
  129. GUILayout.Space(VerticalSpace);
  130. using (new GUILayout.HorizontalScope())
  131. {
  132. GUILayout.Label("Test modes: ", GUILayout.Width(150.0f));
  133. DoTestTypeToggle(TestType.CheckIfExists);
  134. DoTestTypeToggle(TestType.LoadBytes);
  135. }
  136. GUILayout.Space(VerticalSpace);
  137. using (new GUILayout.HorizontalScope())
  138. {
  139. GUILayout.Label("Read modes: ", GUILayout.Width(150.0f));
  140. DoReadModeToggle(ReadMode.BSA);
  141. DoReadModeToggle(ReadMode.WWW);
  142. DoReadModeToggle(ReadMode.UnityWebRequest);
  143. #if !UNITY_ANDROID || UNITY_EDITOR
  144. DoReadModeToggle(ReadMode.Direct);
  145. #endif
  146. }
  147. GUI.enabled = m_selectedPaths.Count > 0;
  148. if (GUILayout.Button("Test Selected Paths (" + m_selectedPaths.Count + ")"))
  149. {
  150. coroutineHost.StartCoroutine(TestAllCoroutine(m_selectedPaths.ToArray(), RepetitionCount, m_readModes, m_testModes, m_results));
  151. }
  152. GUI.enabled = true;
  153. GUILayout.Box(m_status);
  154. using (var scroll = new GUILayout.ScrollViewScope(m_resultsScroll))
  155. {
  156. m_resultsScroll = scroll.scrollPosition;
  157. GUI.skin.label.alignment = TextAnchor.MiddleLeft;
  158. GUI.skin.label.clipping = TextClipping.Clip;
  159. foreach (var result in m_results)
  160. {
  161. using (var layout = new GUILayout.HorizontalScope())
  162. {
  163. GUILayout.Label(result.path, GUILayout.Width(200));
  164. GUILayout.Label(result.readMode.ToString(), GUILayout.Width(160));
  165. GUILayout.Label(result.testType.ToString(), GUILayout.Width(160));
  166. if (result.error != null)
  167. {
  168. GUILayout.Label(result.error.GetType().ToString());
  169. }
  170. else
  171. {
  172. GUILayout.Label(result.duration.ToString());
  173. GUILayout.Label((result.memoryPeak / 1024.0 / 1024.0).ToString("F2") + " MB");
  174. }
  175. }
  176. }
  177. GUI.skin.label.clipping = TextClipping.Overflow;
  178. GUI.skin.label.alignment = TextAnchor.MiddleCenter;
  179. }
  180. }
  181. }
  182. private string StreamingAssetsPath
  183. {
  184. get
  185. {
  186. #if UNITY_EDITOR
  187. if (BetterStreamingAssets.Root == EditorApkPath)
  188. {
  189. return "jar:" + new Uri(EditorApkPath).AbsoluteUri + "!/assets";
  190. }
  191. #endif
  192. return Application.streamingAssetsPath;
  193. }
  194. }
  195. private void Initialize()
  196. {
  197. m_allStreamingAssets = BetterStreamingAssets.GetFiles("/", "*", SearchOption.AllDirectories);
  198. coroutineHost = gameObject.AddComponent<CoroutineHost>();
  199. // allocate something for mono heap to grow
  200. var bytes = new byte[200 * 1024 * 1024];
  201. Debug.LogFormat("Allocated {0}, mono heap size: {1}", bytes.Length, Profiler.GetMonoHeapSizeLong());
  202. }
  203. private void DoTestTypeToggle(TestType testMode)
  204. {
  205. bool wasSet = (m_testModes & testMode) == testMode;
  206. if (GUILayout.Toggle(wasSet, testMode.ToString()))
  207. {
  208. m_testModes |= testMode;
  209. }
  210. else if (wasSet)
  211. {
  212. if (m_testModes != testMode)
  213. m_testModes &= ~testMode;
  214. }
  215. }
  216. private void DoReadModeToggle(ReadMode readMode)
  217. {
  218. bool wasSet = (m_readModes & readMode) == readMode;
  219. if (GUILayout.Toggle(wasSet, readMode.ToString()))
  220. {
  221. m_readModes |= readMode;
  222. }
  223. else if (wasSet)
  224. {
  225. if (m_readModes != readMode)
  226. m_readModes &= ~readMode;
  227. }
  228. }
  229. private IEnumerator TestAllCoroutine(IEnumerable<string> paths, int attempts, ReadMode readModes, TestType testTypes, List<TestInfo> results)
  230. {
  231. LogWorkProgress("starting...");
  232. string logPath = Path.Combine(Application.persistentDataPath, "BSA_test_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + "_.csv");
  233. enabled = false;
  234. results.Clear();
  235. try
  236. {
  237. foreach (var path in paths)
  238. {
  239. foreach (ReadMode readMode in Enum.GetValues(typeof(ReadMode)))
  240. {
  241. if ((readMode & readModes) != readMode)
  242. continue;
  243. foreach (TestType testType in Enum.GetValues(typeof(TestType)))
  244. {
  245. if ((testType & testTypes) != testType)
  246. continue;
  247. var testInfo = new TestInfo()
  248. {
  249. readMode = readMode,
  250. testType = testType,
  251. path = path,
  252. attempts = attempts,
  253. };
  254. yield return coroutineHost.StartCoroutine(ErrorCatchingCoroutine(TestHarness(readMode, path, testType, attempts,
  255. (duration, bytes, memory, maxMemory, names) =>
  256. {
  257. testInfo.duration = duration;
  258. testInfo.bytesRead = bytes;
  259. testInfo.memoryPeak = memory;
  260. testInfo.maxMemoryPeak = maxMemory;
  261. }),
  262. ex =>
  263. {
  264. testInfo.error = ex;
  265. }
  266. ));
  267. results.Add(testInfo);
  268. }
  269. }
  270. }
  271. }
  272. finally
  273. {
  274. enabled = true;
  275. if (LogToFile)
  276. {
  277. using (var writer = File.CreateText(logPath))
  278. {
  279. foreach (var result in results)
  280. {
  281. string errorMessage = string.Empty;
  282. if (result.error != null)
  283. errorMessage = result.error.ToString().Replace(Environment.NewLine, ";");
  284. writer.WriteLine("\"{0}\"\t{1}\t{2}\t{3}\t{4}\t\"{5}\"", result.path, result.readMode, result.testType, result.duration, result.memoryPeak, errorMessage);
  285. }
  286. }
  287. LogWorkProgress("Logged at: " + logPath);
  288. }
  289. }
  290. }
  291. private void LogWorkProgress(string status)
  292. {
  293. Debug.Log("WORK PROGRESS: " + status);
  294. if (string.IsNullOrEmpty(m_status))
  295. m_status = status;
  296. else
  297. m_status += "\n" + status;
  298. }
  299. private IEnumerator ErrorCatchingCoroutine(IEnumerator inner, Action<System.Exception> onError)
  300. {
  301. m_status = string.Empty;
  302. for (; ; )
  303. {
  304. bool next = false;
  305. try
  306. {
  307. next = inner.MoveNext();
  308. }
  309. catch (System.Exception ex)
  310. {
  311. onError(ex);
  312. break;
  313. }
  314. if (!next)
  315. break;
  316. yield return inner.Current;
  317. }
  318. }
  319. private IEnumerator TestHarness(ReadMode readMode, string path, TestType testType, int attempts, TestResultDelegate callback)
  320. {
  321. var stopwatch = new Stopwatch();
  322. string[] assetNames = null;
  323. var streamingAssetsUrl = Path.Combine(StreamingAssetsPath, path.TrimStart('/')).Replace('\\', '/');
  324. long bytesRead = 0;
  325. long maxMemoryPeak = 0;
  326. long totalMemoryPeaks = 0;
  327. for (int i = 0; i < attempts; ++i)
  328. {
  329. IDisposable toDispose = null;
  330. yield return Resources.UnloadUnusedAssets();
  331. GC.Collect();
  332. GC.WaitForPendingFinalizers();
  333. yield return null;
  334. var memoryUnityBefore = Profiler.GetTotalAllocatedMemoryLong();
  335. //var memoryMonoBefore = Profiler.GetMonoUsedSizeLong();
  336. stopwatch.Start();
  337. if (readMode == ReadMode.WWW)
  338. {
  339. #pragma warning disable 0618 // Type or member is obsolete
  340. var www = new WWW(streamingAssetsUrl);
  341. #pragma warning restore 0618 // Type or member is obsolete
  342. toDispose = www;
  343. {
  344. yield return www;
  345. Profiler.BeginSample(testType.ToString());
  346. switch (testType)
  347. {
  348. case TestType.CheckIfExists:
  349. if (!string.IsNullOrEmpty(www.error))
  350. throw new System.Exception(www.error);
  351. break;
  352. case TestType.LoadBytes:
  353. bytesRead += www.bytes.Length;
  354. break;
  355. default:
  356. throw new NotSupportedException();
  357. }
  358. Profiler.EndSample();
  359. }
  360. }
  361. else if (readMode == ReadMode.BSA)
  362. {
  363. Profiler.BeginSample(testType.ToString());
  364. switch (testType)
  365. {
  366. case TestType.CheckIfExists:
  367. if (!BetterStreamingAssets.FileExists(path))
  368. throw new System.InvalidOperationException();
  369. break;
  370. case TestType.LoadBytes:
  371. bytesRead += BetterStreamingAssets.ReadAllBytes(path).Length;
  372. break;
  373. }
  374. Profiler.EndSample();
  375. }
  376. else if (readMode == ReadMode.Direct)
  377. {
  378. var p = streamingAssetsUrl;
  379. Profiler.BeginSample(testType.ToString());
  380. switch (testType)
  381. {
  382. case TestType.CheckIfExists:
  383. if (!File.Exists(p))
  384. throw new System.InvalidOperationException();
  385. break;
  386. case TestType.LoadBytes:
  387. bytesRead += File.ReadAllBytes(p).Length;
  388. break;
  389. }
  390. Profiler.EndSample();
  391. }
  392. else if (readMode == ReadMode.UnityWebRequest)
  393. {
  394. var www = UnityEngine.Networking.UnityWebRequest.Get(streamingAssetsUrl);
  395. toDispose = www;
  396. #pragma warning disable 0618 // Type or member is obsolete
  397. yield return www.Send();
  398. #pragma warning restore 0618 // Type or member is obsolete
  399. Profiler.BeginSample(testType.ToString());
  400. switch (testType)
  401. {
  402. case TestType.CheckIfExists:
  403. if (!string.IsNullOrEmpty(www.error))
  404. throw new System.Exception(www.error);
  405. break;
  406. case TestType.LoadBytes:
  407. bytesRead += (int)www.downloadedBytes;
  408. break;
  409. default:
  410. throw new NotSupportedException();
  411. }
  412. Profiler.EndSample();
  413. }
  414. stopwatch.Stop();
  415. var memoryPeak = Math.Max(0, Profiler.GetTotalAllocatedMemoryLong() - memoryUnityBefore);
  416. // + Math.Max(0, Profiler.GetMonoUsedSizeLong() - memoryMonoBefore);
  417. maxMemoryPeak = System.Math.Max(memoryPeak, maxMemoryPeak);
  418. totalMemoryPeaks += memoryPeak;
  419. yield return null;
  420. if (toDispose != null)
  421. toDispose.Dispose();
  422. yield return null;
  423. }
  424. yield return Resources.UnloadUnusedAssets();
  425. GC.Collect();
  426. GC.WaitForPendingFinalizers();
  427. yield return null;
  428. callback(new TimeSpan(stopwatch.ElapsedTicks / attempts), bytesRead / attempts, totalMemoryPeaks / attempts, maxMemoryPeak, assetNames);
  429. }
  430. }
  431. }