-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdw_benchmark.inc
302 lines (244 loc) · 6.88 KB
/
tdw_benchmark.inc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/** ======================================================================== **\
Benchmark
| Description:
Allows for the possibility to compare the performance of different
algorithms (functions).
Example:
benchmark SomeFunction[1000](local)
{
local = 5;
printf("%d", local);
}
| Version:
1.1.1
| License:
Copyright (c) 2016 Double V
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgement in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
\** ======================================================================== **/
#if defined _inc_tdw_benchmark
#undef _inc_tdw_benchmark
#endif
#if defined _TDW_BENCHMARK
#endinput
#endif
#define _TDW_BENCHMARK
const _BENCHMARK_TIMER_MAJOR = 1_000;
const _BENCHMARK_TIMER_MINOR = 10_000;
forward OnBenchPerformed(function_name[], result_time);
#if !defined TDW_MAX_DEFAULT_ITERATIONS
#define TDW_MAX_DEFAULT_ITERATIONS (10000)
#endif
#include "tdw_debug"
#define u@$ 0]);
#define __:%0$ u@(u[_:%0 u@$
#define benchmark%1(%2) bm@r:%1(%2)
#define bm@r: __:bm@r0:bm@r1:$
#define bm@r0:bm@r1:%8$%1[%3]%0(%2) %8$forward public bmf@%1();public bmf@%1()for(new bm@l:bmv@current,%2;_:bmv@current<(%3);++bmv@current)
#define bm@r1:%8$%1(%2) %8$forward public bmf@%1(bmv@it);public bmf@%1(bmv@it)for(new bm@l:bmv@current,%2;_:bmv@current<bmv@it;++bmv@current)
#define bmf@%0\32;%1(%2) bmf@%0%1(%2)
#define bm@l:%1,; %1;
#define sbenchmark:%1(%3) \
for(new %1@nonei=1,%1@nonet,%1@nonett,%1@iterMi=0,%1@iterMj=_:C:0,%3;%1@nonei--!=0;printf(#%1": %dms [%dx%d]",%1@nonett,\
_BENCHMARK_TIMER_MAJOR,_BENCHMARK_TIMER_MINOR))\
for(%1@iterMj=0;%1@iterMj<(_BENCHMARK_TIMER_MAJOR);%1@nonett+=GetTickCount()-%1@nonet,++%1@iterMj)\
for(%1@iterMi=0,%1@nonet=GetTickCount();%1@iterMi<(_BENCHMARK_TIMER_MINOR);++%1@iterMi)
#define sfbenchmark:%1(%3) \
for(new %1@nonei=1,%1@nonet=GetTickCount(),%1@iterMj=_:C:0,%3;%1@nonei--!=0;printf(#%1": %dms [%d]",GetTickCount()-%1@nonet,\
_BENCHMARK_TIMER_MAJOR))\
for(%1@iterMj=0;%1@iterMj<(_BENCHMARK_TIMER_MAJOR);++%1@iterMj)
#define C:%1,; %1;
// -----------------------------------------------------------------------------
// Vars
// -----------------------------------------------------------------------------
static stock
TDW_g_sOffset@Dat,
TDW_g_sOffset@PFT,
TDW_g_sOffset@NFT,
TDW_g_sNumberOfPublics,
TDW_g_sOnBenchPerformed[] = !"OnBenchPerformed",
TDW_g_sCallbacks,
TDW_g_sJITStatus[2][] = {"not interpreted", "JIT-interpreted"};
// -----------------------------------------------------------------------------
// Internal
// -----------------------------------------------------------------------------
static stock
benchmark_init()
{
new
addr;
const
AMX_HDR_OFFSET_PFT = 32,
AMX_HDR_OFFSET_NFT = 36;
#emit lctrl 1
#emit neg
#emit stor.pri TDW_g_sOffset@Dat
#emit add.c AMX_HDR_OFFSET_PFT
#emit stor.s.pri addr
#emit lref.s.pri addr
#emit load.s.alt addr
#emit add
#emit const.alt AMX_HDR_OFFSET_PFT
#emit sub
#emit stor.pri TDW_g_sOffset@PFT
#emit load.pri TDW_g_sOffset@Dat
#emit add.c AMX_HDR_OFFSET_NFT
#emit stor.s.pri addr
#emit lref.s.pri addr
#emit load.s.alt addr
#emit add
#emit const.alt AMX_HDR_OFFSET_NFT
#emit sub
#emit stor.pri TDW_g_sOffset@NFT
#emit load.alt TDW_g_sOffset@PFT
#emit sub
#emit shr.c.pri 3
#emit stor.pri TDW_g_sNumberOfPublics
return 0;
}
stock
benchmark_readString(dest[], address)
{
new
cur_char,
i;
do {
#emit lref.s.pri address
#emit const.alt 0xff
#emit and
#emit stor.s.pri cur_char
dest[i++] = cur_char;
#emit inc.s address
} while (cur_char != '\0');
return
dest[i] = '\0',
0;
}
#if defined FILTERSCRIPT
public
OnFilterScriptInit()
{
new is_jit;
#emit lctrl 7
#emit stor.s.pri is_jit
printf("Status: %s", TDW_g_sJITStatus[is_jit]);
if (funcidx(TDW_g_sOnBenchPerformed) != -1) {
TDW_g_sCallbacks |= 1;
}
return
#if defined TDWBM@OnFilterScriptInit
TDWBM@OnFilterScriptInit();
#else
1;
#endif
}
#if defined _ALS_OnFilterScriptInit
#undef OnFilterScriptInit
#else
#define _ALS_OnFilterScriptInit
#endif
#define OnFilterScriptInit TDWBM@OnFilterScriptInit
#if defined TDWBM@OnFilterScriptInit
forward TDWBM@OnFilterScriptInit();
#endif
#else
public
OnGameModeInit()
{
new is_jit;
#emit lctrl 7
#emit stor.s.pri is_jit
printf("Status: %s", TDW_g_sJITStatus[is_jit]);
if (funcidx(TDW_g_sOnBenchPerformed) != -1) {
TDW_g_sCallbacks |= 1;
}
return
#if defined TDWBM@OnGameModeInit
TDWBM@OnGameModeInit();
#else
1;
#endif
}
#if defined _ALS_OnGameModeInit
#undef OnGameModeInit
#else
#define _ALS_OnGameModeInit
#endif
#define OnGameModeInit TDWBM@OnGameModeInit
#if defined TDWBM@OnGameModeInit
forward TDWBM@OnGameModeInit();
#endif
#endif
// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------
stock
benchmark_run()
{
if (TDW_g_sOffset@Dat == 0) {
benchmark_init();
}
const
TDW_MAX_FUNCTION_NAME = 32,
prefix = ('b') | ('m' << 8) | ('f' << 16) | ('@' << 24), // bmf@
TDW_MAX_DEFAULT_ITERATIONS_PRE = TDW_MAX_DEFAULT_ITERATIONS;
static const
message[] = "[benchmark]: \"%s\" took %dms to execute.";
new
idx,
addr,
tmp,
function_name[TDW_MAX_FUNCTION_NAME],
time@start,
time@result;
while (idx < TDW_g_sNumberOfPublics) {
addr = (idx << 3) + TDW_g_sOffset@PFT + 4;
#emit lref.s.pri addr
#emit load.alt TDW_g_sOffset@Dat
#emit add
#emit stor.s.pri addr
#emit lref.s.pri addr
#emit stor.s.pri tmp
if (tmp == prefix) {
#emit load.s.pri addr
#emit add.c 4
#emit stor.s.pri addr
benchmark_readString(function_name, addr);
#emit load.pri TDW_g_sOffset@PFT
#emit load.s.alt idx
#emit shl.c.alt 3
#emit add
#emit stor.s.pri addr
#emit lref.s.pri addr
#emit stor.s.pri addr
time@start = GetTickCount();
#emit push.c TDW_MAX_DEFAULT_ITERATIONS_PRE
#emit push.c 4
#emit lctrl 6
#emit add.c 0x1C
#emit push.pri
#emit load.s.pri addr
#emit sctrl 6
time@result = GetTickCount() - time@start;
p:0(message, function_name, time@result);
if (TDW_g_sCallbacks & 1) {
CallLocalFunction(TDW_g_sOnBenchPerformed, !"si",
function_name, time@result
);
}
}
++idx;
}
return 0;
}