summaryrefslogtreecommitdiffstatshomepage
path: root/tests/performance/log-results.js
blob: f8214b68d4eb313b48f6422f8a47ba34daf479f9 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node

/*
 * Get the test results and format them in the way required by the API.
 *
 * Contains some backward compatibility logic for the original test suite format,
 * see #59900 for details.
 */

/**
 * External dependencies.
 */
const https = require( 'https' );
const [ token, branch, hash, baseHash, date, host ] =
	process.argv.slice( 2 );
const { median, parseFile, accumulateValues } = require( './utils' );

const testSuiteMap = {
	'Admin › Locale: en_US': 'admin',
	'Admin › Locale: de_DE': 'admin-l10n',
	'Homepage › Theme: twentytwentyone, Locale: en_US': 'home-classic-theme',
	'Homepage › Theme: twentytwentyone, Locale: de_DE':
		'home-classic-theme-l10n',
	'Homepage › Theme: twentytwentythree, Locale: en_US': 'home-block-theme',
	'Homepage › Theme: twentytwentythree, Locale: de_DE':
		'home-block-theme-l10n',
	'Homepage › Theme: twentytwentyfour, Locale: en_US': 'home-twentytwentyfour',
	'Homepage › Theme: twentytwentyfour, Locale: de_DE':
		'home-twentytwentyfour-l10n',
	'Homepage › Theme: twentytwentyfive, Locale: en_US': 'home-twentytwentyfive',
	'Homepage › Theme: twentytwentyfive, Locale: de_DE':
		'home-twentytwentyfive-l10n',
};

/**
 * @type {Array<{file: string, title: string, results: Record<string,number[]>[]}>}
 */
const afterStats = parseFile( 'performance-results.json' );

if ( ! afterStats.length ) {
	console.error( 'No results file found' );
	process.exit( 1 );
}

/**
 * @type {Array<{file: string, title: string, results: Record<string,number[]>[]}>}
 */
const baseStats = parseFile( 'base-performance-results.json' );

if ( ! baseStats.length ) {
	console.error( 'No base results file found' );
	process.exit( 1 );
}

/**
 * @type {Record<string, number>}
 */
const metrics = {};

/**
 * @type {Record<string, number>}
 */
const baseMetrics = {};

for ( const { title, results } of afterStats ) {
	const testSuiteName = testSuiteMap[ title ];
	if ( ! testSuiteName ) {
		continue;
	}

	const baseStat = baseStats.find( ( s ) => s.title === title );

	const currResults = accumulateValues( results );
	const baseResults = accumulateValues( baseStat.results );

	for ( const [ metric, values ] of Object.entries( currResults ) ) {
		metrics[ `${ testSuiteName }-${ metric }` ] = median( values );
	}

	for ( const [ metric, values ] of Object.entries( baseResults ) ) {
		baseMetrics[ `${ testSuiteName }-${ metric }` ] = median( values );
	}
}

const data = new TextEncoder().encode(
	JSON.stringify( {
		branch,
		hash,
		baseHash,
		timestamp: date,
		metrics: metrics,
		baseMetrics: baseMetrics,
	} )
);

const options = {
	hostname: host,
	port: 443,
	path: '/api/log?token=' + token,
	method: 'POST',
	headers: {
		'Content-Type': 'application/json',
		'Content-Length': data.length,
	},
};

const req = https.request( options, ( res ) => {
	console.log( `statusCode: ${ res.statusCode }` );

	res.on( 'data', ( d ) => {
		process.stdout.write( d );
	} );
} );

req.on( 'error', ( error ) => {
	console.error( error );
	process.exit( 1 );
} );

req.write( data );
req.end();