-
Notifications
You must be signed in to change notification settings - Fork 1
/
daneel_ai_profiler.py
96 lines (66 loc) · 1.87 KB
/
daneel_ai_profiler.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import cProfile
import pstats
import daneel_ai
import os
import logging
import sys
import StringIO
import re
import picklegamestate
import cPickle
from daneel_ai import gameLoopBenchMark
from tp.client.cache import Cache
state_dir = "./states/"
def isGameStateFile(file_name):
return re.search('.*gamestate$', file_name)
def unpickle(pickle_location):
file = open(pickle_location, 'rb')
old = cPickle.load(file)
file.close()
return old
def getPickledStates():
global state_dir
file_list = os.listdir(state_dir)
return filter(isGameStateFile, file_list)
class FakeStdOut(object):
# wrapping class that
# means we can pipe stdout to a file and stdout if we want
def __init__(self, filename, mode):
self.file = open(filename, mode)
self.stderr = sys.stderr
sys.stderr = self
def __del__(self):
self.close()
def close(self):
if self.stderr is not None:
sys.stderr = self.stderr
self.stderr = None
if self.file is not None:
self.file.close()
self.file = None
def write(self, data):
self.file.write(data)
# self.stderr.write(data)
if __name__=='__main__':
global state_dir
list_of_states = getPickledStates()
# print list_of_states
# FIXME remove the hard codings here
rulesfile='risk'
turns = 0
connection = None
verbosity = 1
for state in list_of_states:
basename = state.partition('.')
output = state_dir + basename[0] + ".out"
profile = state_dir + state + ".profile"
# tested_state = picklegamestate.GameState().unpickle(state_dir + state)
print state_dir + state
cache = Cache('test')
cache.file = state_dir + state
cache.load()
cProfile.runctx('gameLoopBenchMark(rulesfile,turns,connection,cache,verbosity)', globals(), locals(), profile)
file = FakeStdOut(output, 'w')
p = pstats.Stats(profile,stream=file)
p.strip_dirs().sort_stats('time').print_stats(100)
file.close()