forked from dashingsoft/pyarmor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
executable file
·382 lines (288 loc) · 9.96 KB
/
benchmark.py
File metadata and controls
executable file
·382 lines (288 loc) · 9.96 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
#############################################################
# #
# Copyright @ 2013 - 2017 Dashingsoft corp. #
# All rights reserved. #
# #
# pyarmor #
# #
# Version: 1.7.0 - #
# #
#############################################################
#
#
# @File: benchmark.py
#
# @Author: Jondy Zhao(jondy.zhao@gmail.com)
#
# @Create Date: 2017/11/21
#
# @Description:
#
# Check performance of pyarmor.
#
import logging
import os
import shutil
import sys
import subprocess
import time
from ctypes import c_int, c_void_p, py_object, pythonapi, PYFUNCTYPE
import pytransform
OBF_MODULE_MODE = 'none', 'des', 'aes'
OBF_CODE_MODE = 'none', 'fast', 'aes', 'wrap'
PYARMOR_PATH = os.path.dirname(__file__)
PYARMOR = 'pyarmor.py'
def make_test_script(filename):
lines = [
'def empty():',
' return 0',
'',
'def call_1k_function(n):',
' for i in range(n):',
' one_thousand()',
'',
'def call_10k_function(n):',
' for i in range(n):',
' ten_thousand()',
'',
'def one_thousand():',
' if True:',
' i = 0',
]
lines.extend([' i += 1'] * 100)
lines.append('\n return 1000\n')
lines.extend(['def ten_thousand():',
' if True:',
' i = 0'])
lines.extend([' i += 1'] * 1000)
lines.append('\n return 10000\n')
with open(filename, 'wb') as f:
f.write('\n'.join(lines).encode())
def call_pyarmor(args):
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
def obffuscate_scripts(output, filename,
mod_mode, code_mode, wrap_mode, adv_mode):
project = os.path.join(output, 'project')
if os.path.exists(project):
shutil.rmtree(project)
args = [sys.executable, PYARMOR, 'init', '--src', output,
'--entry', filename, project]
call_pyarmor(args)
args = [sys.executable, PYARMOR, 'config',
'--manifest', 'include %s' % filename,
'--obf-mod', mod_mode,
'--obf-code', code_mode,
'--wrap-mode', wrap_mode,
'--advanced', adv_mode,
'--restrict-mode', '0',
'--package-runtime', '0',
project]
call_pyarmor(args)
args = [sys.executable, PYARMOR, 'build', '-B', project]
call_pyarmor(args)
for s in os.listdir(os.path.join(project, 'dist')):
shutil.copy(os.path.join(project, 'dist', s), output)
def metricmethod(func):
if not hasattr(time, 'process_time'):
time.process_time = time.clock
def wrap(*args, **kwargs):
t1 = time.process_time()
result = func(*args, **kwargs)
t2 = time.process_time()
logging.info('%-50s: %10.6f ms', func.__name__, (t2 - t1) * 1000)
return result
return wrap
@metricmethod
def verify_license(m):
try:
prototype = PYFUNCTYPE(py_object)
dlfunc = prototype(('get_registration_code', m))
code = dlfunc()
except Exception:
logging.warning('Verify license failed')
code = ''
return code
@metricmethod
def init_pytransform(m):
major, minor = sys.version_info[0:2]
# Python2.5 no sys.maxsize but sys.maxint
# bitness = 64 if sys.maxsize > 2**32 else 32
prototype = PYFUNCTYPE(c_int, c_int, c_int, c_void_p)
init_module = prototype(('init_module', m))
init_module(major, minor, pythonapi._handle)
prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int)
init_runtime = prototype(('init_runtime', m))
init_runtime(0, 0, 0, 0)
@metricmethod
def load_pytransform():
return pytransform._load_library(PYARMOR_PATH, is_runtime=1)
@metricmethod
def total_extra_init_time():
m = load_pytransform()
init_pytransform(m)
verify_license(m)
@metricmethod
def import_first_no_obfuscated_module(name):
return __import__(name)
@metricmethod
def import_first_obfuscated_module(name):
return __import__(name)
@metricmethod
def re_import_no_obfuscated_module(name):
return __import__(name)
@metricmethod
def re_import_obfuscated_module(name):
return __import__(name)
@metricmethod
def run_empty_obfuscated_code_object(foo):
return foo.empty()
@metricmethod
def run_obfuscated_1k_bytecode(foo):
return foo.one_thousand()
@metricmethod
def run_obfuscated_10k_bytecode(foo):
return foo.ten_thousand()
@metricmethod
def run_empty_no_obfuscated_code_object(foo):
return foo.empty()
@metricmethod
def run_no_obfuscated_1k_bytecode(foo):
return foo.one_thousand()
@metricmethod
def run_no_obfuscated_10k_bytecode(foo):
return foo.ten_thousand()
@metricmethod
def import_many_obfuscated_modules(name, n=100):
for i in range(n):
__import__(name % i)
@metricmethod
def import_many_no_obfuscated_modules(name, n=100):
for i in range(n):
__import__(name % i)
@metricmethod
def call_1000_no_obfuscated_1k_bytecode(foo):
return foo.call_1k_function(1000)
@metricmethod
def call_1000_obfuscated_1k_bytecode(foo):
return foo.call_1k_function(1000)
@metricmethod
def call_1000_no_obfuscated_10k_bytecode(foo):
return foo.call_10k_function(1000)
@metricmethod
def call_1000_obfuscated_10k_bytecode(foo):
return foo.call_10k_function(1000)
@metricmethod
def call_10000_no_obfuscated_1k_bytecode(foo):
return foo.call_1k_function(10000)
@metricmethod
def call_10000_obfuscated_1k_bytecode(foo):
return foo.call_1k_function(10000)
@metricmethod
def call_10000_no_obfuscated_10k_bytecode(foo):
return foo.call_10k_function(10000)
@metricmethod
def call_10000_obfuscated_10k_bytecode(foo):
return foo.call_10k_function(10000)
def main():
if not os.path.exists('benchmark.py'):
logging.warning('Please change current path to %s', PYARMOR_PATH)
return
output = '.benchtest'
name = 'bfoo'
filename = os.path.join(output, name + '.py')
obname = 'obfoo'
obfilename = os.path.join(output, obname + '.py')
if len(sys.argv) > 1 and 'bootstrap'.startswith(sys.argv[1]):
if len(sys.argv) < 6:
sys.argv.extend(['1', '1', '1', '0'])
obf_mod, obf_code, wrap_mode, adv_mode = sys.argv[2:6]
if os.path.exists(output) and output.endswith('.benchtest'):
logging.info('Clean output path: %s', output)
shutil.rmtree(output)
logging.info('Create output path: %s', output)
os.makedirs(output)
logging.info('Generate test script %s ...', filename)
make_test_script(filename)
logging.info('Obffuscate test script ...')
shutil.copy(filename, obfilename)
obffuscate_scripts(output, os.path.basename(obfilename),
obf_mod, obf_code, wrap_mode, adv_mode)
if not os.path.exists(obfilename):
logging.info('Something is wrong to obsfucate the script')
return
logging.info('Generate obffuscated script %s', obfilename)
logging.info('Copy benchmark.py to %s', output)
shutil.copy('benchmark.py', output)
logging.info('')
logging.info('Now change to "%s"', output)
logging.info('Run "%s benchmark.py".', sys.executable)
return
filename = os.path.basename(filename)
if os.path.exists(filename):
logging.info('Test script: %s', filename)
else:
logging.warning('Test script: %s not found', filename)
logging.info('Run "%s benchmark.py bootstrap" first.', sys.executable)
return
obfilename = os.path.basename(obfilename)
if os.path.exists(obfilename):
logging.info('Obfuscated script: %s', obfilename)
else:
logging.warning('Obfuscated script: %s not found', obfilename)
logging.info('Run "%s benchmark.py bootstrap" first.', sys.executable)
return
logging.info('--------------------------------------')
# It doens't work for super mode
# logging.info('')
# total_extra_init_time()
logging.info('')
foo = import_first_no_obfuscated_module(name)
obfoo = import_first_obfuscated_module(obname)
logging.info('')
foo = re_import_no_obfuscated_module(name)
obfoo = re_import_obfuscated_module(obname)
logging.info('')
n = 10
logging.info('--- Import %d modules ---', n)
for i in range(n):
shutil.copy(filename, filename.replace('.py', '_%s.py' % i))
with open(obfilename) as f:
lines = f.readlines()
with open(obfilename.replace('.py', '_%s.py' % i), 'w') as f:
f.write(lines[2] if lines[0].find('pyarmor_runtime') > 0 \
else ''.join(lines))
import_many_no_obfuscated_modules('bfoo_%s', n)
import_many_obfuscated_modules('obfoo_%s', n)
logging.info('')
run_empty_no_obfuscated_code_object(foo)
run_empty_obfuscated_code_object(obfoo)
logging.info('')
run_no_obfuscated_1k_bytecode(foo)
run_obfuscated_1k_bytecode(obfoo)
logging.info('')
run_no_obfuscated_10k_bytecode(foo)
run_obfuscated_10k_bytecode(obfoo)
logging.info('')
call_1000_no_obfuscated_1k_bytecode(foo)
call_1000_obfuscated_1k_bytecode(obfoo)
logging.info('')
call_1000_no_obfuscated_10k_bytecode(foo)
call_1000_obfuscated_10k_bytecode(obfoo)
logging.info('')
call_10000_no_obfuscated_1k_bytecode(foo)
call_10000_obfuscated_1k_bytecode(obfoo)
logging.info('')
call_10000_no_obfuscated_10k_bytecode(foo)
call_10000_obfuscated_10k_bytecode(obfoo)
logging.info('')
logging.info('--------------------------------------')
if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
format='%(message)s',
)
main()