File size: 1,811 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
import { withCurrent } from './describe.js'
import { Interface, Scenario, intoFullInterface } from './index.js'

export async function runScenarios(
  scenarios: Scenario[],
  iface: Interface
): Promise<void> {
  const fullIface = intoFullInterface(iface)
  if (scenarios.some((scenario) => scenario.only)) {
    scenarios = scenarios.filter((scenario) => scenario.only)
  }
  scenarios = await fullIface.filterScenarios(scenarios)
  let variants = []
  for (const scenario of scenarios) {
    let props = [{}]
    for (const [key, options] of Object.entries(scenario.config)) {
      const newProps = []
      for (const prop of props) {
        if (prop === 'scenario' || prop === 'name')
          throw new Error("Cannot use 'scenario' or 'name' as a property name")
        for (const value of options) {
          newProps.push({
            ...prop,
            [key]: value,
          })
        }
      }
      props = newProps
    }
    variants.push(
      ...props.map((props) => ({
        scenario,
        props,
      }))
    )
  }
  variants = await fullIface.filterScenarioVariants(variants)

  for (const variant of variants) {
    try {
      const measurements = new Map()
      await withCurrent(
        {
          iface: fullIface,
          measurements,
          scenario: variant,
        },
        async () => {
          await fullIface.start(variant.scenario.name, variant.props)
          measurements.set('start', {
            value: Date.now(),
            unit: 'ms',
          })
          await variant.scenario.fn(variant.props)
          await fullIface.end(variant.scenario.name, variant.props)
        }
      )
    } catch (e) {
      await fullIface.error(variant.scenario.name, variant.props, e)
      process.exitCode = 1
    }
  }

  await fullIface.finish()
}