Skip to content

Commit

Permalink
pure: show execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi committed Nov 7, 2024
1 parent cd514ea commit cc8ddee
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jline.reader.ParsedLine;

import static org.finos.legend.engine.repl.shared.ExecutionHelper.executeCode;
import static org.finos.legend.engine.repl.shared.ExecutionHelper.printExecutionTime;
import static org.finos.legend.engine.repl.shared.REPLHelper.ansiGreen;

public class Execute implements Command
Expand All @@ -51,7 +52,9 @@ public String documentation()
@Override
public boolean process(String line) throws Exception
{
long startTime = System.currentTimeMillis();
this.client.println(execute(line));
this.client.println(printExecutionTime(startTime));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public static ExecutionHelper.ExecuteResultSummary executeCode(String txt, Clien
return resultHandler.value(res, pmcd, pureModel, execPlan);
}

public static String printExecutionTime(long startTime)
{
return String.format("%.1f", (float) (System.currentTimeMillis() - startTime) / 1000) + "s";
}

public static class ExecuteResultSummary
{
public final PureModelContextData pureModelContextData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;

import static org.finos.legend.engine.plan.execution.stores.relational.result.RelationalResultGridPrintUtility.prettyGridPrint;
import static org.finos.legend.engine.repl.shared.ExecutionHelper.printExecutionTime;

public class DataCube__DEV__runDuckDBSelectSQL implements Command
{
Expand Down Expand Up @@ -79,6 +80,7 @@ public boolean process(String line) throws Exception
{
try (Statement statement = connection.createStatement())
{
long startTime = System.currentTimeMillis();
ResultSet result = statement.executeQuery(expression);
List<String> columnNames = Lists.mutable.empty();
List<String> columnTypes = Lists.mutable.empty();
Expand All @@ -89,6 +91,7 @@ public boolean process(String line) throws Exception
}
this.client.println("Executed SELECT SQL: '" + expression + "'");
this.client.println(prettyGridPrint(result, columnNames, columnNames, 40, 60));
this.client.println(printExecutionTime(startTime));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void initialize(PureModelContextData pureModelContextData, List<DataCube
String newRuntime = ((PackageableElementPtr) fn.parameters.get(1)).fullPath;
if (runtime != null && !runtime.equals(newRuntime))
{
throw new RuntimeException("Can't initialize DataCube. Source query contains multiple different ->from(), only one is expected");
throw new RuntimeException("Can't launch DataCube. Source query contains multiple different ->from(), only one is expected");
}
runtime = newRuntime;
}
Expand All @@ -130,7 +130,7 @@ else if (fn.parameters.size() == 3)
String newRuntime = ((PackageableElementPtr) fn.parameters.get(2)).fullPath;
if ((mapping != null && !mapping.equals(newMapping)) || (runtime != null && !runtime.equals(newRuntime)))
{
throw new RuntimeException("Can't initialize DataCube. Source query contains multiple different ->from(), only one is expected");
throw new RuntimeException("Can't launch DataCube. Source query contains multiple different ->from(), only one is expected");
}
mapping = newMapping;
runtime = newRuntime;
Expand Down Expand Up @@ -183,7 +183,7 @@ public void initializeFromTable(PureModelContextData pureModelContextData)
}
catch (Exception e)
{
throw new RuntimeException("Can't initialize DataCube: expected to get a relation type");
throw new RuntimeException("Can't launch DataCube: expected to get a relation type");
}
this.initialize(pureModelContextData, ListIterate.collect(RelationTypeHelper.convert(relationType).columns, col -> new DataCubeQueryColumn(col.name, col.type)));
}
Expand All @@ -192,7 +192,7 @@ public void initializeWithREPLExecutedQuery(ExecutionHelper.ExecuteResultSummary
{
if (!(executeResultSummary.result instanceof RelationalResult) || !(((RelationalResult) executeResultSummary.result).builder instanceof TDSBuilder))
{
throw new RuntimeException("Can't initialize DataCube: last executed query did not produce a TDS (i.e. data-grid), try a different query...");
throw new RuntimeException("Can't launch DataCube: last executed query did not produce a TDS (i.e. data-grid), try a different query...");
}

RelationType relationType;
Expand All @@ -202,7 +202,7 @@ public void initializeWithREPLExecutedQuery(ExecutionHelper.ExecuteResultSummary
}
catch (Exception e)
{
throw new RuntimeException("Can't initialize DataCube: last executed query must return a relation type, try a different query...");
throw new RuntimeException("Can't launch DataCube: last executed query must return a relation type, try a different query...");
}

boolean isDynamic = false;
Expand All @@ -220,7 +220,7 @@ public void initializeWithREPLExecutedQuery(ExecutionHelper.ExecuteResultSummary
}
if (isDynamic)
{
throw new RuntimeException("Can't initialize DataCube: last executed query produced dynamic result, try casting the result with cast(@" + M3Paths.Relation + "<(...)>) syntax or use 'cache' command to dump the data out to a table and query against that table instead...");
throw new RuntimeException("Can't launch DataCube: last executed query produced dynamic result, try casting the result with cast(@" + M3Paths.Relation + "<(...)>) syntax or use 'cache' command to dump the data out to a table and query against that table instead...");
}

RelationalResult result = (RelationalResult) executeResultSummary.result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import static org.finos.legend.engine.repl.relational.schema.MetadataReader.getTables;
import static org.finos.legend.engine.repl.shared.ExecutionHelper.executeCode;
import static org.finos.legend.engine.repl.shared.ExecutionHelper.printExecutionTime;

public class Cache implements Command
{
Expand Down Expand Up @@ -91,6 +92,7 @@ public boolean process(String line) throws Exception

try
{
long startTime = System.currentTimeMillis();
executeCode(expression, this.client, (Result res, PureModelContextData pmcd, PureModel pureModel, SingleExecutionPlan plan) ->
{
if (res instanceof RelationalResult)
Expand All @@ -117,6 +119,7 @@ public boolean process(String line) throws Exception
{
statement.executeUpdate(DatabaseManager.fromString(databaseConnection.type.name()).relationalDatabaseSupport().load(tableName, tempFile.getTemporaryPathForFile(), relationalResultColumns));
this.client.println("Cached into table: '" + tableName + "'");
this.client.println(printExecutionTime(startTime));
}
}
catch (SQLException e)
Expand Down

0 comments on commit cc8ddee

Please sign in to comment.