xref: /aosp_15_r20/external/capstone/suite/benchmark.py (revision 9a0e4156d50a75a99ec4f1653a0e9602a5d45c18)
1#!/usr/bin/python
2
3# Simple benchmark for Capstone by disassembling random code. By Nguyen Anh Quynh, 2014
4# Syntax:
5# ./suite/benchmark.py          --> Benchmark all archs
6# ./suite/benchmark.py x86      --> Benchmark all X86 (all 16bit, 32bit, 64bit)
7# ./suite/benchmark.py x86-32   --> Benchmark X86-32 arch only
8# ./suite/benchmark.py arm      --> Benchmark all ARM (arm, thumb)
9# ./suite/benchmark.py aarch64  --> Benchmark ARM-64
10# ./suite/benchmark.py mips     --> Benchmark all Mips (32bit, 64bit)
11# ./suite/benchmark.py ppc      --> Benchmark PPC
12
13from capstone import *
14
15from time import time
16from random import randint
17import sys
18
19
20# file providing code to disassemble
21FILE = '/usr/bin/python'
22
23
24all_tests = (
25        (CS_ARCH_X86, CS_MODE_16, "X86-16 (Intel syntax)", 0),
26        (CS_ARCH_X86, CS_MODE_32, "X86-32 (ATT syntax)", CS_OPT_SYNTAX_ATT),
27        (CS_ARCH_X86, CS_MODE_32, "X86-32 (Intel syntax)", 0),
28        (CS_ARCH_X86, CS_MODE_64, "X86-64 (Intel syntax)", 0),
29        (CS_ARCH_ARM, CS_MODE_ARM, "ARM", 0),
30        (CS_ARCH_ARM, CS_MODE_THUMB, "THUMB (ARM)", 0),
31        (CS_ARCH_MIPS, CS_MODE_MIPS32 + CS_MODE_BIG_ENDIAN, "MIPS-32 (Big-endian)", 0),
32        (CS_ARCH_MIPS, CS_MODE_MIPS64 + CS_MODE_LITTLE_ENDIAN, "MIPS-64-EL (Little-endian)", 0),
33        (CS_ARCH_ARM64, CS_MODE_ARM, "ARM-64 (AArch64)", 0),
34        (CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, "PPC", 0),
35        (CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, "PPC, print register with number only", CS_OPT_SYNTAX_NOREGNAME),
36        (CS_ARCH_SPARC, CS_MODE_BIG_ENDIAN, "Sparc", 0),
37        (CS_ARCH_SYSZ, 0, "SystemZ", 0),
38        (CS_ARCH_XCORE, 0, "XCore", 0),
39        (CS_ARCH_M68K, 0, "M68K", 0),
40        )
41
42
43# for debugging
44def to_hex(s):
45    return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK
46
47def get_code(f, size):
48    code = f.read(size)
49    if len(code) != size:  # reached end-of-file?
50        # then reset file position to begin-of-file
51        f.seek(0)
52        code = f.read(size)
53
54    return code
55
56
57def cs(md, code):
58    insns = md.disasm(code, 0)
59    # uncomment below line to speed up this function 200 times!
60    # return
61    for i in insns:
62        if i.address == 0x100000:
63            print i
64
65
66def cs_lite(md, code):
67    insns = md.disasm_lite(code, 0)
68    for (addr, size, mnem, ops) in insns:
69        if addr == 0x100000:
70            print i
71
72
73cfile = open(FILE)
74
75for (arch, mode, comment, syntax) in all_tests:
76    try:
77        request = sys.argv[1]
78        if not request in comment.lower():
79            continue
80    except:
81        pass
82
83    print("Platform: %s" %comment)
84
85    try:
86        md = Cs(arch, mode)
87        #md.detail = True
88
89        if syntax != 0:
90            md.syntax = syntax
91
92        # warm up few times
93        cfile.seek(0)
94        for i in xrange(3):
95            code = get_code(cfile, 128)
96            #print to_hex(code)
97            #print
98            cs(md, code)
99
100        # start real benchmark
101        c_t = 0
102        for i in xrange(50000):
103            code = get_code(cfile, 128)
104            #print to_hex(code)
105            #print
106
107            t1 = time()
108            cs(md, code)
109            c_t += time() - t1
110
111        print "Benchmark - full obj:", c_t, "seconds"
112        print
113
114        cfile.seek(0)
115        c_t = 0
116        for i in xrange(50000):
117            code = get_code(cfile, 128)
118            #print to_hex(code)
119            #print
120
121            t1 = time()
122            cs_lite(md, code)
123            c_t += time() - t1
124
125        print "Benchmark - lite:", c_t, "seconds"
126        print
127    except CsError as e:
128        print("ERROR: %s" %e)
129