File size: 3,935 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import minimist from 'minimist'
import { setCurrentScenarios } from './describe.js'
import { join } from 'path'
import { Scenario, ScenarioVariant, runScenarios } from './index.js'
import compose from './interfaces/compose.js'
import { pathToFileURL } from 'url'
;(async () => {
  const knownArgs = new Set([
    'scenario',
    's',
    'json',
    'j',
    'console',
    'datadog',
    'snowflake',
    'interactive',
    'i',
    'help',
    'h',
    '?',
    '_',
  ])
  const args = minimist(process.argv.slice(2), {
    alias: {
      s: 'scenario',
      j: 'json',
      i: 'interactive',
      '?': 'help',
      h: 'help',
    },
  })

  if (args.help || (Object.keys(args).length === 1 && args._.length === 0)) {
    console.log('Usage: devlow-bench [options] <scenario files>')
    console.log('## Selecting scenarios')
    console.log(
      '  --scenario=<filter>, -s=<filter>   Only run the scenario with the given name'
    )
    console.log(
      '  --interactive, -i                  Select scenarios and variants interactively'
    )
    console.log(
      '  --<prop>=<value>                   Filter by any variant property defined in scenarios'
    )
    console.log('## Output')
    console.log(
      '  --json=<path>, -j=<path>           Write the results to the given path as JSON'
    )
    console.log(
      '  --console                          Print the results to the console'
    )
    console.log(
      '  --datadog[=<hostname>]             Upload the results to Datadog'
    )
    console.log(
      '                                     (requires DATADOG_API_KEY environment variables)'
    )
    console.log(
      '  --snowflake[=<batch-uri>]          Upload the results to Snowflake'
    )
    console.log(
      '                                     (requires SNOWFLAKE_TOPIC_NAME and SNOWFLAKE_SCHEMA_ID and environment variables)'
    )
    console.log('## Help')
    console.log('  --help, -h, -?                     Show this help')
  }

  const scenarios: Scenario[] = []
  setCurrentScenarios(scenarios)

  for (const path of args._) {
    await import(pathToFileURL(join(process.cwd(), path)).toString())
  }

  setCurrentScenarios(null)

  const cliIface = {
    filterScenarios: async (scenarios: Scenario[]) => {
      if (args.scenario) {
        const filter = [].concat(args.scenario)
        return scenarios.filter((s) =>
          filter.some((filter) => s.name.includes(filter))
        )
      }
      return scenarios
    },
    filterScenarioVariants: async (variants: ScenarioVariant[]) => {
      const propEntries = Object.entries(args).filter(
        ([key]) => !knownArgs.has(key)
      )
      if (propEntries.length === 0) return variants
      for (const [key, value] of propEntries) {
        const values = (Array.isArray(value) ? value : [value]).map((v) =>
          v.toString()
        )
        variants = variants.filter((variant) => {
          const prop = variant.props[key]
          if (typeof prop === 'undefined') return false
          const str = prop.toString()
          return values.some((v) => str.includes(v))
        })
      }
      return variants
    },
  }
  let ifaces = [
    cliIface,
    args.interactive && (await import('./interfaces/interactive.js')).default(),
    args.json && (await import('./interfaces/json.js')).default(args.json),
    args.datadog &&
      (await import('./interfaces/datadog.js')).default(
        typeof args.datadog === 'string' ? { host: args.datadog } : undefined
      ),
    args.snowflake &&
      (await import('./interfaces/snowflake.js')).default(
        typeof args.snowflake === 'string'
          ? { gatewayUri: args.snowflake }
          : undefined
      ),
    args.console !== false &&
      (await import('./interfaces/console.js')).default(),
  ].filter((x) => x)
  await runScenarios(scenarios, compose(...ifaces))
})().catch((e) => {
  console.error(e.stack)
  process.exit(1)
})