Skip to content

Commit

Permalink
refactor, wip: try to register within the individual scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce committed Aug 26, 2023
1 parent de919cc commit 5b08c65
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 103 deletions.
20 changes: 16 additions & 4 deletions src/cli/benchmark-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { LimitBenchmarkPool } from '../benchmark/parallel-helper'
import * as os from 'os'
import { guard } from '../util/assert'
import fs from 'fs'
import { scripts } from './scripts-info'
import { register, scripts } from './common/scripts-info'

const now = date2string(new Date())

Expand All @@ -22,6 +22,18 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: `File to write all the measurements to in a per-file-basis (defaults to {italic benchmark-${now}.json})`, defaultValue: `benchmark-${now}.json`, typeLabel: '{underline file}' }
]

const toolName = 'benchmark'
const description = 'Benchmark the static backwards slicer'

register('benchmark', {
toolName,
target: 'benchmark-app',
description,
type: 'master script',
usageExample: `${toolName} "example-folder/"`,
options: optionDefinitions
})

export interface BenchmarkCliOptions {
verbose: boolean
help: boolean
Expand All @@ -34,14 +46,14 @@ export interface BenchmarkCliOptions {

export const optionHelp = [
{
header: scripts.benchmark.description,
header: description,
content: 'Slice given files with additional benchmark information'
},
{
header: 'Synopsis',
content: [
`$ ${scripts.benchmark.toolName} {italic example-folder/}`,
`$ ${scripts.benchmark.toolName} {bold --help}`
`$ ${toolName} {italic example-folder/}`,
`$ ${toolName} {bold --help}`
]
},
{
Expand Down
21 changes: 17 additions & 4 deletions src/cli/benchmark-helper-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RParseRequestFromFile } from '../r-bridge'
import fs from 'fs'
import { displayEnvReplacer } from '../util/json'
import { guard } from '../util/assert'
import { scripts } from './scripts-info'
import { register } from './common/scripts-info'

export const optionDefinitions: OptionDefinition[] = [
{ name: 'verbose', alias: 'v', type: Boolean, description: 'Run with verbose logging [do not use for the real benchmark as this affects the time measurements, but only to find errors]' },
Expand All @@ -17,6 +17,19 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: `File to write the measurements to (appends a single line in JSON format)`, typeLabel: '{underline file}' },
]

const toolName = 'benchmark-single'
const description = 'Helper Script to Benchmark the Slicer'

register('benchmark-single', {
toolName,
target: 'benchmark-helper-app',
description,
usageExample: `${toolName} "example.R" --output "example.json"`,
options: optionDefinitions,
type: 'helper script',
masterScripts: [ 'benchmark' ]
})

export interface SingleBenchmarkCliOptions {
verbose: boolean
help: boolean
Expand All @@ -27,14 +40,14 @@ export interface SingleBenchmarkCliOptions {

export const optionHelp = [
{
header: scripts['benchmark-helper'].description,
header: description,
content: 'Will slice for all possible variables, signal by exit code if slicing was successful, and can be run standalone'
},
{
header: 'Synopsis',
content: [
`$ ${scripts['benchmark-helper'].toolName} {italic example-file.R} --output {italic output.json}`,
`$ ${scripts['benchmark-helper'].toolName} {bold --help}`
`$ ${toolName} {italic example-file.R} --output {italic output.json}`,
`$ ${toolName} {bold --help}`
]
},
{
Expand Down
7 changes: 7 additions & 0 deletions src/cli/common/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* The goal of this module is simply to streamline the creation of new scripts.
*
* @module
*/
import { ScriptInformation, scripts } from './scripts-info'

35 changes: 35 additions & 0 deletions src/cli/common/scripts-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* This file contains the references to all scripts, as well as their explanations and arguments.
*
* @module
*/
import { MergeableRecord } from '../../util/objects'
import { OptionDefinition } from 'command-line-usage'


interface BaseScriptInformation extends MergeableRecord {
toolName: string
target: string
description: string
usageExample: string
options: OptionDefinition[]
}

export interface MasterScriptInformation extends BaseScriptInformation {
type: 'master script',
}

export interface HelperScriptInformation extends BaseScriptInformation {
type: 'helper script',
masterScripts: string[]
}

export type ScriptInformation = MasterScriptInformation | HelperScriptInformation


export const scripts = new Map<string, ScriptInformation>()

export function register(name: string, information: ScriptInformation) {
scripts.set(name, information)
}

20 changes: 16 additions & 4 deletions src/cli/export-quads-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import commandLineUsage, { OptionDefinition } from 'command-line-usage'
import { serialize2quads } from '../util/quads'
import fs from 'fs'
import { allRFilesFrom } from '../util/files'
import { scripts } from './scripts-info'
import { register } from './common/scripts-info'


export const optionDefinitions: OptionDefinition[] = [
Expand All @@ -16,6 +16,18 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: 'File to write all the generated quads to (defaults to {italic out.quads})', typeLabel: '{underline file}' },
]

const toolName = 'export-quads'
const description = 'Export quads of the normalized AST of a given R code file'

register('export-quads', {
toolName,
target: 'export-quads-app',
description,
usageExample: `${toolName} "example.R" --output "example.quads"`,
options: optionDefinitions,
type: 'master script',
})

export interface QuadsCliOptions {
verbose: boolean
help: boolean
Expand All @@ -26,14 +38,14 @@ export interface QuadsCliOptions {

export const optionHelp = [
{
header: scripts['export-quads'].description,
header: description,
content: 'Generate RDF N-Quads from the AST of a given R script'
},
{
header: 'Synopsis',
content: [
`$ ${scripts['export-quads'].toolName} {bold -i} {italic example.R} {bold --output} {italic "example.quads"}`,
`$ ${scripts['export-quads'].toolName} {bold --help}`
`$ ${toolName} {bold -i} {italic example.R} {bold --output} {italic "example.quads"}`,
`$ ${toolName} {bold --help}`
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl/commands/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { quitCommand } from './quit'
import { scripts } from '../../scripts-info'
import { scripts } from '../../common/scripts-info'
import { waitOnScript } from '../execute'
import { splitArguments } from '../../../util/args'
import { ReplCommand } from './main'
Expand Down
72 changes: 0 additions & 72 deletions src/cli/scripts-info.ts

This file was deleted.

22 changes: 17 additions & 5 deletions src/cli/slicer-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { guard } from '../util/assert'
import { SingleSlicingCriterion, SlicingCriteria } from '../slicing'
import { BenchmarkSlicer, stats2string, summarizeSlicerStats } from '../benchmark'
import { NodeId } from '../r-bridge'
import { scripts } from './scripts-info'
import { register, scripts } from './common/scripts-info'

export const optionDefinitions: OptionDefinition[] = [
{ name: 'verbose', alias: 'v', type: Boolean, description: 'Run with verbose logging' },
Expand All @@ -18,6 +18,18 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: 'File to write all the generated quads to (defaults to the commandline)', typeLabel: '{underline file}' },
]

const toolName = 'slicer'
const description = 'Static backwards executable slicer for R'

register('slicer', {
toolName,
target: 'slicer-app',
description,
options: optionDefinitions,
usageExample: `${toolName} -c "12@product" test/testfiles/example.R`,
type: 'master script',
})

export interface SlicerCliOptions {
verbose: boolean
help: boolean
Expand All @@ -30,15 +42,15 @@ export interface SlicerCliOptions {

export const optionHelp = [
{
header: scripts.slicer.description,
header: description,
content: 'Slice R code based on a given slicing criterion'
},
{
header: 'Synopsis',
content: [
`$ ${scripts.slicer.toolName} {bold -c} {italic "12@product"} {italic test/testfiles/example.R}`,
`$ ${scripts.slicer.toolName} {bold -i} {italic example.R} {bold --stats} {bold --criterion} {italic "8:3;3:1;12@product"}`,
`$ ${scripts.slicer.toolName} {bold --help}`
`$ ${toolName} {bold -c} {italic "12@product"} {italic test/testfiles/example.R}`,
`$ ${toolName} {bold -i} {italic example.R} {bold --stats} {bold --criterion} {italic "8:3;3:1;12@product"}`,
`$ ${toolName} {bold --help}`
]
},
{
Expand Down
12 changes: 11 additions & 1 deletion src/cli/statistics-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ import {
printFeatureStatistics,
initFileProvider,
setFormatter,
voidFormatter, ContextsWithCount
voidFormatter, ContextsWithCount, toolName, description
} from '../statistics'
import { log, LogLevel } from '../util/log'
import commandLineArgs from 'command-line-args'
import commandLineUsage from 'command-line-usage'
import { guard } from '../util/assert'
import { allRFilesFrom, writeTableAsCsv } from '../util/files'
import { DefaultMap } from '../util/defaultmap'
import { register } from './common/scripts-info'

register('stats', {
toolName,
target: 'statistics-app',
description,
options: optionDefinitions,
usageExample: `${toolName} -i example.R --output-dir "output-folder/"`,
type: 'master script',
})

const options = commandLineArgs(optionDefinitions) as StatsCliOptions

Expand Down
20 changes: 16 additions & 4 deletions src/cli/summarizer-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import fs from 'fs'
import { SlicingCriteria } from '../slicing'
import { escape } from '../statistics'
import { displayEnvReplacer } from '../util/json'
import { scripts } from './scripts-info'
import { register } from './common/scripts-info'

export const optionDefinitions: OptionDefinition[] = [
{ name: 'verbose', alias: 'v', type: Boolean, description: 'Run with verbose logging' },
Expand All @@ -24,6 +24,18 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: `Basename of the summaries (defaults to {italic <input>-summary})`, typeLabel: '{underline file}' },
]

const toolName = 'summarizer'
const description = 'Summarize the results of the benchmark'

register('summarizer', {
toolName,
target: 'summarizer-app',
description,
options: optionDefinitions,
usageExample: `${toolName} "benchmark.json"`,
type: 'master script',
})

export interface BenchmarkCliOptions {
verbose: boolean
help: boolean
Expand All @@ -34,14 +46,14 @@ export interface BenchmarkCliOptions {

export const optionHelp = [
{
header: scripts.summarizer.description,
header: description,
content: 'Summarize and explain the results of the benchmark tool. Summarizes in two stages: first per-request, and then overall'
},
{
header: 'Synopsis',
content: [
`$ ${scripts.summarizer.toolName} {italic benchmark.json}`,
`$ ${scripts.summarizer.toolName} {bold --help}`
`$ ${toolName} {italic benchmark.json}`,
`$ ${toolName} {bold --help}`
]
},
{
Expand Down
Loading

0 comments on commit 5b08c65

Please sign in to comment.