-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from snyk/feat/memoization-in-graph-to-dep-tree
feat: add memoization when converting a graph into a tree
- Loading branch information
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as depGraphLib from '../../src'; | ||
import { graphToDepTree } from '../../src/legacy'; | ||
|
||
const dependencyName = 'needle'; | ||
|
||
async function generateLargeGraph(width: number) { | ||
const builder = new depGraphLib.DepGraphBuilder( | ||
{ name: 'npm' }, | ||
{ name: 'root', version: '1.2.3' }, | ||
); | ||
const rootNodeId = 'root-node'; | ||
|
||
const deepDependency = { name: dependencyName, version: '1.2.3' }; | ||
const deepDependency2 = { name: dependencyName + 2, version: '1.2.3' }; | ||
|
||
builder.addPkgNode(deepDependency, dependencyName); | ||
builder.addPkgNode(deepDependency2, deepDependency2.name); | ||
builder.connectDep(rootNodeId, dependencyName); | ||
builder.connectDep(deepDependency.name, deepDependency2.name); | ||
|
||
for (let j = 0; j < width / 2; j++) { | ||
const shallowName = `id-${j}`; | ||
const shallowDependency = { name: shallowName, version: '1.2.3' }; | ||
|
||
builder.addPkgNode(shallowDependency, shallowName); | ||
builder.connectDep(rootNodeId, shallowName); | ||
builder.connectDep(shallowName, dependencyName); | ||
} | ||
|
||
for (let j = 0; j < width / 2; j++) { | ||
const shallowName = `second-${j}`; | ||
const shallowDependency = { name: shallowName, version: '1.2.3' }; | ||
|
||
builder.addPkgNode(shallowDependency, shallowName); | ||
builder.connectDep(deepDependency2.name, shallowName); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
describe('stress tests', () => { | ||
test('graphToDepTree() with memoization (without deduplicateWithinTopLevelDeps) succeed for large dep-graphs', async () => { | ||
const graph = await generateLargeGraph(125000); | ||
|
||
const depTree = await graphToDepTree(graph, 'gomodules', { | ||
deduplicateWithinTopLevelDeps: false, | ||
}); | ||
expect(depTree).toBeDefined(); | ||
}); | ||
}); |