@@ -369,6 +369,22 @@ def cleanup_result_dict(result_dict):
369
369
370
370
371
371
def get_result_dict (data , mode ):
372
+ """
373
+ Extract performance metrics from the data and calculate the results for each metric.
374
+
375
+ This function uses a regular expression to extract performance metrics and
376
+ their values from the data.
377
+ It then calculates the results for each metric using the `get_data_for_entry` function and
378
+ stores them in a dictionary.
379
+
380
+ Parameters:
381
+ data (str): The data containing the performance metrics and their values.
382
+ mode (str): The mode of the device ('desktop' or 'mobile').
383
+
384
+ Returns:
385
+ dict: A dictionary where the keys are the performance metrics and
386
+ the values are dictionaries containing the results for each metric.
387
+ """
372
388
result_dict = {}
373
389
tmp_dict = {}
374
390
regex = r"(?P<name>TTFB|DOMContentLoaded|firstPaint|FCP|LCP|Load|TBT|CLS|FirstVisualChange|SpeedIndex|VisualComplete85|LastVisualChange)\:[ ]{0,1}(?P<value>[0-9\.ms]+)" # pylint: disable=line-too-long
@@ -391,6 +407,22 @@ def get_result_dict(data, mode):
391
407
return result_dict
392
408
393
409
def get_data_for_entry (mode , key , values ):
410
+ """
411
+ Calculate the performance data for a given entry.
412
+
413
+ This function calculates the median, range, points, and message for a given performance metric.
414
+ It processes the values, which are in milliseconds or seconds,
415
+ and calculates the total, biggest, median, and range.
416
+ It also gets the full name of the metric and calculates the points.
417
+
418
+ Parameters:
419
+ mode (str): The mode of the device ('desktop' or 'mobile').
420
+ key (str): The performance metric key.
421
+ values (list): The list of values for the performance metric.
422
+
423
+ Returns:
424
+ dict: A dictionary containing the median, range, mode, points, message, and values.
425
+ """
394
426
biggest = 0
395
427
total = 0
396
428
value_range = 0
@@ -448,6 +480,26 @@ def get_fullname(key):
448
480
return fullname
449
481
450
482
def get_points (mode , key , result , median ):
483
+ """
484
+ Calculate the performance points based on the mode, key, result, and median.
485
+
486
+ This function assigns points based on different performance metrics such as:
487
+ TTFB, TBT, FCP, LCP, CLS, SpeedIndex, FirstVisualChange, VisualComplete85, and Load.
488
+ The points are calculated differently for each metric and
489
+ can also vary based on the mode ('desktop' or 'mobile').
490
+
491
+ Parameters:
492
+ mode (str): The mode of the device ('desktop' or 'mobile').
493
+ key (str): The performance metric key.
494
+ result (float): The result value for the SpeedIndex related metrics.
495
+ median (float): The median value for the TTFB, TBT, FCP, LCP, and CLS metrics.
496
+
497
+ Returns:
498
+ float: The calculated performance points.
499
+
500
+ Note:
501
+ For more information on SpeedIndex, refer to: https://docs.webpagetest.org/metrics/speedindex/
502
+ """
451
503
points = - 1
452
504
if 'TTFB' in key :
453
505
points = get_ttfb_points (mode , median )
@@ -479,6 +531,16 @@ def get_points(mode, key, result, median):
479
531
return points
480
532
481
533
def get_cls_points (median ):
534
+ """
535
+ Calculate the Cumulative Layout Shift (CLS) points based on the mode and median.
536
+
537
+ Parameters:
538
+ mode (str): The mode of the device ('desktop' or 'mobile').
539
+ median (float): The median CLS in milliseconds.
540
+
541
+ Returns:
542
+ float: The calculated CLS points.
543
+ """
482
544
points = 5.0
483
545
if median <= 0.1 :
484
546
points = 5.0
@@ -489,6 +551,16 @@ def get_cls_points(median):
489
551
return points
490
552
491
553
def get_fcp_points (median ):
554
+ """
555
+ Calculate the First Contentful Paint (FCP) points based on the mode and median.
556
+
557
+ Parameters:
558
+ mode (str): The mode of the device ('desktop' or 'mobile').
559
+ median (float): The median FCP in milliseconds.
560
+
561
+ Returns:
562
+ float: The calculated FCP points.
563
+ """
492
564
points = 5.0
493
565
if median <= 1800 :
494
566
points = 5.0
@@ -499,6 +571,16 @@ def get_fcp_points(median):
499
571
return points
500
572
501
573
def get_tbt_points (median ):
574
+ """
575
+ Calculate the Total Blocking Time (TBT) points based on the mode and median.
576
+
577
+ Parameters:
578
+ mode (str): The mode of the device ('desktop' or 'mobile').
579
+ median (float): The median TBT in milliseconds.
580
+
581
+ Returns:
582
+ float: The calculated TBT points.
583
+ """
502
584
points = 5.0
503
585
if median <= 200 :
504
586
points = 5.0
@@ -509,6 +591,16 @@ def get_tbt_points(median):
509
591
return points
510
592
511
593
def get_lcp_points (mode , median ):
594
+ """
595
+ Calculate the Largest Contentful Paint (LCP) points based on the mode and median.
596
+
597
+ Parameters:
598
+ mode (str): The mode of the device ('desktop' or 'mobile').
599
+ median (float): The median LCP in milliseconds.
600
+
601
+ Returns:
602
+ float: The calculated LCP points.
603
+ """
512
604
points = 5.0
513
605
if 'desktop' in mode :
514
606
if median <= 500 :
@@ -527,6 +619,16 @@ def get_lcp_points(mode, median):
527
619
return points
528
620
529
621
def get_ttfb_points (mode , median ):
622
+ """
623
+ Calculate the Time to First Byte (TTFB) points based on the mode and median.
624
+
625
+ Parameters:
626
+ mode (str): The mode of the device ('desktop' or 'mobile').
627
+ median (float): The median TTFB in milliseconds.
628
+
629
+ Returns:
630
+ float: The calculated TTFB points.
631
+ """
530
632
points = 5.0
531
633
if 'desktop' in mode :
532
634
if median <= 250 :
0 commit comments