PageRenderTime 32ms CodeModel.GetById 19ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Python | 147 lines | 106 code | 24 blank | 17 comment | 10 complexity | afba81c1d4227a600d73093ff58101ee MD5 | raw file
  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
 16from iptest.assert_util import *
 17skiptest("win32")
 18skiptest("silverlight")  #no time.clock or GetTotalMemory
 19
 20import clr
 21clr.AddReference("Microsoft.Dynamic")
 22from Microsoft.Scripting.Generation import Snippets
 23import gc
 24skipMemoryCheck = Snippets.Shared.SaveSnippets or clr.GetCurrentRuntime().Configuration.DebugMode
 25
 26from time import clock
 27
 28# GetTotalMemory() actually pulls in System
 29def evalLoop(N):
 30    for i in range(N):
 31        func = compile(code, '<>', 'exec')
 32        eval(func)
 33    
 34def evalTest(N):
 35    startMem = GetTotalMemory()
 36    startTime = clock()
 37    evalLoop(N)
 38    endTime = clock()
 39    gc.collect(2)
 40    endMem = GetTotalMemory()
 41    return max(endMem-startMem, 0)
 42    
 43t_list = [
 44        "if not 1 + 2 == 3: raise AssertionError('Assertion Failed')",
 45        "(a,b) = (0, 1)",
 46        "2+"*10 + "2",
 47    
 48        "import sys",
 49        
 50        "from time import clock",
 51    
 52        "eval('2+2')",
 53        "globals(), locals()",
 54    
 55        "try:\n    x = 10\nexcept:\n    pass",
 56    
 57        "def f(): pass",
 58        "def f(a): pass",
 59        "def f(a, b, c, d, e, f, g, h, i, j): pass",
 60    
 61        "def f(*args): pass",
 62        "def f(a, *args): pass",
 63        "def f(func, *args): func(args)",
 64        "def f(**args): pass",
 65    
 66        "def f(a, b=2, c=[2,3]): pass",
 67    
 68        "def f(x):\n    for i in range(x):\n        yield i",
 69        "def f(x):\n    print locals()",
 70        "def f(x):\n    print globals()",
 71    
 72        "lambda x: x + 2",
 73    
 74        "(lambda x: x + 2)(0)",
 75        "(lambda x, y, z, u, v, w: x + 2)(0, 0, 0, 0, 0, 0)",
 76    
 77        "class C:\n    pass",
 78    
 79        "class C:\n    class D:pass\n    pass",
 80        "def f(x):\n    def g(y):pass\n    pass",
 81        "def f(x):\n    def g(*y):pass\n    pass",
 82    
 83        "class C:\n    def f(self):\n        pass",
 84        "def f():\n    class C: pass\n    pass",
 85        "def f():pass\nclass C:pass\nf()",
 86    ]
 87
 88# account for adaptive compilation
 89expectedMem = 24000
 90
 91if is_cli64:
 92    expectedMem = int(expectedMem*1.25)
 93
 94for code in t_list:    
 95    baseMem = evalTest(10)
 96    
 97    usedMax = max(expectedMem, 4*baseMem)
 98    if not skipMemoryCheck:
 99        for repetitions in [100, 500]:
100            usedMem = evalTest(repetitions)
101            Assert(usedMem < usedMax, "Allocated %i (max %i, base %i) running %s %d times" % (usedMem, usedMax, baseMem, code, repetitions))
102    else:
103        # not to measure the memory usage, but still try to peverify the code at the end
104        evalTest(2)
105    
106e = compile("def f(): return 42\n", "", "single")
107names = {}
108eval(e, names)
109AreEqual(names['f'](), 42)
110    
111code = """
112x=2
113def f(y):
114    return x+y
115z = f(3)
116"""
117e = compile(code, "", "exec")
118names = {}
119eval(e, names)
120AreEqual(names['z'], 5)
121
122
123def test_cp26005():
124    def coroutine():
125        try: pass
126        except: pass
127        just_numbers = range(1,1000)
128        def inner_method():
129                    return just_numbers
130        yield None
131        yield None
132     
133    from System import GC
134    def get_memory():
135        for _ in xrange(4):
136                    GC.Collect()
137                    GC.WaitForPendingFinalizers()
138        return GC.GetTotalMemory(True)/1e6
139    before = get_memory()
140    for j in xrange(10000):
141        crt = coroutine()
142        crt.next()
143    after = get_memory()
144    Assert(after-before < 10)
145
146
147test_cp26005()