Commit 65034b67 authored by Leonard Faecke's avatar Leonard Faecke
Browse files

+ Saving total data collected in data/total.json on run

+ Added folder data to gitignore
+ Changed analyzed resources from t3q-resources to iwd-TTCN3-B2013-03_D14wk10
parent 1683e812
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
/bin/
data
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@
    <booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
    <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="de.ugoe.cs.swe.T3Q.T3Q"/>
    <stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="de.ugoe.cs.swe.T3Q"/>
    <stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--config config/t3q.cfg --profile defaultProfile C:\Users\leofa\Documents\_t3q\t3q-resources --analyze-usage"/>
    <stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--config config/t3q.cfg --profile defaultProfile C:\Users\leofa\Documents\_t3q\iwd-TTCN3-B2013-03_D14wk10 --analyze-usage"/>
    <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="de.ugoe.cs.swe.T3Q"/>
    <stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xms256m -Xmx4g"/>
    <stringAttribute key="yk-options" value="&#13;&#10;additional-options2=onexit\=snapshot&#13;&#10;"/>
+66 −4
Original line number Diff line number Diff line
package de.ugoe.cs.swe.T3Q;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
@@ -10,6 +12,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -33,6 +36,8 @@ import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.validation.FeatureBasedDiagnostic;

import com.google.common.base.Stopwatch;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.Inject;
import com.google.inject.Injector;

