PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Tests/test_memory.py

#
Python | 147 lines | 106 code | 24 blank | 17 comment | 7 complexity | afba81c1d4227a600d73093ff58101ee MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. from iptest.assert_util import *
  16. skiptest("win32")
  17. skiptest("silverlight") #no time.clock or GetTotalMemory
  18. import clr
  19. clr.AddReference("Microsoft.Dynamic")
  20. from Microsoft.Scripting.Generation import Snippets
  21. import gc
  22. skipMemoryCheck = Snippets.Shared.SaveSnippets or clr.GetCurrentRuntime().Configuration.DebugMode
  23. from time import clock
  24. # GetTotalMemory() actually pulls in System
  25. def evalLoop(N):
  26. for i in range(N):
  27. func = compile(code, '<>', 'exec')
  28. eval(func)
  29. def evalTest(N):
  30. startMem = GetTotalMemory()
  31. startTime = clock()
  32. evalLoop(N)
  33. endTime = clock()
  34. gc.collect(2)
  35. endMem = GetTotalMemory()
  36. return max(endMem-startMem, 0)
  37. t_list = [
  38. "if not 1 + 2 == 3: raise AssertionError('Assertion Failed')",
  39. "(a,b) = (0, 1)",
  40. "2+"*10 + "2",
  41. "import sys",
  42. "from time import clock",
  43. "eval('2+2')",
  44. "globals(), locals()",
  45. "try:\n x = 10\nexcept:\n pass",
  46. "def f(): pass",
  47. "def f(a): pass",
  48. "def f(a, b, c, d, e, f, g, h, i, j): pass",
  49. "def f(*args): pass",
  50. "def f(a, *args): pass",
  51. "def f(func, *args): func(args)",
  52. "def f(**args): pass",
  53. "def f(a, b=2, c=[2,3]): pass",
  54. "def f(x):\n for i in range(x):\n yield i",
  55. "def f(x):\n print locals()",
  56. "def f(x):\n print globals()",
  57. "lambda x: x + 2",
  58. "(lambda x: x + 2)(0)",
  59. "(lambda x, y, z, u, v, w: x + 2)(0, 0, 0, 0, 0, 0)",
  60. "class C:\n pass",
  61. "class C:\n class D:pass\n pass",
  62. "def f(x):\n def g(y):pass\n pass",
  63. "def f(x):\n def g(*y):pass\n pass",
  64. "class C:\n def f(self):\n pass",
  65. "def f():\n class C: pass\n pass",
  66. "def f():pass\nclass C:pass\nf()",
  67. ]
  68. # account for adaptive compilation
  69. expectedMem = 24000
  70. if is_cli64:
  71. expectedMem = int(expectedMem*1.25)
  72. for code in t_list:
  73. baseMem = evalTest(10)
  74. usedMax = max(expectedMem, 4*baseMem)
  75. if not skipMemoryCheck:
  76. for repetitions in [100, 500]:
  77. usedMem = evalTest(repetitions)
  78. Assert(usedMem < usedMax, "Allocated %i (max %i, base %i) running %s %d times" % (usedMem, usedMax, baseMem, code, repetitions))
  79. else:
  80. # not to measure the memory usage, but still try to peverify the code at the end
  81. evalTest(2)
  82. e = compile("def f(): return 42\n", "", "single")
  83. names = {}
  84. eval(e, names)
  85. AreEqual(names['f'](), 42)
  86. code = """
  87. x=2
  88. def f(y):
  89. return x+y
  90. z = f(3)
  91. """
  92. e = compile(code, "", "exec")
  93. names = {}
  94. eval(e, names)
  95. AreEqual(names['z'], 5)
  96. def test_cp26005():
  97. def coroutine():
  98. try: pass
  99. except: pass
  100. just_numbers = range(1,1000)
  101. def inner_method():
  102. return just_numbers
  103. yield None
  104. yield None
  105. from System import GC
  106. def get_memory():
  107. for _ in xrange(4):
  108. GC.Collect()
  109. GC.WaitForPendingFinalizers()
  110. return GC.GetTotalMemory(True)/1e6
  111. before = get_memory()
  112. for j in xrange(10000):
  113. crt = coroutine()
  114. crt.next()
  115. after = get_memory()
  116. Assert(after-before < 10)
  117. test_cp26005()