]> gerrit.simantics Code Review - simantics/platform.git/blob
76cd51e900923c88917cd61d2ac1dde235f72646
[simantics/platform.git] /
1 package org.simantics.scl.compiler.tests;
2
3 import org.junit.Assert;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
7 import org.simantics.scl.compiler.errors.CompilationErrorFormatter;
8 import org.simantics.scl.compiler.module.repository.ImportFailure;
9 import org.simantics.scl.compiler.module.repository.ImportFailureException;
10 import org.simantics.scl.compiler.module.repository.ModuleRepository;
11 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
12 import org.simantics.scl.compiler.top.ExpressionEvaluator;
13 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16
17 public class MemoryLeakExperiment {
18     ModuleRepository moduleRepository;
19
20     EnvironmentSpecification environmentSpecification;
21
22     @Before
23     public void initialize() throws Exception {
24         moduleRepository = InitialRepository.getInitialRepository();
25
26         // Environment for compiling expressions
27         environmentSpecification = new EnvironmentSpecification();
28         environmentSpecification.importModule("Builtin", "");
29         environmentSpecification.importModule("Prelude", "");
30     }
31
32     private void testExpression0(String expressionText,
33             Object expectedValue,
34             Type expectedType) throws Exception {
35         
36         RuntimeEnvironment runtimeEnvironment;
37         try {
38             runtimeEnvironment = moduleRepository.createRuntimeEnvironment(environmentSpecification,
39                     getClass().getClassLoader());
40         } catch(ImportFailureException e) {
41             for(ImportFailure failure : e.failures)
42                 System.err.println("Failed to import " + failure.moduleName);
43             throw e;
44         }
45
46         // Compiling and running expression
47         try {
48             Object result = new ExpressionEvaluator(runtimeEnvironment, expressionText)
49                     .expectedType(expectedType)
50                     .interpretIfPossible(false)
51                     .eval();
52             if(expectedValue != null)
53                 Assert.assertEquals(expectedValue, result);
54         } catch(SCLExpressionCompilationException e) {
55             System.out.println(CompilationErrorFormatter.toString(expressionText, e.getErrors()));
56             throw e;
57         }
58     }
59
60     @Test
61     public void testIt() throws Exception {
62         for(int i=0;i<1000000;++i) {
63             testExpression0("fst (\"a\", \"b\")", "a", Types.STRING);
64             if(i % 10000 == 0 && i > 0) {
65                 System.gc();
66                 System.runFinalization();
67                 System.gc();
68                 Thread.sleep(100L);
69                 double used = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
70                 System.out.println(i + " " + used*1e-6 + " Mb, " + (used / i) + " b / expression");
71             }
72         }
73     }
74 }