/******************************************************************************* | |
* Copyright (c) 2004-2011 Abel Hegedus, Istvan Rath and Daniel Varro | |
* This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v. 2.0 which is available at | |
* http://www.eclipse.org/legal/epl-v20.html. | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
*******************************************************************************/ | |
package org.eclipse.viatra.query.application.common; | |
import java.util.Collection; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.ecore.resource.Resource; | |
import org.eclipse.emf.ecore.resource.ResourceSet; | |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; | |
import org.eclipse.viatra.query.application.queries.EObjectMatch; | |
import org.eclipse.viatra.query.application.queries.EObjectMatcher; | |
import org.eclipse.viatra.query.runtime.api.IPatternMatch; | |
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | |
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngineManager; | |
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; | |
import org.eclipse.viatra.query.runtime.emf.EMFScope; | |
import org.eclipse.viatra.query.runtime.exception.ViatraQueryException; | |
import org.eclipse.viatra.query.runtime.extensibility.QuerySpecificationRegistry; | |
/** | |
* @author Abel Hegedus | |
* @author Istvan Rath | |
* | |
*/ | |
public class ViatraQueryHeadlessBenchmark { | |
private Resource loadModel(String modelPath) { | |
// Loads the resource | |
long start = System.nanoTime(); | |
ResourceSet resourceSet = new ResourceSetImpl(); | |
URI fileURI = URI.createFileURI(modelPath); | |
Resource resource = resourceSet.getResource(fileURI, true); | |
long resourceInit = System.nanoTime(); | |
System.out.println("EMF load took: " + (resourceInit - start) / 1000000 + " ms"); | |
return resource; | |
} | |
private void prettyPrintMatches(StringBuilder results, Collection<? extends IPatternMatch> matches) { | |
System.out.println("Found matches:"); | |
for (IPatternMatch match : matches) { | |
System.out.println(match.prettyPrint()); | |
results.append(match.prettyPrint()); | |
} | |
if(matches.size() == 0) { | |
results.append("Empty match set"); | |
} | |
} | |
private void measureMemory(StringBuilder results) { | |
System.gc(); | |
System.gc(); | |
System.gc(); | |
System.gc(); | |
System.gc(); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); | |
System.out.println("Used memory: " + usedMemory + " bytes"); | |
System.out.println("Used memory: " + (usedMemory / 1024) / 1024 + " megabytes"); | |
} | |
public String executePatternSpecificBenchmark(String modelPath) { | |
StringBuilder results = new StringBuilder(); | |
Resource resource = loadModel(modelPath); | |
if (resource != null) { | |
try { | |
// get all matches of the pattern | |
long startMatching = System.nanoTime(); | |
ViatraQueryEngine engine = ViatraQueryEngineManager.getInstance().getQueryEngine(new EMFScope(resource)); | |
EObjectMatcher matcher = EObjectMatcher.on(engine); | |
long matcherInit = System.nanoTime(); | |
Collection<EObjectMatch> matches = matcher.getAllMatches(); | |
long collectedMatches = System.nanoTime(); | |
System.out.println( | |
"Init took: " + (matcherInit - startMatching) / 1000000 + " Collecting took: " | |
+ (collectedMatches - matcherInit) / 1000000 + " ms"); | |
measureMemory(results); | |
prettyPrintMatches(results, matches); | |
} catch (ViatraQueryException e) { | |
e.printStackTrace(); | |
results.append(e.getMessage()); | |
} | |
} else { | |
results.append("Resource not found"); | |
} | |
return results.toString(); | |
} | |
/** | |
* Returns the match set for patternFQN over the model in modelPath in pretty printed form | |
* | |
* @param modelPath | |
* @param patternFQN | |
* @return | |
*/ | |
@SuppressWarnings({ "unchecked", "rawtypes" }) | |
public String executeGenericBenchmark(String modelPath, String patternFQN) { | |
StringBuilder results = new StringBuilder(); | |
Resource resource = loadModel(modelPath); | |
if (resource != null) { | |
try { | |
long startMatching = System.nanoTime(); | |
ViatraQueryEngine engine = ViatraQueryEngineManager.getInstance().getQueryEngine(new EMFScope(resource)); | |
ViatraQueryMatcher matcher = QuerySpecificationRegistry.getQuerySpecification(patternFQN).getMatcher(engine); | |
long matcherInit = System.nanoTime(); | |
// assuming that there is a pattern definition registered matching 'patternFQN' | |
if (matcher!=null) { | |
Collection<IPatternMatch> matches = matcher.getAllMatches(); | |
prettyPrintMatches(results, matches); | |
// get all matches of the pattern | |
long collectedMatches = System.nanoTime(); | |
System.out.println( | |
"Init took: " + (matcherInit - startMatching) / 1000000 + " Collecting took: " | |
+ (collectedMatches - matcherInit) / 1000000 + " ms"); | |
measureMemory(results); | |
prettyPrintMatches(results, matches); | |
} | |
} catch (ViatraQueryException e) { | |
e.printStackTrace(); | |
results.append(e.getMessage()); | |
} | |
} else { | |
results.append("Resource not found"); | |
} | |
return results.toString(); | |
} | |
} |