|
| 1 | +const modes = require('./modes.js') |
| 2 | +const sampling = require('./sampling.js') |
| 3 | + |
| 4 | +const analysis = { |
| 5 | + getErrorBudget: (script, defaultErrorBudget) => { |
| 6 | + if (script && script.sampling && script.sampling.errorBudget !== undefined) { |
| 7 | + return script.sampling.errorBudget |
| 8 | + } else if (defaultErrorBudget !== undefined) { |
| 9 | + return defaultErrorBudget |
| 10 | + } else { |
| 11 | + return sampling.defaults.sampling.errorBudget |
| 12 | + } |
| 13 | + }, |
| 14 | + |
| 15 | + /** |
| 16 | + * Analyze a set of sampling reports, each of which is the result of a sample battery's execution |
| 17 | + * results. |
| 18 | + * @param script Artillery script used |
| 19 | + * @param report The collection of reports to analyze |
| 20 | + * @param defaultErrorBudget Maximum number of errors allowed |
| 21 | + */ |
| 22 | + analyzeSamples: (script, report, defaultErrorBudget) => { |
| 23 | + let reports |
| 24 | + |
| 25 | + if (report.reports) { |
| 26 | + reports = report.reports // eslint-disable-line prefer-destructuring |
| 27 | + } else { |
| 28 | + reports = [report] |
| 29 | + } |
| 30 | + |
| 31 | + const totals = { |
| 32 | + scenariosCreated: 0, |
| 33 | + scenariosCompleted: 0, |
| 34 | + requestsCompleted: 0, |
| 35 | + codes: {}, |
| 36 | + errors: {}, |
| 37 | + } |
| 38 | + |
| 39 | + const mergeReportPropertyCounts = property => |
| 40 | + reports.forEach(test => |
| 41 | + Object.keys(test[property]).forEach((key) => { |
| 42 | + const countForTest = test[property][key] |
| 43 | + const isNewKey = totals[property][key] === undefined |
| 44 | + |
| 45 | + if (isNewKey) { |
| 46 | + totals[property][key] = countForTest |
| 47 | + } else { |
| 48 | + totals[property][key] += countForTest |
| 49 | + } |
| 50 | + })) |
| 51 | + |
| 52 | + mergeReportPropertyCounts('codes') |
| 53 | + mergeReportPropertyCounts('errors') |
| 54 | + |
| 55 | + const accumulateReportTotals = property => |
| 56 | + reports.forEach((test) => { totals[property] += test[property] }) |
| 57 | + |
| 58 | + accumulateReportTotals('scenariosCreated') |
| 59 | + accumulateReportTotals('scenariosCompleted') |
| 60 | + accumulateReportTotals('requestsCompleted') |
| 61 | + |
| 62 | + const totalErrors = Object.keys(totals.errors).reduce((total, message) => total + totals.errors[message], 0) |
| 63 | + |
| 64 | + const errorBudget = analysis.getErrorBudget(script, defaultErrorBudget) |
| 65 | + |
| 66 | + const finalReport = { |
| 67 | + errors: totalErrors, |
| 68 | + reports, |
| 69 | + totals, |
| 70 | + } |
| 71 | + |
| 72 | + if (totalErrors > errorBudget) { |
| 73 | + finalReport.errorMessage = `${ |
| 74 | + modes.getModeForDisplay(script) |
| 75 | + } failure: scenarios run: ${ |
| 76 | + totals.scenariosCreated |
| 77 | + }, total errors: ${ |
| 78 | + totalErrors |
| 79 | + }, error budget: ${ |
| 80 | + errorBudget |
| 81 | + }` |
| 82 | + } |
| 83 | + |
| 84 | + return finalReport |
| 85 | + }, |
| 86 | + |
| 87 | + /** |
| 88 | + * Analyze a set of reports, each of which is the result of an acceptance test's execution |
| 89 | + * @param timeNow Origin time of the artillery test |
| 90 | + * @param script Artillery script used |
| 91 | + * @param settings Platform settings used |
| 92 | + * @param results Results of the tests |
| 93 | + * |
| 94 | + * @returns Object |
| 95 | + */ |
| 96 | + analyzeAcceptance: (timeNow, script, settings, results) => |
| 97 | + analysis.analyzeSamples(script, results, sampling.defaults.acceptance.errorBudget), |
| 98 | + |
| 99 | + /** |
| 100 | + * Analyze a set of reports, each of which is the result of an monitoring test's execution |
| 101 | + * @param timeNow Origin time of the artillery test |
| 102 | + * @param script Artillery script used |
| 103 | + * @param settings Platform settings used |
| 104 | + * @param results Results of the tests |
| 105 | + * |
| 106 | + * @returns Object |
| 107 | + */ |
| 108 | + analyzeMonitoring: (timeNow, script, settings, results) => |
| 109 | + analysis.analyzeSamples(script, results, sampling.defaults.monitoring.errorBudget), |
| 110 | + |
| 111 | + /** |
| 112 | + * Analyze the performance results. If there is one payload, then it is a report. Otherwise, it is a set of payloads. |
| 113 | + * @param timeNow Origin time of the artillery test |
| 114 | + * @param script Artillery script used |
| 115 | + * @param settings Platform settings used |
| 116 | + * @param results Results of the tests |
| 117 | + * |
| 118 | + * @returns Object |
| 119 | + */ |
| 120 | + analyzePerformance: (timeNow, script, settings, results) => { |
| 121 | + if (results && results.length === 1) { |
| 122 | + return results[0] // return the report |
| 123 | + } else if (results) { |
| 124 | + return results |
| 125 | + } else { // eslint-disable-next-line no-underscore-dangle |
| 126 | + return { message: `load test from ${script._genesis} successfully completed from ${timeNow} @ ${Date.now()}` } |
| 127 | + } |
| 128 | + }, |
| 129 | +} |
| 130 | + |
| 131 | +module.exports = analysis |
0 commit comments