File size: 2,100 Bytes
9f5cf6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { buildDistribution } from '../utils/stats.js'
import type {
  BenchmarkConfig,
  RunContext,
  Scenario,
  ScenarioSummary,
  SingleExecution,
} from '../types.js'

function normalizeError(error: unknown): string {
  if (error instanceof Error) return `${error.name}: ${error.message}`
  return String(error)
}

function summarize(

  scenario: Scenario,

  executions: SingleExecution[],

): ScenarioSummary {
  const successRuns = executions.filter(item => item.ok && !item.skipped).length
  const failedRuns = executions.filter(item => !item.ok && !item.skipped).length
  const skippedRuns = executions.filter(item => item.skipped).length
  const measured = executions.filter(item => !item.skipped).map(item => item.durationMs)
  return {
    id: scenario.id,
    name: scenario.name,
    category: scenario.category,
    description: scenario.description,
    tags: scenario.tags,
    totalRuns: executions.length,
    skippedRuns,
    successRuns,
    failedRuns,
    successRate:
      executions.length === 0
        ? 0
        : (successRuns / Math.max(1, successRuns + failedRuns)) * 100,
    durationMs: buildDistribution(measured),
    examples: executions
      .map(item => item.details)
      .filter(Boolean)
      .slice(0, 3) as Array<Record<string, unknown>>,
    errors: executions
      .map(item => item.error)
      .filter((value): value is string => Boolean(value))
      .slice(0, 8),
  }
}

export async function runScenarios({

  scenarios,

  context,

}: {

  scenarios: Scenario[]

  context: RunContext

  config: BenchmarkConfig

}): Promise<ScenarioSummary[]> {
  const results: ScenarioSummary[] = []
  for (const scenario of scenarios) {
    try {
      const executions = await scenario.run(context)
      results.push(summarize(scenario, executions))
    } catch (error) {
      results.push(
        summarize(scenario, [
          {
            ok: false,
            durationMs: 0,
            error: normalizeError(error),
          },
        ]),
      )
    }
  }
  return results
}