/
profile.lua
163 lines (136 loc) · 3.54 KB
/
profile.lua
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
local classlib = arg[1]
local test = arg[2]
local iterations = 5000000
local Profiler = require("Profiler").new(test.." on library "..classlib.." ("..iterations.." iterations)")
local class
if classlib == 'classy' then
class = require("classy")
elseif classlib == 'middleclass' then
class = require("middleclass")
end
local note = classlib..' '..test.." ("..iterations.." iterations)"
local file = 'Profiler/'..classlib..'-'..test..'-result.txt'
print("Running test: "..note)
local function WriteReport(profiler, filename)
local f,err = io.open(filename, "w")
assert(f, err)
f:write(profiler:GetFormattedReport())
f:flush()
f:close()
end
local BaseClass
local Subclass
local DoubleSubclass
if classlib == 'classy' then
if test == 'classes' then
Profiler:Start()
end
BaseClass = class("BaseClass", {
Bar = 0;
Baz = 0;
Foo = 0;
})
function BaseClass:__init__(foo,bar,baz)
self.Foo = foo
self.Bar = bar
self.Baz = baz
end
function BaseClass:func()
self.Bar = self.Bar + 1
end
BaseClass = BaseClass:finalize()
Subclass = BaseClass:subclass("Subclass", {
NewTable = 0;
NewBoolean = false;
})
function Subclass:__init__(foo, bar, baz, bool)
BaseClass.__init__(self, foo, bar, baz)
self.NewTable = {"Test Table"}
self.NewBoolean = bool
end
Subclass = Subclass:finalize()
DoubleSubclass = Subclass:subclass("DoubleSubclass", {
AnotherNewTable = 0;
})
function DoubleSubclass:__init__(bar)
Subclass.__init__(self, 2, bar, "String value!", false)
self.AnotherNewTable = {}
end
DoubleSubclass = DoubleSubclass:finalize()
if test == 'classes' then
Profiler:Stop()
WriteReport(Profiler, file)
end
elseif classlib == 'middleclass' then
if test == 'classes' then
Profiler:Start()
end
BaseClass = class("BaseClass")
function BaseClass:initialize(foo,bar,baz)
self.Foo = foo
self.Bar = bar
self.Baz = baz
end
function BaseClass:func()
self.Bar = self.Bar + 1
end
Subclass = BaseClass:subclass("Subclass")
function Subclass:initialize(foo, bar, baz, bool)
BaseClass.initialize(self, foo, bar, baz)
self.NewTable = {"Test Table"}
self.NewBoolean = bool
end
DoubleSubclass = Subclass:subclass("DoubleSubclass")
function DoubleSubclass:initialize(bar)
Subclass.initialize(self, 2, bar, "String value!", false)
self.AnotherNewTable = {}
end
if test == 'classes' then
Profiler:Stop()
WriteReport(Profiler, file)
end
end
if test == 'alloc' then
local t = {}
for i=1, iterations do
t[i] = true
end
Profiler:Start()
for i=1, iterations do
t[i] = BaseClass:new(2, 3, "String value!")
end
Profiler:Stop()
WriteReport(Profiler, file)
elseif test == 'methods' then
local bar = 3
local instance = BaseClass:new(2, bar, "String value!")
Profiler:Start()
for i=1, iterations do
instance:func()
end
Profiler:Stop()
WriteReport(Profiler, file)
assert(instance.Bar == bar + iterations, "Result expected from function calls was wrong. Expected: "..bar + iterations.." Got: "..instance.Bar)
elseif test == 'inheritance-alloc' then
local t = {}
for i=1, iterations do
t[i] = true
end
local i
Profiler:Start()
for i=1, iterations do
t[i] = DoubleSubclass:new(3)
end
Profiler:Stop()
WriteReport(Profiler, file)
elseif test == 'inheritance-methods' then
local bar = 3
local instance = DoubleSubclass:new(bar)
Profiler:Start()
for i=1, iterations do
instance:func()
end
Profiler:Stop()
WriteReport(Profiler, file)
assert(instance.Bar == bar + iterations, "Result expected from function calls was wrong. Expected: "..bar + iterations.." Got: "..instance.Bar)
end