@@ -59,6 +64,9 @@ public class TTCN3ResourceProvider {
	
	private final Stopwatch stopwatch = Stopwatch.createUnstarted();

	private String dataDirName = "data";
	private int dataLimit = 16384;
	
	public TTCN3ResourceProvider(final ArrayList<String> paths,
			LoggingInterface logger, QualityCheckProfile activeProfile) {
		super();
@@ -261,12 +269,20 @@ public class TTCN3ResourceProvider {
			List<Future<TTCN3Usage>> output = pool.invokeAll(analyzer);
			pool.shutdown();
			System.out.print('\n');
			
			List<Map<String, Long>> maps = new ArrayList<>();
			for (Future<TTCN3Usage> f : output) {
				TTCN3Usage o = f.get();
				System.out.println("Usage analysis output of " + o.getResource().getURI().lastSegment());
				printOutput(o.getResource(), o.getOutput());
				//System.out.println("Usage analysis output of " + o.getResource().getURI().lastSegment());
				//printOutput(o.getResource(), o.getOutput());
				maps.add(o.getOutput());
			}
			
			Map<String, Long> unified = unifyMaps(maps);
			
			System.out.println("Total usage analysis output:");
			printOutput(unified);
			saveJson(unified, "total.json");

		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
@@ -282,7 +298,11 @@ public class TTCN3ResourceProvider {
		System.out.println(message);
	}
	
	private void printOutput(Resource resource, Map<String,Integer> usage) {
	private void printOutput(Resource resource, Map<String,Long> usage) {
		usage.forEach((k,v) -> System.out.println(k+","+v));
	}
	
	private void printOutput(Map<String,Long> usage) {
		usage.forEach((k,v) -> System.out.println(k+","+v));
	}
	
@@ -433,4 +453,46 @@ public class TTCN3ResourceProvider {
	public void setCores(int cores) {
		this.cores = cores;
	}
	
	private Map<String, Long> unifyMaps(List<Map<String, Long>> mapList) {
		Map<String, Long> unified = new HashMap<>();
		for(Map<String, Long> map : mapList) {
			for(String key : map.keySet()) {
				Long val = map.get(key);
				
				if(unified.containsKey(key)) val += unified.get(key);
				unified.put(key, val);
			}
		}
		return unified;
	}
	
	private void saveJson(Map<String, Long> json, String name) {
		try {
			final File dir = new File(dataDirName);
			if(name == null || name.equals("")) { name = UUID.randomUUID().toString() + "_data.json"; }
			if(name.length() < 6 || !name.substring(name.length()-5, name.length()).equals(".json")) {
				name += ".json";
			}
			Gson gson = new GsonBuilder().setPrettyPrinting().create();
			if(!dir.exists()) {
				if(!dir.mkdir()) {
					System.out.println("Could not find or create target directory");
					return;
				}
			}
			String jsonString = gson.toJson(json);
			System.out.println("Serialized data size: " + jsonString.length());
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(dir, name)), dataLimit);
			writer.write(jsonString);
			writer.flush();
			//gson.toJson(json, new FileWriter(new File(dir, name))); //FileWriter has a limit at 8192 characters
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setDataDirName(String name) {
		dataDirName = name;
	}
}
+2 −2
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ import com.google.common.collect.Maps;

public class TTCN3Usage {
	private final Resource resource;
	private final Map<String,Integer> output;
	private final Map<String,Long> output;
	
	public TTCN3Usage(final Resource resource) {
		this.resource = resource;
@@ -19,7 +19,7 @@ public class TTCN3Usage {
		return resource;
	}

	public Map<String,Integer> getOutput() {
	public Map<String,Long> getOutput() {
		return output;
	}

+72 −14
Original line number Diff line number Diff line
@@ -3,11 +3,19 @@ package de.ugoe.cs.swe.T3Q;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.ENamedElement;
@@ -20,6 +28,8 @@ import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import com.google.gson.stream.JsonReader;

import de.ugoe.cs.swe.common.MiscTools;
import de.ugoe.cs.swe.common.logging.LoggingInterface;
@@ -49,11 +59,13 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
		EObject model = this.resource.getContents().get(0);
		
		//adjust method name, parameters, etc. as needed
		genericAnalysisExample(model);
		//genericAnalysisSingle(model);
		
		//collect basic stats (keep in mind multi-threaded processing, need to collect output in a file or in output), 
		//for initial basic testing single-threaded mode should provide first results
		//System.out.println("Module, FunctionDef, AltConstruct, RecordDefNamed, ComponentDef, ConstDef");
		
		List<Map<String, Long>> allJsons = new ArrayList<>();
		for (TTCN3Module module : ((TTCN3File)model).getModules()) {
			//immediate output (will look weird if single-core is not activated)
			
@@ -74,11 +86,17 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
			
			
			//countDefinitions(module, output);
			analyzeModule(module, output);
			
			//analyzeModule(module, output);
			
			//make gson of every model
			Map<String, Long> json= genericAnalysisSingle(module.eContainer());
			allJsons.add(json);
		}
		
		Map<String, Long> unified = unifyJsons(allJsons);

		unified.forEach((k, v) -> addNToOutput(output, k, v));

		stopwatch.stop();

		System.out.println("Analyzing usage in file: " + this.resource.getURI().devicePath().replaceFirst("///", "") + '\n'
@@ -88,7 +106,47 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
		return output;
	}
	
	private void genericAnalysisExample(EObject model) {
	private void saveJson(Map<String, Long> json) {
		saveJson(json, null);
	}
	
	private void saveJson(Map<String, Long> json, String name) {
		String dataDirName = "data";
		try {
			final File dir = new File(dataDirName);
			if(name == null || name.equals("")) { name = UUID.randomUUID().toString() + "_data.json"; }
			if(name.length() < 6 || !name.substring(name.length()-5, name.length()).equals(".json")) {
				name += ".json";
			}
			Gson gson = new GsonBuilder().setPrettyPrinting().create();
			
			if(!dir.exists()) {
				if(dir.mkdir()) {
					gson.toJson(json, new FileWriter(new File(dir, name)));
				}
			} else {
				gson.toJson(json, new FileWriter(new File(dir, name)));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private Map<String, Long> unifyJsons(List<Map<String, Long>> jsonList) {
		Map<String, Long> unified = new HashMap<>();
		for(Map<String, Long> json : jsonList) {
			for(String key : json.keySet()) {
				Long val = json.get(key);
				
				if(unified.containsKey(key)) val += unified.get(key);
				if(val > Long.MAX_VALUE) System.out.println("Value too large for integer");
				unified.put(key, val);
			}
		}
		return unified;
	}

	private Map<String, Long> genericAnalysisSingle(EObject model) {
		//--------------------------------------------------------
		//TODO: per file, total

@@ -119,9 +177,9 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
		//create basic json
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String json = gson.toJson(counts);
		System.out.println(json);
		//System.out.println(json);
		//TODO: store as file or return and integrate
		
		return counts;
		//--------------------------------------------------------
	}
	
@@ -134,7 +192,7 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
	private void countDefinitions(TTCN3Module module, TTCN3Usage output) {
		System.out.println("Analyzing module definitions");
		List<ModuleDefinition> definitions = EcoreUtil2.getAllContentsOfType(module, ModuleDefinition.class);
		output.getOutput().put("TotalDef", definitions.size());
		output.getOutput().put("TotalDef", (long) definitions.size());
		
		Set<Class<?>> toAnalyze = new HashSet<Class<?>>();
		for (ModuleDefinition d : definitions) {
@@ -266,7 +324,7 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
	 * @param identifier The identifier to add to
	 */
	private void addToOutput(TTCN3Usage output, String identifier) {
		addNToOutput(output, identifier, 1);
		addNToOutput(output, identifier, (long) 1);
	}
	
	
@@ -274,13 +332,13 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
	 * Add n to the given identifier in the output
	 * @param output The TTCN3Usage to add to.
	 * @param identifier The identifier to add to
	 * @param n The number to add to the output to. Cannot be negative.
	 * @param v The number to add to the output to. Cannot be negative.
	 */
	private void addNToOutput(TTCN3Usage output, String identifier, int n) {
	private void addNToOutput(TTCN3Usage output, String identifier, Long v) {
		if(output.getOutput().containsKey(identifier)) {
			output.getOutput().replace(identifier, output.getOutput().get(identifier) + n);
			output.getOutput().replace(identifier, output.getOutput().get(identifier) + v);
		} else {
			output.getOutput().put(identifier, n);
			output.getOutput().put(identifier, v);
		}
	}
	
@@ -582,7 +640,7 @@ public class UsageAnalyzer implements Callable<TTCN3Usage> {
		String id = "ExtConst:";
		if(con.getType().getPre() == null) id += "Non-Primitive";
		else id += con.getType().getPre();
		addNToOutput(output, id, con.getId().getIds().size());
		addNToOutput(output, id, (long) con.getId().getIds().size());
	}
	
	/**
@@ -652,7 +710,7 @@ System.out.println("Analyzing function");
			}
			n = mod.getParam().getList().getParams().size();
		}
		addNToOutput(output, id, n);
		addNToOutput(output, id, (long) n);
	}
	
	@Override