Best JavaScript code snippet using best
analyze.ts
Source:analyze.ts
1/*2 * Copyright (c) 2019, salesforce.com, inc.3 * All rights reserved.4 * SPDX-License-Identifier: MIT5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT6*/7import json2md from 'json2md';8import { BenchmarkComparison, ResultComparison, BenchmarkMetricNames, BenchmarkStats, ResultComparisonBenchmark, ResultComparisonGroup, ResultComparisonProject } from '@best/types';9interface MarkdownTable {10 table: {11 headers: string[];12 rows: string[][];13 }14}15interface SignificantlyChangedSummary {16 improved: string[][]17 regressed: string[][]18}19type GroupedTables = { [projectName: string]: MarkdownTable[] }20function padding(n: number) {21 return n > 022 ? Array.apply(null, Array((n - 1) * 3))23 .map(() => ' ')24 .join('') + 'ââ '25 : '';26}27function generateMarkdownFromGroupedTables(tables: GroupedTables) {28 const flattenedTables = Object.keys(tables).reduce((groups, projectName): json2md.DataObject[] => {29 groups.push({ h2: `*${projectName}*` });30 groups.push(...tables[projectName]);31 return groups;32 }, <json2md.DataObject[]>[])33 return json2md(flattenedTables);34}35function generateRow(36 name: string,37 metrics: {38 baseStats: BenchmarkStats;39 targetStats: BenchmarkStats;40 samplesComparison: 0 | 1 | -1;41 },42 includeEmojiTrend: boolean43): string[] {44 const baseStats = metrics.baseStats;45 const targetStats = metrics.targetStats;46 const samplesComparison = metrics.samplesComparison;47 const percentage = (Math.abs(baseStats.median - targetStats.median) / baseStats.median) * 100;48 const relativeTrend = targetStats.median - baseStats.median;49 const sign = Math.sign(relativeTrend) === 1 ? '+' : '';50 const comparisonEmoji = (samplesComparison === 0 ? 'ð' : samplesComparison === 1 ? 'ð' : 'ð');51 return [52 name,53 `${baseStats.median.toFixed(2)} (± ${baseStats.medianAbsoluteDeviation.toFixed(2)}ms)`,54 `${targetStats.median.toFixed(2)} (± ${targetStats.medianAbsoluteDeviation.toFixed(2)}ms)`,55 sign + relativeTrend.toFixed(1) + 'ms (' + percentage.toFixed(1) + '%)' + (includeEmojiTrend ? ` ${comparisonEmoji}` : '')56 ]57}58function generateRowsFromComparison<RowType>(stats: ResultComparison, handler: (node: ResultComparisonBenchmark, parentName: string) => RowType[], name: string = '', initialRows: RowType[] = []) {59 if (stats.type === "project" || stats.type === "group") {60 return stats.comparisons.reduce((rows, node): RowType[] => {61 if (node.type === "project" || node.type === "group") {62 return generateRowsFromComparison(node, handler, node.name, rows);63 } else if (node.type === "benchmark") {64 rows.push(...handler(node, name));65 }66 return rows;67 }, initialRows);68 } else {69 return initialRows;70 }71}72function significantlyChangedRows(stats: ResultComparison, threshold: number, name: string = '', initialRows: SignificantlyChangedSummary = { improved: [], regressed: [] }) {73 const highThreshold = Math.abs(threshold); // handle whether the threshold is positive or negative74 const lowThreshold = -1 * highThreshold;75 if (stats.type === "project" || stats.type === "group") {76 return stats.comparisons.reduce((rows, node): SignificantlyChangedSummary => {77 if (node.type === "project" || node.type === "group") {78 return significantlyChangedRows(node, threshold, node.name, rows);79 } else if (node.type === "benchmark") {80 // for the significantly changed summary, we only check for aggregate81 const metrics = node.metrics.aggregate;82 if (metrics) {83 const { baseStats, targetStats, samplesComparison } = metrics;84 85 if (samplesComparison !== 0 && baseStats.median > 1 && targetStats.median > 1) { // ensures passes Mann-Whiteney U test and results are more than 1ms86 const percentage = (Math.abs(baseStats.median - targetStats.median) / baseStats.median) * 100;87 const relativeTrend = targetStats.median - baseStats.median;88 const relativePercentage = Math.sign(relativeTrend) * percentage;89 const row = generateRow(`${name}/${node.name}`, metrics, false);90 if (relativePercentage < lowThreshold) { // less than a negative is GOOD (things got faster)91 rows.improved.push(row);92 } else if (relativePercentage > highThreshold) { // more than a positive is WORSE (things got slower)93 rows.regressed.push(row);94 }95 }96 }97 }98 return rows;99 }, initialRows)100 } else {101 return initialRows;102 }103}104function generateAllRows(stats: ResultComparison) {105 return generateRowsFromComparison(stats, (node, parentName) => {106 const rows: string[][] = [];107 const emptyFields = Array.apply(null, Array(3)).map(() => '-');108 rows.push([`${parentName}/${node.name}`, ...emptyFields]);109 Object.keys(node.metrics).forEach(metric => {110 const metrics = node.metrics[metric as BenchmarkMetricNames];111 if (metrics) {112 rows.push(generateRow(padding(1) + metric, metrics, true));113 }114 })115 return rows;116 })117}118function generateCommentWithTables(result: BenchmarkComparison, handler: (node: ResultComparisonProject | ResultComparisonGroup, baseCommit: string, targetCommit: string) => MarkdownTable[]) {119 const { baseCommit, targetCommit, comparisons } = result;120 const grouped: GroupedTables = comparisons.reduce((tables, node): GroupedTables => {121 if (node.type === "project" || node.type === "group") {122 const markdownTables = handler(node, baseCommit, targetCommit);123 if (markdownTables.length) {124 return {125 ...tables,126 [node.name]: markdownTables127 }128 }129 return tables;130 }131 return tables;132 }, <GroupedTables>{});133 return generateMarkdownFromGroupedTables(grouped);134}135export function generateComparisonSummary(result: BenchmarkComparison, threshold: number) {136 return generateCommentWithTables(result, (node, base, target) => {137 const changes = significantlyChangedRows(node, threshold);138 const tables: MarkdownTable[] = [];139 140 if (changes.improved.length) {141 tables.push({142 table: {143 headers: [`â
Improvements`, `base (\`${base}\`)`, `target (\`${target}\`)`, 'trend'],144 rows: changes.improved145 }146 })147 }148 if (changes.regressed.length) {149 tables.push({150 table: {151 headers: [`â Regressions`, `base (\`${base}\`)`, `target (\`${target}\`)`, 'trend'],152 rows: changes.regressed153 }154 });155 }156 return tables;157 })158}159function generateAllRowsTable(baseCommit: string, targetCommit: string, stats: ResultComparison): MarkdownTable {160 const { name: benchmarkName } = stats;161 const mdName = benchmarkName.replace('.benchmark', '');162 return {163 table: {164 headers: [`${mdName}`, `base (\`${baseCommit}\`)`, `target (\`${targetCommit}\`)`, 'trend'],165 rows: generateAllRows(stats)166 }167 }168}169export function generateComparisonComment(result: BenchmarkComparison) {170 const tablesMarkdown = generateCommentWithTables(result, (node, base, target) => {171 const tables = node.comparisons.map(child => {172 return generateAllRowsTable(base, target, child);173 })174 return tables;175 });176 return `# Full Results\n\n${tablesMarkdown}`;177}178// this takes all the results and recursively goes through them179// then it creates a flat list of all of the percentages of change180export function generatePercentages(stats: ResultComparison): number[] {181 return generateRowsFromComparison(stats, (node, parentName) => {182 const rows: number[] = [];183 Object.keys(node.metrics).map(metricName => {184 const metrics = node.metrics[metricName as BenchmarkMetricNames];185 if (metrics) {186 const { baseStats, targetStats, samplesComparison } = metrics;187 const baseMed = baseStats.median;188 const targetMed = targetStats.median;189 190 const percentage = Math.abs((baseMed - targetMed) / baseMed * 100);191 const relativeTrend = targetMed - baseMed;192 // ensures passes Mann-Whiteney U test and results are more than 1ms193 if (samplesComparison !== 0 && baseMed > 1 && targetMed > 1) {194 rows.push(Math.sign(relativeTrend) * percentage);195 } else {196 rows.push(0); // otherwise we count it as zero197 }198 }199 })200 return rows;201 })...
Using AI Code Generation
1const { tablesMarkdown } = require('@daisy/ace-core');2];3console.log(tablesMarkdown(table));4const { tablesMarkdown } = require('@daisy/ace-core');5];6console.log(tablesMarkdown(table, { align: 'right' }));7const { tablesMarkdown } = require('@daisy/ace-core');8];9console.log(tablesMarkdown(table, { align: 'left' }));10const { tablesMarkdown } = require('@daisy/ace-core');11];12console.log(tablesMarkdown(table, { align: 'center' }));13const { tablesMarkdown } = require('@daisy/ace-core');
Using AI Code Generation
1var BestBuy = require('./BestBuy.js');2var bestBuy = new BestBuy();3var table = bestBuy.tablesMarkdown("tv");4console.log(table);5var request = require('request');6var cheerio = require('cheerio');7var Table = require('cli-table');8function BestBuy() {9}10BestBuy.prototype.getTvs = function(callback) {11 request(this.url, function(error, response, body) {12 if (!error && response.statusCode == 200) {13 callback(body);14 }15 });16};17BestBuy.prototype.tablesMarkdown = function(type) {18 var self = this;19 if (type == "tv") {20 self.getTvs(function(body) {21 var $ = cheerio.load(body);22 var table = new Table({23 });24 $('div.item-info').each(function(i, element) {25 var name = $(element).find('a').text();26 var price = $(element).find('div.priceView-hero-price span').text();27 var image = $(element).find('img').attr('src');28 table.push([name, price, image]);29 });30 console.log(table.toString());31 });32 }33};34module.exports = BestBuy;35{36 "dependencies": {37 },38 "devDependencies": {},39 "scripts": {40 },41}42var BestBuy = require('./BestBuy.js');
Using AI Code Generation
1var BestMatch = require('BestMatch');2var list1 = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];3var list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];4var table = new BestMatch();5var result = table.tablesMarkdown(list1, list2);6console.log(result);
Using AI Code Generation
1var BestPractice = require('bestpractice');2var bp = new BestPractice();3var table = bp.createTable();4var tableMarkdown = bp.tablesMarkdown(table);5var markdownFile = bp.createMarkdownFile(tableMarkdown);6var htmlFile = bp.createHtmlFile(markdownFile);7var BestPractice = require('bestpractice');8var bp = new BestPractice();9var table = bp.createTable();10var tableMarkdown = bp.tablesMarkdown(table);11var markdownFile = bp.createMarkdownFile(tableMarkdown);12var htmlFile = bp.createHtmlFile(markdownFile);13var pdfFile = bp.createPdfFile(htmlFile);14var BestPractice = require('bestpractice');15var bp = new BestPractice();16var table = bp.createTable();17var tableMarkdown = bp.tablesMarkdown(table);18var markdownFile = bp.createMarkdownFile(tableMarkdown);19var htmlFile = bp.createHtmlFile(markdownFile);20var pdfFile = bp.createPdfFile(htmlFile);21var docxFile = bp.createDocxFile(pdfFile);
Using AI Code Generation
1var table = document.getElementById("table");2var tableArray = new Array();3tableArray[0] = new Array("Name", "Age", "City", "State", "Country", "Zip Code");4tableArray[1] = new Array("John Doe", "45", "New York", "NY", "USA", "10001");5tableArray[2] = new Array("Jane Doe", "23", "New York", "NY", "USA", "10001");6tableArray[3] = new Array("Joe Smith", "34", "New York", "NY", "USA", "10001");7tableArray[4] = new Array("Jane Smith", "23", "New York", "NY", "USA", "10001");8tableArray[5] = new Array("Joe Bloggs", "34", "New York", "NY", "USA", "10001");9tableArray[6] = new Array("Jane Bloggs", "23", "New York", "NY", "USA", "10001");10tableArray[7] = new Array("John Smith", "34", "New York", "NY", "USA", "10001");11tableArray[8] = new Array("Jane Doe", "23", "New York", "NY", "USA", "10001");12tableArray[9] = new Array("Joe Bloggs", "34", "New York", "NY", "USA", "10001");13tableArray[10] = new Array("Jane Bloggs", "23", "New York", "NY", "USA", "10001");14table.innerHTML = tablesMarkdown(tableArray);
Using AI Code Generation
1var bestResults = [];2bestResults.push(new BestResult("A", 3, 3, 3, 3));3bestResults.push(new BestResult("B", 2, 2, 2, 2));4bestResults.push(new BestResult("C", 1, 1, 1, 1));5var table = BestResult.tablesMarkdown(bestResults);6document.write(table);
Using AI Code Generation
1const BestPractice = require('bestpractice');2const bestpractice = new BestPractice();3const fs = require('fs');4const path = require('path');5const myPath = path.join(__dirname, 'test3.md');6const myPath2 = path.join(__dirname, 'test4.md');7const myPath3 = path.join(__dirname, 'test5.md');8const myPath4 = path.join(__dirname, 'test6.md');9const myPath5 = path.join(__dirname, 'test7.md');10const myPath6 = path.join(__dirname, 'test8.md');11const myPath7 = path.join(__dirname, 'test9.md');12const myPath8 = path.join(__dirname, 'test10.md');13const myPath9 = path.join(__dirname, 'test11.md');14const myPath10 = path.join(__dirname, 'test12.md');15const myPath11 = path.join(__dirname, 'test13.md');16const myPath12 = path.join(__dirname, 'test14.md');17const myPath13 = path.join(__dirname, 'test15.md');18const myPath14 = path.join(__dirname, 'test16.md');19const myPath15 = path.join(__dirname, 'test17.md');20const myPath16 = path.join(__dirname, 'test18.md');21const myPath17 = path.join(__dirname, 'test19.md');22const myPath18 = path.join(__dirname, 'test20.md');23const myPath19 = path.join(__dirname, 'test21.md');24const myPath20 = path.join(__dirname, 'test22.md');25const myPath21 = path.join(__dirname, 'test23.md');26const myPath22 = path.join(__dirname, 'test24.md');27const myPath23 = path.join(__dirname, 'test25.md');28const myPath24 = path.join(__dirname, 'test26.md');29const myPath25 = path.join(__dirname, 'test27.md');30const myPath26 = path.join(__dirname, 'test28.md');31const myPath27 = path.join(__dirname, 'test29.md');32const myPath28 = path.join(__dirname, '
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!