-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize rating initialization #839
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from server.rating import Leaderboard, PlayerRatings, RatingType | ||
|
@@ -135,6 +137,16 @@ def test_initialization_transient(chained_ratings): | |
assert chained_ratings["tmm_2v2"] == (300, 200) | ||
|
||
|
||
def test_initialization_transient_caching(chained_ratings): | ||
# Return a new mock on each call. By default it will return the same mock | ||
chained_ratings._get_initial_rating = mock.Mock(side_effect=mock.Mock) | ||
|
||
chained_ratings["global"] = (1000, 50) | ||
|
||
assert chained_ratings["tmm_2v2"] == chained_ratings["tmm_2v2"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a typo? How will this ever fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checks that the rating is cached and not recomputed. If it were recomputed we would get different mock objects for the two calls. |
||
chained_ratings._get_initial_rating.assert_called_once() | ||
|
||
|
||
def test_initialization_with_high_deviation(chained_ratings): | ||
chained_ratings["ladder_1v1"] = (1000, 150) | ||
assert chained_ratings["ladder_1v1"] == (1000, 150) | ||
|
@@ -161,6 +173,30 @@ def test_dict_update(chained_ratings): | |
assert chained_ratings["global"] == (750, 100) | ||
|
||
|
||
def test_dict_update_caching(chained_ratings): | ||
# Return a new mock on each call. By default it will return the same mock | ||
chained_ratings._get_initial_rating = mock.Mock(side_effect=mock.Mock) | ||
|
||
chained_ratings["ladder_1v1"] = (1000, 50) | ||
previous_rating = chained_ratings["global"] | ||
chained_ratings._get_initial_rating.assert_called_once() | ||
|
||
chained_ratings.update({ | ||
"ladder_1v1": (500, 100) | ||
}) | ||
# Global should be re-initialized after dict update | ||
assert chained_ratings["global"] != previous_rating | ||
assert chained_ratings._get_initial_rating.call_count == 2 | ||
|
||
chained_ratings.update({ | ||
"ladder_1v1": (500, 100), | ||
"global": (750, 100) | ||
}) | ||
# Global should not be re-initialized | ||
assert chained_ratings["global"] == (750, 100) | ||
assert chained_ratings._get_initial_rating.call_count == 2 | ||
|
||
|
||
def test_ratings_update_same_leaderboards(chained_leaderboards): | ||
ratings1 = PlayerRatings(chained_leaderboards) | ||
ratings2 = PlayerRatings(chained_leaderboards) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it because of the rating initialization? It doesn't become clear, why this comment on performance is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s there because this is now the second time I’ve had to optimize something that was added to that function because it showed up in the profiler.