UNPKG

wdio-performancetotal-service

Version:

WebdriverIO service for analyzing test flow performance

100 lines (99 loc) 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PerformanceCache = void 0; const partial_log_entry_1 = require("./entities/partial-log-entry"); const performance_log_entry_1 = require("./entities/performance-log-entry"); const file_writer_1 = require("./helpers/file-writer"); const step_suffix_1 = require("./constants/step-suffix"); const id_generator_1 = require("./helpers/id-generator"); class PerformanceCache { constructor() { this._startLogEntries = new Array(); this._endLogEntries = new Array(); this._performanceEntries = new Array(); } sampleStart(stepName, instanceId) { const logEntry = this.setSample(partial_log_entry_1.StepType.Start, stepName, instanceId); this._startLogEntries.unshift(logEntry); } sampleEnd(stepName, instanceId) { const logEntry = this.setSample(partial_log_entry_1.StepType.End, stepName, instanceId); this._endLogEntries.push(logEntry); } getSampleTime(stepName) { return this.getPerformanceEntryTime(stepName); } async flush(fileName, browser, isTestPassed) { this.createPerformanceEntries(isTestPassed, browser); await this.writePerformanceDataToFile(fileName); } setSample(stepType, stepName, instanceId) { let id = ""; const logEntry = new partial_log_entry_1.PartialLogEntry(); if (stepType == partial_log_entry_1.StepType.Start) { id = new id_generator_1.IdGenerator().getId(); } else { id = this.getStartIdByStepName(stepName, instanceId); } logEntry.id = id; logEntry.instanceId = instanceId; logEntry.name = stepName; logEntry.type = partial_log_entry_1.StepType.Start; logEntry.time = new Date().getTime(); logEntry.displayTime = new Date().toLocaleString(); return logEntry; } getPerformanceEntryTime(stepName) { let duration = 0; const startEntry = this._startLogEntries.find(e => e.name == stepName + step_suffix_1.StepSuffix.used); if (startEntry) { const endEntry = this._endLogEntries.find(e => e.id == startEntry.id); if (endEntry) { duration = endEntry.time - startEntry.time; } } return duration; } createPerformanceEntries(isTestPassed, browser) { const revStartEntries = this._startLogEntries.reverse(); revStartEntries.forEach(startEntry => { const tempPerformanceEntry = new performance_log_entry_1.PerformanceLogEntry(); const correspondedEndEntry = this._endLogEntries.find((e) => e.id == startEntry.id); if (correspondedEndEntry) { tempPerformanceEntry.id = startEntry.id; tempPerformanceEntry.instanceId = startEntry.instanceId; tempPerformanceEntry.name = correspondedEndEntry.name; tempPerformanceEntry.brName = browser.capabilities.browserName ?? ""; tempPerformanceEntry.startDisplayTime = startEntry.displayTime; tempPerformanceEntry.startTime = startEntry.time; tempPerformanceEntry.endTime = correspondedEndEntry.time; tempPerformanceEntry.duration = tempPerformanceEntry.getDuration(); tempPerformanceEntry.isTestPassed = isTestPassed; this._performanceEntries.push(tempPerformanceEntry); } }); } getStartIdByStepName(stepName, instanceId) { let id = ""; const startEntry = this._startLogEntries.find((e) => e.name == stepName && e.instanceId == instanceId); if (startEntry) { id = startEntry.id; startEntry.name += step_suffix_1.StepSuffix.used; } return id; } clearData() { this._startLogEntries = []; this._endLogEntries = []; this._performanceEntries = []; } async writePerformanceDataToFile(fileName) { for (let performanceEntry of this._performanceEntries) { await file_writer_1.FileWriter.getInstance().appendLineToFile(fileName, `${JSON.stringify(performanceEntry)}\n`); } ; this.clearData(); } } exports.PerformanceCache = PerformanceCache;