-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcost.py
More file actions
36 lines (27 loc) · 1.28 KB
/
cost.py
File metadata and controls
36 lines (27 loc) · 1.28 KB
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
"""Cost function which assigns a cost to a font enrichment latency."""
import math
NO_COST_THRESHOLD_S = 0.4
TIMEOUT_THRESHOLD_S = 3.5
MAX_COST = 1000
SIGMOID_WIDTH = 11.5
def cost(time_ms):
"""Assigns a cost to a measure of request latency.
The general approach is this:
- For request times less than some threshold cost is nearly 0 (while the
page is still loading other resources).
- For requests above the threshold cost rises non-linearly.
- However, there is a certain point beyond which cost maxes out. Typically
browsers at some point will give up on a font load and proceed without it.
So any wait beyond that point will not have any further negative effects.
To model this behaviour we use a sigmoid function, specifically the logistic function.
It is specifically shaped such that cost beings to rise after NO_COST_THRESHOLD and
hit's it's maximum value shortly after the TIMEOUT_THRESHOLD.
See the design doc
(https://docs.google.com/document/d/1kx62tpy5hGIbHh6tHMAryon9Sgye--W_IsHTeCMlmEo/edit)
for a more detailed discussion.
"""
time_s = time_ms / 1000
width = TIMEOUT_THRESHOLD_S - NO_COST_THRESHOLD_S
scale = SIGMOID_WIDTH / width
value = scale * (time_s - 0.5 * width - NO_COST_THRESHOLD_S)
return MAX_COST * (1 / (1 + math.exp(-value)))