3
3
from pathlib import Path
4
4
import os
5
5
import re
6
+ import subprocess
6
7
from datetime import datetime
7
8
from models import Rating
8
9
from tests .utils import get_config_or_default , get_translation
11
12
SITESPEED_USE_DOCKER = get_config_or_default ('sitespeed_use_docker' )
12
13
13
14
def get_result (sitespeed_use_docker , arg ):
14
-
15
15
result = ''
16
16
if sitespeed_use_docker :
17
17
base_directory = Path (os .path .dirname (
18
18
os .path .realpath (__file__ )) + os .path .sep ).parent
19
19
data_dir = base_directory .resolve ()
20
20
21
- command = "docker run --rm -v {1 }:/sitespeed.io sitespeedio/sitespeed.io:latest {0}" . format (
22
- arg , data_dir )
21
+ command = ( f "docker run --rm -v { data_dir } :/sitespeed.io "
22
+ f"sitespeedio/sitespeed.io:latest { arg } " )
23
23
24
- import subprocess
25
- process = subprocess .Popen (command .split (), stdout = subprocess .PIPE )
26
- output , _ = process .communicate (timeout = REQUEST_TIMEOUT * 10 )
27
- result = str (output )
24
+ with subprocess .Popen (command .split (), stdout = subprocess .PIPE ) as process :
25
+ output , _ = process .communicate (timeout = REQUEST_TIMEOUT * 10 )
26
+ result = str (output )
28
27
else :
29
- import subprocess
30
-
31
- command = "node node_modules{1}sitespeed.io{1}bin{1}sitespeed.js {0}" .format (
32
- arg , os .path .sep )
33
-
34
- process = subprocess .Popen (
35
- command .split (), stdout = subprocess .PIPE )
28
+ command = (f"node node_modules{ os .path .sep } sitespeed.io{ os .path .sep } bin{ os .path .sep } "
29
+ f"sitespeed.js { arg } " )
36
30
37
- output , _ = process .communicate (timeout = REQUEST_TIMEOUT * 10 )
38
- result = str (output )
31
+ with subprocess .Popen (
32
+ command .split (), stdout = subprocess .PIPE ) as process :
33
+ output , _ = process .communicate (timeout = REQUEST_TIMEOUT * 10 )
34
+ result = str (output )
39
35
40
36
return result
41
37
@@ -107,13 +103,14 @@ def run_test(global_translation, lang_code, url):
107
103
rating += validator_rating
108
104
result_dict .update (validator_result_dict )
109
105
110
- if use_reference and reference_rating != None :
106
+ if use_reference and reference_rating is not None :
111
107
validator_rating_overall = validator_rating .get_overall ()
112
108
if reference_rating < validator_rating_overall and \
113
109
validator_rating_overall != - 1 and \
114
110
validator_rating_overall != - 1 :
115
- rating .overall_review += '- [{2}] Advice: Rating may improve from {0} to {1} with {3} changes\r \n ' .format (
116
- reference_rating , validator_rating_overall , reference_name , validator_name )
111
+ rating .overall_review += (
112
+ f'- [{ reference_name } ] Advice: Rating may improve from { reference_rating } to '
113
+ f'{ validator_rating_overall } with { validator_name } changes\r \n ' )
117
114
118
115
print (global_translation ('TEXT_TEST_END' ).format (
119
116
datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )))
@@ -257,14 +254,15 @@ def validate_on_mobile(url):
257
254
return result_dict
258
255
259
256
260
- def rate_result_dict (result_dict , reference_result_dict , mode , global_translation , local_translation ):
257
+ def rate_result_dict (result_dict , reference_result_dict ,
258
+ mode , global_translation , local_translation ):
261
259
limit = 500
262
260
263
261
rating = Rating (global_translation )
264
262
performance_review = ''
265
263
overview_review = ''
266
264
267
- if reference_result_dict != None :
265
+ if reference_result_dict is not None :
268
266
reference_name = 'UNKNOWN'
269
267
if result_dict ['name' ].startswith ('mobile' ):
270
268
reference_name = 'mobile'
@@ -290,8 +288,9 @@ def rate_result_dict(result_dict, reference_result_dict, mode, global_translatio
290
288
if mobile_obj ['median' ] > (limit + noxternal_obj ['median' ]):
291
289
value_diff = mobile_obj ['median' ] - noxternal_obj ['median' ]
292
290
293
- txt = '- [{2}] Advice: {0} may improve by {1:.2f}ms with {3} changes\r \n ' .format (
294
- key , value_diff , reference_name , mode )
291
+ txt = (
292
+ f'- [{ reference_name } ] Advice: { key } may improve by '
293
+ f'{ value_diff :.2f} ms with { mode } changes\r \n ' )
295
294
296
295
overview_review += txt
297
296
key_matching = True
@@ -304,8 +303,9 @@ def rate_result_dict(result_dict, reference_result_dict, mode, global_translatio
304
303
if mobile_obj ['range' ] > (limit + noxternal_obj ['range' ]):
305
304
value_diff = mobile_obj ['range' ] - noxternal_obj ['range' ]
306
305
307
- txt = '- [{2}] Advice: {0} could be ±{1:.2f}ms less "bumpy" with {3} changes\r \n ' .format (
308
- key , value_diff , reference_name , mode )
306
+ txt = (
307
+ f'- [{ reference_name } ] Advice: { key } could be ±{ value_diff :.2f} ms '
308
+ f'less "bumpy" with { mode } changes\r \n ' )
309
309
310
310
overview_review += txt
311
311
key_matching = True
@@ -329,13 +329,13 @@ def rate_result_dict(result_dict, reference_result_dict, mode, global_translatio
329
329
continue
330
330
if 'points' in value and value ['points' ] != - 1 :
331
331
points = value ['points' ]
332
- entry_rating = Rating (_ )
332
+ entry_rating = Rating (global_translation )
333
333
entry_rating .set_overall (points )
334
334
entry_rating .set_performance (
335
335
points , value ['msg' ])
336
336
rating += entry_rating
337
337
else :
338
- performance_review += '{0} \r \n ' . format ( value ['msg' ])
338
+ performance_review += f" { value ['msg' ]} \r \n "
339
339
340
340
rating .overall_review = rating .overall_review + overview_review
341
341
rating .performance_review = rating .performance_review + performance_review
@@ -372,9 +372,7 @@ def get_result_dict(data, mode):
372
372
number = float (
373
373
value .replace ('s' , '' )) * 1000
374
374
total += number
375
- if number > biggest :
376
- biggest = number
377
- # print(' ', number, total)
375
+ biggest = max (biggest , number )
378
376
value_count = len (values )
379
377
if value_count < 2 :
380
378
value_range = 0
@@ -449,7 +447,9 @@ def get_result_dict(data, mode):
449
447
else :
450
448
points = 1.0
451
449
452
- elif 'SpeedIndex' in key or 'FirstVisualChange' in key or 'VisualComplete85' in key or 'Load' in key :
450
+ elif 'SpeedIndex' in key or \
451
+ 'FirstVisualChange' in key or \
452
+ 'VisualComplete85' in key or 'Load' in key :
453
453
# https://docs.webpagetest.org/metrics/speedindex/
454
454
adjustment = 500
455
455
if 'mobile' in mode :
@@ -469,7 +469,7 @@ def get_result_dict(data, mode):
469
469
'range' : value_range ,
470
470
'mode' : mode ,
471
471
'points' : points ,
472
- 'msg' : '- [{2 }] {3 }: {0 :.2f}ms, ±{1 :.2f}ms' . format ( result , value_range , mode , fullname ) ,
472
+ 'msg' : f '- [{ mode } ] { fullname } : { result :.2f} ms, ±{ value_range :.2f} ms' ,
473
473
'values' : values
474
474
}
475
475
0 commit comments