Skip to content

Commit 424921e

Browse files
committed
Added performance test for exporting large mesh
1 parent 2bb2c57 commit 424921e

File tree

3 files changed

+182
-78
lines changed

3 files changed

+182
-78
lines changed

Assets/FbxExporters/Editor/UnitTests/DefaultSelectionTest.cs

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,14 @@ namespace FbxExporters.UnitTests
1313
/// Tests that the right GameObjects are exported and
1414
/// that they have the expected transforms.
1515
/// </summary>
16-
public class DefaultSelectionTest
16+
public class DefaultSelectionTest : ExporterTestBase
1717
{
18-
private string _filePath;
19-
protected string filePath { get { return string.IsNullOrEmpty(_filePath) ? Application.dataPath : _filePath; } set { _filePath = value; } }
20-
21-
private string _fileNamePrefix;
22-
protected string fileNamePrefix { get { return string.IsNullOrEmpty(_fileNamePrefix) ? "_safe_to_delete__" : _fileNamePrefix; }
23-
set { _fileNamePrefix = value; } }
24-
25-
private string _fileNameExt;
26-
protected string fileNameExt { get { return string.IsNullOrEmpty(_fileNameExt) ? ".fbx" : _fileNameExt; } set { _fileNameExt = value; } }
27-
28-
private string MakeFileName(string baseName = null, string prefixName = null, string extName = null)
29-
{
30-
if (baseName==null)
31-
baseName = Path.GetRandomFileName();
32-
33-
if (prefixName==null)
34-
prefixName = this.fileNamePrefix;
35-
36-
if (extName==null)
37-
extName = this.fileNameExt;
38-
39-
return prefixName + baseName + extName;
40-
}
41-
42-
protected string GetRandomFileNamePath(string pathName = null, string prefixName = null, string extName = null)
43-
{
44-
string temp;
45-
46-
if (pathName==null)
47-
pathName = this.filePath;
48-
49-
if (prefixName==null)
50-
prefixName = this.fileNamePrefix;
51-
52-
if (extName==null)
53-
extName = this.fileNameExt;
54-
55-
// repeat until you find a file that does not already exist
56-
do {
57-
temp = Path.Combine (pathName, MakeFileName(prefixName: prefixName, extName: extName));
58-
59-
} while(File.Exists (temp));
60-
61-
return temp;
62-
}
63-
6418
protected GameObject m_root;
6519

6620
[TearDown]
67-
public void Term ()
21+
public override void Term ()
6822
{
69-
foreach (string file in Directory.GetFiles (this.filePath, MakeFileName("*"))) {
70-
File.Delete (file);
71-
}
23+
base.Term ();
7224
if (m_root) {
7325
UnityEngine.Object.DestroyImmediate (m_root);
7426
}
@@ -212,7 +164,7 @@ private GameObject CreateGameObject(string name, Transform parent = null)
212164
var go = new GameObject (name);
213165
go.transform.SetParent (parent);
214166
return go;
215-
}
167+
}
216168

