Skip to content

Commit 702a5a6

Browse files
committed
Merge branch 'alexhanh'
2 parents 6dedaad + 5d7ac73 commit 702a5a6

File tree

4 files changed

+29
-44
lines changed

4 files changed

+29
-44
lines changed

profile.py

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sc2reader import Replay
88

99
def parse_replays():
10+
# Run four times to dampen noise
1011
for run in range(1,4):
1112
file_list = []
1213
rootdir = "test_replays/build17811/"
@@ -20,45 +21,18 @@ def parse_replays():
2021
print file
2122
replay = Replay(file)
2223

23-
# Problem with the profiler is that it adds conciderable amount of overhead
24-
# cProfile.run("parse_replays()","replay_profile")
25-
# stats = Stats("replay_profile")
26-
# stats.strip_dirs().sort_stats("time").print_stats(30)
24+
# Use the results of this function when comparing performance with other libraries.
25+
def benchmark_with_timetime():
26+
start = time.time()
27+
parse_replays()
28+
diff = time.time() - start
29+
print diff
2730

28-
start = time.time()
29-
parse_replays()
30-
diff = time.time() - start
31-
print diff
31+
def profile():
32+
cProfile.run("parse_replays()","replay_profile")
33+
stats = Stats("replay_profile")
34+
stats.strip_dirs().sort_stats("time").print_stats(30)
3235

33-
# ========================================
34-
# Results for March 1 2011
35-
# With cProfile
3636

37-
# Tue Mar 1 06:25:22 2011 replay_profile
38-
#
39-
# 20568477 function calls (20565106 primitive calls) in 37.650 seconds
40-
#
41-
# Ordered by: internal time
42-
# List reduced from 184 to 15 due to restriction <15>
43-
#
44-
# ncalls tottime percall cumtime percall filename:lineno(function)
45-
# 3484431 7.903 0.000 8.559 0.000 utils.py:42(get_big)
46-
# 3057363 5.960 0.000 13.308 0.000 utils.py:89(get_big_int)
47-
# 33 4.828 0.146 35.395 1.073 parsers.py:225(load)
48-
# 472272 2.948 0.000 3.360 0.000 objects.py:98(__init__)
49-
# 42861 2.227 0.000 6.391 0.000 eventparsers.py:154(load)
50-
# 472272 2.222 0.000 3.223 0.000 parsers.py:254(get_parser)
51-
# 114174 1.225 0.000 4.265 0.000 eventparsers.py:50(load)
52-
# 294732 1.224 0.000 3.983 0.000 eventparsers.py:394(load)
53-
# 473916 1.060 0.000 3.568 0.000 utils.py:108(get_timestamp)
54-
# 1142493 0.926 0.000 0.926 0.000 utils.py:66(peek)
55-
# 3965823 0.745 0.000 0.745 0.000 {len}
56-
# 7095 0.705 0.000 0.705 0.000 {method 'upper' of 'str' objects}
57-
# 695461 0.644 0.000 0.644 0.000 {range}
58-
# 1 0.575 0.575 37.571 37.571 profile.py:9(parse_replays)
59-
# 566628 0.491 0.000 0.491 0.000 {method 'rjust' of 'str' objects}
60-
61-
62-
# With time.time()
63-
# 28.2304489613
64-
# ========================================
37+
benchmark_with_timetime()
38+
#profile()

sc2reader/objects.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def __init__(self, pid, data, realm="us"):
163163
self.handicap = data[6]
164164
self.team = None # A number to be supplied later
165165
self.type = "" # Human or Computer
166+
self.events = list()
166167

167168
def __str__(self):
168169
return "Player %s - %s (%s)" % (self.pid, self.name, self.actual_race)

sc2reader/parsers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,16 @@ def load(self, replay, filecontents):
178178
length += 128
179179

180180
text = bytes.get_string(length)
181-
replay.messages.append(Message(time, replay.player[player_id], target, text))
182-
181+
try:
182+
replay.messages.append(Message(time, replay.player[player_id], target, text))
183+
except KeyError:
184+
# This was added because some replay sites added their own tampered
185+
# messages to replays with non-existent player_id.
186+
#
187+
# This will simply ignore and fail silently if such message is
188+
# found.
189+
pass
190+
183191
recorders = [player for player in replay.players if player and player.recorder==True]
184192
if len(recorders) > 1:
185193
raise ValueError("There should be 1 and only 1 recorder; %s were found" % len(recorders))

test_replays/test_all.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
# Encoding: UTF-8
2+
13
# Run tests with "py.test" in the project root dir
2-
#encoding:UTF-8
34
import os, sys
45
import pytest
56

6-
#sys.path.append(os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../")))
77
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../")))
88

99
from sc2reader import Replay
@@ -119,6 +119,8 @@ def test_ffa():
119119
# Player 'Boom' won because the last building of the last player was destroyed,
120120
# but the winner cannot be parsed because "Player has left" event isn't generated.
121121
# Unknown result is the best we can do.
122+
print replay.length
123+
print replay.events[-2]
122124
assert replay.winner_known == False
123125

124126
def test_unknown_winner():
@@ -145,7 +147,7 @@ def test_us_realm():
145147
assert replay.player['ShadesofGray'].url == "http://us.battle.net/sc2/en/profile/2358439/1/ShadesofGray/"
146148
assert replay.player['reddawn'].url == "http://us.battle.net/sc2/en/profile/2198663/1/reddawn/"
147149

148-
# We can be really proud: phpsc2replay fails with the messages!
150+
# TODO: Current problem.. both players are set as the recording players
149151
def test_kr_realm_and_tampered_messages():
150152
replay = Replay("test_replays/build17811/11.SC2Replay")
151153
assert replay.player['명지대학교'].url == "http://kr.battle.net/sc2/en/profile/258945/1/명지대학교/"

0 commit comments

Comments
 (0)