1 package org.simantics.scl.compiler.tests;
3 import org.junit.Assert;
4 import org.junit.Before;
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;
17 public class MemoryLeakExperiment {
18 ModuleRepository moduleRepository;
20 EnvironmentSpecification environmentSpecification;
23 public void initialize() throws Exception {
24 moduleRepository = InitialRepository.getInitialRepository();
26 // Environment for compiling expressions
27 environmentSpecification = new EnvironmentSpecification();
28 environmentSpecification.importModule("Builtin", "");
29 environmentSpecification.importModule("Prelude", "");
32 private void testExpression0(String expressionText,
34 Type expectedType) throws Exception {
36 RuntimeEnvironment runtimeEnvironment;
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);
46 // Compiling and running expression
48 Object result = new ExpressionEvaluator(runtimeEnvironment, expressionText)
49 .expectedType(expectedType)
50 .interpretIfPossible(false)
52 if(expectedValue != null)
53 Assert.assertEquals(expectedValue, result);
54 } catch(SCLExpressionCompilationException e) {
55 System.out.println(CompilationErrorFormatter.toString(expressionText, e.getErrors()));
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) {
66 System.runFinalization();
69 double used = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
70 System.out.println(i + " " + used*1e-6 + " Mb, " + (used / i) + " b / expression");