217169
private void CompareHierarchies(
218170
GameObject expectedHierarchy, GameObject actualHierarchy,
@@ -256,31 +208,5 @@ private void CompareHierarchies(GameObject[] expectedHierarchy, GameObject[] act
256208
CompareGlobalTransform (actualHierarchy [i].transform, expectedHierarchy [i].transform);
257209
}
258210
}
259-
260-
private GameObject ExportSelection(Object[] selected)
261-
{
262-
// export selected to a file, then return the root
263-
var filename = GetRandomFileNamePath();
264-
265-
Debug.unityLogger.logEnabled = false;
266-
var fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filename, selected) as string;
267-
Debug.unityLogger.logEnabled = true;
268-
269-
Assert.IsNotNull (fbxFileName);
270-
271-
// make filepath relative to project folder
272-
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
273-
{
274-
fbxFileName = "Assets" + fbxFileName.Substring (Application.dataPath.Length);
275-
}
276-
// refresh the assetdata base so that we can query for the model
277-
AssetDatabase.Refresh ();
278-
279-
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath (fbxFileName);
280-
var fbxRoot = unityMainAsset as GameObject;
281-
282-
Assert.IsNotNull (fbxRoot);
283-
return fbxRoot;
284-
}
285211
}
286212
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System.Collections;
6+
using System.Diagnostics;
7+
8+
namespace FbxExporters.UnitTests
9+
{
10+
public class ExportPerformanceTest : ExporterTestBase
11+
{
12+
13+
private const int NumMeshesToCombine = 15;
14+
15+
// Export should not take longer than this, otherwise test fails
16+
private const long PerformanceThresholdMilliseconds = 30000;
17+
private Stopwatch m_stopwatch;
18+
private GameObject m_toExport;
19+
20+
[SetUp]
21+
public void Init()
22+
{
23+
m_stopwatch = new Stopwatch ();
24+
m_toExport = CreateGameObjectToExport ();
25+
}
26+
27+
[TearDown]
28+
public override void Term()
29+
{
30+
base.Term ();
31+
GameObject.DestroyImmediate (m_toExport);
32+
}
33+
34+
35+
/// <summary>
36+
/// Creates a GameObject containing a very large mesh to export.
37+
/// </summary>
38+
/// <returns>The game object to export.</returns>
39+
private GameObject CreateGameObjectToExport ()
40+
{
41+
CombineInstance[] combine = new CombineInstance[NumMeshesToCombine];
42+
GameObject spheres = GameObject.CreatePrimitive (PrimitiveType.Sphere);
43+
44+
Transform sphereTransform = spheres.transform;
45+
MeshFilter sphereMeshFilter = spheres.GetComponent<MeshFilter> ();
46+
Assert.IsNotNull (sphereMeshFilter);
47+
48+
for (int i = 0; i < NumMeshesToCombine; i++) {
49+
combine [i].mesh = sphereMeshFilter.sharedMesh;
50+
sphereTransform.position = new Vector3 (i, i, i);
51+
combine [i].transform = sphereTransform.localToWorldMatrix;
52+
}
53+
54+
sphereMeshFilter.sharedMesh = new Mesh ();
55+
sphereMeshFilter.sharedMesh.name = "Spheres Mesh";
56+
sphereMeshFilter.sharedMesh.CombineMeshes (combine);
57+
return spheres;
58+
}
59+
60+
[Test]
61+
public void TestPerformance ()
62+
{
63+
Assert.IsNotNull (m_toExport);
64+
65+
var filename = GetRandomFileNamePath();
66+
67+
UnityEngine.Debug.unityLogger.logEnabled = false;
68+
69+
m_stopwatch.Reset ();
70+
m_stopwatch.Start ();
71+
var fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filename, new Object[]{m_toExport}) as string;
72+
m_stopwatch.Stop ();
73+
74+
UnityEngine.Debug.unityLogger.logEnabled = true;
75+
76+
Assert.IsNotNull (fbxFileName);
77+
Assert.LessOrEqual(m_stopwatch.ElapsedMilliseconds, PerformanceThresholdMilliseconds);
78+
}
79+
}
80+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System.Collections;
6+
using System.IO;
7+
using FbxSdk;
8+
9+
namespace FbxExporters.UnitTests
10+
{
11+
public abstract class ExporterTestBase
12+
{
13+
private string _filePath;
14+
protected string filePath { get { return string.IsNullOrEmpty(_filePath) ? Application.dataPath : _filePath; } set { _filePath = value; } }
15+
16+
private string _fileNamePrefix;
17+
protected string fileNamePrefix { get { return string.IsNullOrEmpty(_fileNamePrefix) ? "_safe_to_delete__" : _fileNamePrefix; }
18+
set { _fileNamePrefix = value; } }
19+
20+
private string _fileNameExt;
21+
protected string fileNameExt { get { return string.IsNullOrEmpty(_fileNameExt) ? ".fbx" : _fileNameExt; } set { _fileNameExt = value; } }
22+
23+
private string MakeFileName(string baseName = null, string prefixName = null, string extName = null)
24+
{
25+
if (baseName==null)
26+
baseName = Path.GetRandomFileName();
27+
28+
if (prefixName==null)
29+
prefixName = this.fileNamePrefix;
30+
31+
if (extName==null)
32+
extName = this.fileNameExt;
33+
34+
return prefixName + baseName + extName;
35+
}
36+
37+
protected string GetRandomFileNamePath(string pathName = null, string prefixName = null, string extName = null)
38+
{
39+
string temp;
40+
41+
if (pathName==null)
42+
pathName = this.filePath;
43+
44+
if (prefixName==null)
45+
prefixName = this.fileNamePrefix;
46+
47+
if (extName==null)
48+
extName = this.fileNameExt;
49+
50+
// repeat until you find a file that does not already exist
51+
do {
52+
temp = Path.Combine (pathName, MakeFileName(prefixName: prefixName, extName: extName));
53+
54+
} while(File.Exists (temp));
55+
56+
return temp;
57+
}
58+
59+
[TearDown]
60+
public virtual void Term ()
61+
{
62+
foreach (string file in Directory.GetFiles (this.filePath, MakeFileName("*"))) {
63+
File.Delete (file);
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Exports the Objects in selected.
69+
/// </summary>
70+
/// <returns>Root of Model Prefab.</returns>
71+
/// <param name="selected">Objects to export.</param>
72+
protected virtual GameObject ExportSelection(Object[] selected)
73+
{
74+
// export selected to a file, then return the root
75+
var filename = GetRandomFileNamePath();
76+
77+
Debug.unityLogger.logEnabled = false;
78+
var fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filename, selected) as string;
79+
Debug.unityLogger.logEnabled = true;
80+
81+
Assert.IsNotNull (fbxFileName);
82+
83+
// make filepath relative to project folder
84+
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
85+
{
86+
fbxFileName = "Assets" + fbxFileName.Substring (Application.dataPath.Length);
87+
}
88+
// refresh the assetdata base so that we can query for the model
89+
AssetDatabase.Refresh ();
90+
91+
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath (fbxFileName);
92+
var fbxRoot = unityMainAsset as GameObject;
93+
94+
Assert.IsNotNull (fbxRoot);
95+
return fbxRoot;
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)