-
Notifications
You must be signed in to change notification settings - Fork 14
feat: added benchmarking script #2535
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
base: staging/pentagon-ai
Are you sure you want to change the base?
Changes from all commits
9de08ae
80f7255
6733f76
5edf6cd
8f07e16
32caeb0
4dd6352
e4fd7de
65c2f0e
c088645
f865204
141f0f2
217f362
125a4ec
7025ab3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| import fs from 'node:fs'; | ||
| import type { NodeId } from '../r-bridge/lang-4.x/ast/model/processing/node-id'; | ||
| import { FlowrAnalyzerBuilder } from '../project/flowr-analyzer-builder'; | ||
| import { fileProtocol } from '../r-bridge/retriever'; | ||
| import { BuiltInProcName } from '../dataflow/environments/built-in-proc-name'; | ||
| import { VertexType } from '../dataflow/graph/vertex'; | ||
| import { RType } from '../r-bridge/lang-4.x/ast/model/type'; | ||
| import { IntervalExpressionSemanticsMapper } from './interval/expression-semantics'; | ||
| import { Identifier } from '../dataflow/environments/identifier'; | ||
| import { PentagonExpressionSemanticsMapper } from './pentagon/expression-semantics'; | ||
| import { IntervalSemanticsMaper } from './interval/condition-semantics'; | ||
| import { UpperBoundsSemanticsMapper } from './pentagon/upper-bounds/upper-bounds-condition-semantics'; | ||
| import { NumericIntervalInferenceVisitor } from './interval/numeric-interval-inference'; | ||
| import { SourceRange } from '../util/range'; | ||
| import { NumericPentagonInferenceVisitor } from './pentagon/numeric-pentagon-inference'; | ||
| import path from 'path'; | ||
| import type { AbsintVisitorConfiguration } from './absint-visitor'; | ||
| import { superBigJsonStringify } from '../util/json'; | ||
| import { UnsupportedFunctions } from './unsupported-functions'; | ||
| import type { LintingResultsSuccess } from '../linter/linter-format'; | ||
|
|
||
| if(process.argv.length < 4) { | ||
| console.error('Usage: ts-node src/abstract-interpretation/benchmarking.ts <file-to-analyze> <output-folder>'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const [_, __, filePath, outputDirectory] = process.argv; | ||
|
|
||
| interface FileMetadata { | ||
| fileName: string; | ||
| intervalResultFileName: string; | ||
| pentagonResultFileName: string; | ||
| loc: number; | ||
| cloc: number; | ||
| numOfDfgNodes: number; | ||
| numOfCfgNodes: number; | ||
| numOfConstants: number; | ||
| numOfRNumberConstants: number; | ||
| RNumberConstantNodeIds: NodeId[]; | ||
| numOfFunctionDefinitions: number; | ||
| numOfFunctionCalls: number; | ||
| numOfSupportedExpressionFunctionCalls: number; | ||
| SupportedExpressionFunctionCallNodeIds: NodeId[]; | ||
| numOfSupportedConditionFunctionCalls: number; | ||
| SupportedConditionFunctionCalls: NodeId[]; | ||
| numOfConditions: number; | ||
| numOfIfThenElse: number; | ||
| numOfWhile: number; | ||
| numOfStopIfNot: number; | ||
| numOfFnWithUnknownSideEffect: number; | ||
| linterDeadCodeIds: NodeId[]; | ||
| metadataGatheringInMs: number; | ||
| baselineInMs: number; | ||
| astInMs: number; | ||
| dfgInMs: number; | ||
| cfgInMs: number; | ||
| intervalAnalysisInMs: number; | ||
| intervalResultGatheringInMs: number; | ||
| pentagonAnalysisInMs: number; | ||
| pentagonResultGatheringInMs: number; | ||
| } | ||
|
|
||
| interface InferredValue { | ||
| nodeId: NodeId; | ||
| inferredValue: string; | ||
| significantFigures: string; | ||
| sourceLocation: string; | ||
| lexeme: string; | ||
| dfgTag: VertexType | 'unknown'; | ||
| } | ||
|
|
||
| void async function() { | ||
| fs.mkdirSync(outputDirectory, { recursive: true }); | ||
| const fileName = path.basename(filePath); | ||
| const fileNameWithoutEnding = path.parse(fileName).name; | ||
| const metadataOutputFile = fileNameWithoutEnding + '.json'; | ||
| const intervalResultFile = fileNameWithoutEnding + '-interval.json'; | ||
| const pentagonResultFile = fileNameWithoutEnding + '-pentagon.json'; | ||
|
|
||
| const baselineStart = performance.now(); | ||
| const analyzer = await new FlowrAnalyzerBuilder() | ||
|
schuler-henry marked this conversation as resolved.
|
||
| .setEngine('tree-sitter') | ||
| .build(); | ||
| analyzer.addRequest(fileProtocol + filePath); | ||
| const astStart = performance.now(); | ||
| const ast = await analyzer.normalize(); | ||
| const dfgStart = performance.now(); | ||
| const dfg = (await analyzer.dataflow()).graph; | ||
| const cfgStart = performance.now(); | ||
| const cfg = await analyzer.controlflow(); | ||
| const cfgEnd = performance.now(); | ||
| const ctx = analyzer.context(); | ||
| const baselineEnd = performance.now(); | ||
|
|
||
| const visitorContext: AbsintVisitorConfiguration = { normalizedAst: ast, dfg, controlFlow: cfg, ctx }; | ||
|
|
||
| // Run Interval Analysis | ||
| const intervalVisitor = new NumericIntervalInferenceVisitor(visitorContext); | ||
|
|
||
| const intervalStart = performance.now(); | ||
| intervalVisitor.start(); | ||
| const intervalEnd = performance.now(); | ||
|
|
||
| const intervalResultStart = performance.now(); | ||
| const intervalResults: InferredValue[] = []; | ||
| for(const nodeId of intervalVisitor.getAbstractTrace().keys()) { | ||
| const value = intervalVisitor.getAbstractValue(nodeId); | ||
| if(value !== undefined) { | ||
| const node = ast.idMap.get(nodeId); | ||
|
|
||
| intervalResults.push({ | ||
| nodeId: nodeId, | ||
|
schuler-henry marked this conversation as resolved.
|
||
| inferredValue: value.toString() ?? 'undefined', | ||
| significantFigures: value.significantFigures?.toString() ?? 'unknown', | ||
| sourceLocation: SourceRange.fromNode(node)?.toString() ?? 'unknown', | ||
| lexeme: node?.lexeme ?? 'unknown', | ||
| dfgTag: dfg.getVertex(nodeId)?.tag ?? 'unknown', | ||
| }); | ||
| } | ||
| } | ||
| const intervalResultEnd = performance.now(); | ||
|
|
||
| // Dump Interval Results | ||
| const intervalPath = path.join(outputDirectory, intervalResultFile); | ||
| fs.rmSync(intervalPath, { recursive: true, force: true }); | ||
| superBigJsonStringify(intervalResults, '\n', (msg) => fs.appendFileSync(intervalPath, msg)); | ||
|
|
||
| // Run Pentagon Analysis | ||
| const pentagonVisitor = new NumericPentagonInferenceVisitor(visitorContext); | ||
|
|
||
| const pentagonStart = performance.now(); | ||
| pentagonVisitor.start(); | ||
| const pentagonEnd = performance.now(); | ||
|
|
||
| const pentagonResultStart = performance.now(); | ||
| const pentagonResults: InferredValue[] = []; | ||
| for(const nodeId of pentagonVisitor.getAbstractTrace().keys()) { | ||
| const value = pentagonVisitor.getAbstractValue(nodeId); | ||
| if(value !== undefined) { | ||
| const node = ast.idMap.get(nodeId); | ||
|
|
||
| pentagonResults.push({ | ||
| nodeId: nodeId, | ||
|
schuler-henry marked this conversation as resolved.
|
||
| inferredValue: value.toString() ?? 'undefined', | ||
| significantFigures: value.value.interval.significantFigures?.toString() ?? 'unknown', | ||
| sourceLocation: SourceRange.fromNode(node)?.toString() ?? 'unknown', | ||
| lexeme: node?.lexeme ?? 'unknown', | ||
| dfgTag: dfg.getVertex(nodeId)?.tag ?? 'unknown', | ||
| }); | ||
| } | ||
| } | ||
| const pentagonResultEnd = performance.now(); | ||
|
|
||
| // Dump Pentagon Results | ||
| const pentagonPath = path.join(outputDirectory, pentagonResultFile); | ||
| fs.rmSync(pentagonPath, { recursive: true, force: true }); | ||
| superBigJsonStringify(pentagonResults, '\n', (msg) => fs.appendFileSync(pentagonPath, msg)); | ||
|
|
||
| // Create File and Runtime Metadata | ||
| const metadataStart = performance.now(); | ||
| const constants = dfg.verticesOfType(VertexType.Value).toArray(); | ||
| const numberConstants = constants.filter(value => ast.idMap.get(value[0])?.type === RType.Number); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you filter on the ast type you have to be more careful wrt. symbols that resolve to numeric constants (which you miss this way), like
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What woud be ur suggested solution?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could resolve the value with alias tracking and then check on the unpacked value in case of symbols etc. but generally I'd argue this is fine 1) you do not support pi etc. yourself right now 2) the other counts are very low 3) you do not care about NA(integer) do you? It was more a comment of something that should be noted wrt. statements made on the result.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay 😄 |
||
|
|
||
| const functionCalls = dfg.verticesOfType(VertexType.FunctionCall).toArray(); | ||
|
schuler-henry marked this conversation as resolved.
|
||
| const supportedExpressionFunctionCalls = functionCalls.filter(([_, dfgCall]) => | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would also be interesting to filter these supported function calls for those calls for which you inferred anything? |
||
| IntervalExpressionSemanticsMapper().find(([id]) => Identifier.matches(id, dfgCall.name)) !== undefined || | ||
| PentagonExpressionSemanticsMapper.find(([id]) => Identifier.matches(id, dfgCall.name)) !== undefined | ||
| ); | ||
| const supportedConditionFunctionCalls = functionCalls.filter(([_, dfgCall]) => | ||
| IntervalSemanticsMaper().find(([id]) => Identifier.matches(id, dfgCall.name)) !== undefined || | ||
| UpperBoundsSemanticsMapper().find(([id]) => Identifier.matches(id, dfgCall.name)) !== undefined || | ||
| ['&&', '!', '||', '&', '|'].some(id => Identifier.matches(id, dfgCall.name)) | ||
| ); | ||
| const conditions = functionCalls.filter(([_, dfgCall]) => | ||
| dfgCall.origin.includes(BuiltInProcName.IfThenElse) || dfgCall.origin.includes(BuiltInProcName.WhileLoop) || dfgCall.origin.includes(BuiltInProcName.StopIfNot) | ||
| ); | ||
| const functionsWithUnknownSideEffects = functionCalls.filter(([_, dfgCall]) => UnsupportedFunctions.isUnsupportedCall(dfgCall)); | ||
|
|
||
| const deadCodeQueryResult = await analyzer.query([{ | ||
| type: 'linter', | ||
| rules: ['dead-code'] | ||
| }]); | ||
| const deadCodeResult = deadCodeQueryResult.linter.results['dead-code'] as LintingResultsSuccess<'dead-code'>; | ||
| const linterDeadNodes = deadCodeResult.results.reduce((acc: NodeId[], val) => acc.concat(val.involvedId ?? []), []); | ||
|
|
||
|
|
||
| const metadataEnd = performance.now(); | ||
|
|
||
|
schuler-henry marked this conversation as resolved.
|
||
| const fileMetadata: FileMetadata = { | ||
| fileName: fileName, | ||
| intervalResultFileName: intervalResultFile, | ||
| pentagonResultFileName: pentagonResultFile, | ||
| loc: analyzer.inspectContext().files.getAllFiles()[0].content().toString().split('\n').length, | ||
| cloc: analyzer.inspectContext().files.getAllFiles()[0].content().toString().split('\n').filter(line => line.trim() !== '' && !line.trimStart().startsWith('#')).length, | ||
| numOfDfgNodes: dfg.vertices(true).toArray().length, | ||
| numOfCfgNodes: cfg.graph.vertices(false).size, | ||
| numOfConstants: constants.length, | ||
| numOfRNumberConstants: numberConstants.length, | ||
| RNumberConstantNodeIds: numberConstants.map(([node]) => node), | ||
| numOfFunctionDefinitions: dfg.verticesOfType(VertexType.FunctionDefinition).toArray().length, | ||
| numOfFunctionCalls: functionCalls.length, | ||
| numOfSupportedExpressionFunctionCalls: supportedExpressionFunctionCalls.length, | ||
| SupportedExpressionFunctionCallNodeIds: supportedExpressionFunctionCalls.map(([node]) => node), | ||
| numOfSupportedConditionFunctionCalls: supportedConditionFunctionCalls.length, | ||
| SupportedConditionFunctionCalls: supportedConditionFunctionCalls.map(([node]) => node), | ||
| numOfConditions: conditions.length, | ||
|
schuler-henry marked this conversation as resolved.
|
||
| numOfIfThenElse: functionCalls.filter(([_, dfgCall]) => dfgCall.origin.includes(BuiltInProcName.IfThenElse)).length, | ||
| numOfWhile: functionCalls.filter(([_, dfgCall]) => dfgCall.origin.includes(BuiltInProcName.WhileLoop)).length, | ||
| numOfStopIfNot: functionCalls.filter(([_, dfgCall]) => dfgCall.origin.includes(BuiltInProcName.StopIfNot)).length, | ||
| numOfFnWithUnknownSideEffect: functionsWithUnknownSideEffects.length, | ||
| linterDeadCodeIds: linterDeadNodes, | ||
| metadataGatheringInMs: metadataEnd - metadataStart, | ||
|
schuler-henry marked this conversation as resolved.
|
||
| baselineInMs: baselineEnd - baselineStart, | ||
| astInMs: dfgStart - astStart, | ||
| dfgInMs: cfgStart - dfgStart, | ||
| cfgInMs: cfgEnd - cfgStart, | ||
| intervalAnalysisInMs: intervalEnd - intervalStart, | ||
| intervalResultGatheringInMs: intervalResultEnd - intervalResultStart, | ||
|
schuler-henry marked this conversation as resolved.
|
||
| pentagonAnalysisInMs: pentagonEnd - pentagonStart, | ||
| pentagonResultGatheringInMs: pentagonResultEnd - pentagonResultStart | ||
| }; | ||
|
|
||
| // Dump File and Runtime Metadata | ||
| const fileMetadataPath = path.join(outputDirectory, metadataOutputFile); | ||
| fs.rmSync(fileMetadataPath, { recursive: true, force: true }); | ||
| fs.writeFileSync(fileMetadataPath, JSON.stringify(fileMetadata, null, 2)); | ||
| }(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, for comprehensibility, giving this function a name (e.g. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function would be more readable if you split parts of it into separate functions :)