Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creative timer #819

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions performance-test/template_data/ChemToGeneDecrease.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": "{ID}"
},
"n1": {
"categories": ["biolink:Gene"]
}
},
"edges": {
"e0": {
"subject": "n0",
"object": "n1",
"predicates": ["biolink:affects"],
"knowledge_type": "inferred",
"qualifier_constraints": [
{
"qualifier_set": [
{
"qualifier_type_id": "biolink:object_direction_qualifier",
"qualifier_value": "decreased"
},
{
"qualifier_type_id": "biolink:object_aspect_qualifier",
"qualifier_value": "activity_or_abundance"
}
]
}
]
}
}
}
},
"ids": ["PUBCHEM.COMPOUND:3007", "PUBCHEM.COMPOUND:5826"]
}
38 changes: 38 additions & 0 deletions performance-test/template_data/ChemToGeneIncrease.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": "{ID}"
},
"n1": {
"categories": ["biolink:Gene"]
}
},
"edges": {
"e0": {
"subject": "n0",
"object": "n1",
"predicates": ["biolink:affects"],
"knowledge_type": "inferred",
"qualifier_constraints": [
{
"qualifier_set": [
{
"qualifier_type_id": "biolink:object_direction_qualifier",
"qualifier_value": "increased"
},
{
"qualifier_type_id": "biolink:object_aspect_qualifier",
"qualifier_value": "activity_or_abundance"
}

]
}
]
}
}
}
},
"ids": ["PUBCHEM.COMPOUND:3007", "PUBCHEM.COMPOUND:5826"]
}
38 changes: 38 additions & 0 deletions performance-test/template_data/GeneDecreaseToChem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": "{ID}"
},
"n1": {
"categories": ["biolink:SmallMolecule"]
}
},
"edges": {
"e0": {
"subject": "n1",
"object": "n0",
"predicates": ["biolink:affects"],
"knowledge_type": "inferred",
"qualifier_constraints": [
{
"qualifier_set": [
{
"qualifier_type_id": "biolink:object_direction_qualifier",
"qualifier_value": "decreased"
},
{
"qualifier_type_id": "biolink:object_aspect_qualifier",
"qualifier_value": "activity_or_abundance"
}

]
}
]
}
}
}
},
"ids": ["NCBIGene:23162", "NCBIGene:7157"]
}
37 changes: 37 additions & 0 deletions performance-test/template_data/GeneIncreaseToChem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": "{ID}"
},
"n1": {
"categories": ["biolink:SmallMolecule"]
}
},
"edges": {
"e0": {
"subject": "n1",
"object": "n0",
"predicates": ["biolink:affects"],
"knowledge_type": "inferred",
"qualifier_constraints": [
{
"qualifier_set": [
{
"qualifier_type_id": "biolink:object_direction_qualifier",
"qualifier_value": "increased"
},
{
"qualifier_type_id": "biolink:object_aspect_qualifier",
"qualifier_value": "activity_or_abundance"
}
]
}
]
}
}
}
},
"ids": ["NCBIGene:23162", "NCBIGene:7157"]
}
23 changes: 23 additions & 0 deletions performance-test/template_data/drugTreatsDisease.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": "{ID}"
},
"n1": {
"categories": ["biolink:Drug"]
}
},
"edges": {
"e0": {
"subject": "n1",
"object": "n0",
"predicates": ["biolink:treats"],
"knowledge_type": "inferred"
}
}
}
},
"ids": ["MONDO:0002909", "MONDO:0019499"]
}
87 changes: 87 additions & 0 deletions performance-test/template_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const fetch = require('node-fetch');
const fs = require('fs/promises');
const path = require('path');
const InferredQueryHandler = require('../packages/query_graph_handler/built/inferred_mode/inferred_mode').default;
const TRAPIQueryHandler = require('../packages/query_graph_handler/built/index').default;

const PORT = Number.parseInt(process.env.PORT) || 3000;
const url = `http://localhost:${PORT}/v1/query`;

async function main() {
/** @type { {[template: string]: {count: number, totalMs: number, avgMs: number?}} } */
const templateTimes = {};

// get list of all files in template_data
const templateDataPath = path.resolve(__dirname, './template_data');
const files = await fs.readdir(templateDataPath);

for (const file of files) {
console.log(file)
const filePath = path.resolve(templateDataPath, file);
const data = JSON.parse(await fs.readFile(filePath, 'utf-8'));
if (data.ids) {
for (const id of data.ids) {
console.log(`file-${id}`)
const queryGraph = JSON.parse(JSON.stringify(data.message.query_graph));
for (const node of Object.values(queryGraph.nodes)) {
if (node.ids === "{ID}") {
node.ids = Array.isArray(id) ? id : [id];
}
}
await handleQueryGraph(queryGraph, templateTimes);
}
} else {
await handleQueryGraph(data.message.query_graph, templateTimes);
}
}

Object.values(templateTimes).map(a => a.avgMin = parseFloat((a.totalMs / a.count / 60 / 1000).toFixed(2)));
console.log(templateTimes)
}

async function handleQueryGraph(queryGraph, templateTimes) {
// create a new InferredQueryHandler instance
const parentHandler = new TRAPIQueryHandler();
parentHandler.setQueryGraph(queryGraph);
parentHandler._initializeResponse();
await parentHandler.addQueryNodes();
await parentHandler._processQueryGraph(parentHandler.queryGraph);
const handler = new InferredQueryHandler(parentHandler, parentHandler.queryGraph, [], {}, parentHandler.path, parentHandler.predicatePath, true);

// get templates
const { qEdge, qSubject, qObject } = handler.getQueryParts();
const subQueries = await handler.createQueries(qEdge, qSubject, qObject);

// go through each template
for (const { template, queryGraph: templateQueryGraph } of subQueries) {
if (!templateTimes[template]) {
templateTimes[template] = { count: 0, totalMs: 0 };
}
const start = Date.now();
try {
const resp = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({message: { query_graph: templateQueryGraph }})
});
if (resp.status < 300) {
const end = Date.now();
templateTimes[template].count++;
templateTimes[template].totalMs += end - start;
} else if (resp.status == 500) {
const is_timeout = (await resp.json())?.description?.includes?.("time");
if (is_timeout) {
templateTimes[template].count++;
templateTimes[template].totalMs += 10 * 60 * 1000; // timeout = 10 minutes
}
}
} catch (e) {
console.log(`Error while requesting: ${e} ${e.stack}`)
}
}
}

main();
Loading
Loading