forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cs
More file actions
75 lines (64 loc) · 2.69 KB
/
Engine.cs
File metadata and controls
75 lines (64 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using NUnit.Framework;
namespace IronPythonTest.Stress {
[TestFixture(Category="IronPython")]
public class Engine
#if FEATURE_REMOTING
: MarshalByRefObject
#endif
{
private readonly ScriptEngine _pe;
private readonly ScriptRuntime _env;
public Engine() {
// Load a script with all the utility functions that are required
// pe.ExecuteFile(InputTestDirectory + "\\EngineTests.py");
_env = Python.CreateRuntime();
_pe = _env.GetEngine("py");
}
private static long GetTotalMemory() {
// Critical objects can take upto 3 GCs to be collected
System.Threading.Thread.Sleep(1000);
for (int i = 0; i < 3; i++) {
GC.Collect();
GC.WaitForPendingFinalizers();
}
return GC.GetTotalMemory(true);
}
#if FEATURE_REFEMIT
[Test]
public void ScenarioXGC() {
long initialMemory = GetTotalMemory();
// Create multiple scopes:
for (int i = 0; i < 10000; i++) {
ScriptScope scope = _pe.CreateScope();
scope.SetVariable("x", "Hello");
_pe.CreateScriptSourceFromFile(System.IO.Path.Combine(Common.InputTestDirectory, "simpleCommand.py")).Execute(scope);
Assert.That(1, Is.EqualTo(_pe.CreateScriptSourceFromString("x").Execute<int>(scope)));
scope = null;
}
// free up weak data structures held onto by the Python runtime
_pe.Execute("import gc\ngc.collect()");
long finalMemory = GetTotalMemory();
long memoryUsed = finalMemory - initialMemory;
const long memoryThreshold = 100000;
bool emitsUncollectibleCode = Snippets.Shared.SaveSnippets || _env.Setup.DebugMode;
if (!emitsUncollectibleCode)
{
System.Console.WriteLine("ScenarioGC used {0} bytes of memory.", memoryUsed);
if (memoryUsed > memoryThreshold)
throw new Exception(String.Format("ScenarioGC used {0} bytes of memory. The threshold is {1} bytes", memoryUsed, memoryThreshold));
}
else {
System.Console.WriteLine("Skipping memory usage test under SaveSnippets and/or Debug mode.");
}
}
#endif
}
}