Home | History | Annotate | Download | only in suite
      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 
     13 from capstone import *
     14 
     15 from time import time
     16 from random import randint
     17 import sys
     18 
     19 
     20 # file providing code to disassemble
     21 FILE = '/usr/bin/python'
     22 
     23 
     24 all_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         )
     40 
     41 
     42 # for debugging
     43 def to_hex(s):
     44     return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK
     45 
     46 def get_code(f, size):
     47     code = f.read(size)
     48     if len(code) != size:  # reached end-of-file?
     49         # then reset file position to begin-of-file
     50         f.seek(0)
     51         code = f.read(size)
     52 
     53     return code
     54 
     55 
     56 def cs(md, code):
     57     insns = md.disasm(code, 0)
     58     # uncomment below line to speed up this function 200 times!
     59     # return
     60     for i in insns:
     61         if i.address == 0x100000:
     62             print i
     63 
     64 
     65 def cs_lite(md, code):
     66     insns = md.disasm_lite(code, 0)
     67     for (addr, size, mnem, ops) in insns:
     68         if addr == 0x100000:
     69             print i
     70 
     71 
     72 cfile = open(FILE)
     73 
     74 for (arch, mode, comment, syntax) in all_tests:
     75     try:
     76         request = sys.argv[1]
     77         if not request in comment.lower():
     78             continue
     79     except:
     80         pass
     81 
     82     print("Platform: %s" %comment)
     83 
     84     try:
     85         md = Cs(arch, mode)
     86         #md.detail = True
     87 
     88         if syntax != 0:
     89             md.syntax = syntax
     90 
     91         # warm up few times
     92         cfile.seek(0)
     93         for i in xrange(3):
     94             code = get_code(cfile, 128)
     95             #print to_hex(code)
     96             #print
     97             cs(md, code)
     98 
     99         # start real benchmark
    100         c_t = 0
    101         for i in xrange(50000):
    102             code = get_code(cfile, 128)
    103             #print to_hex(code)
    104             #print
    105 
    106             t1 = time()
    107             cs(md, code)
    108             c_t += time() - t1
    109 
    110         print "Benchmark - full obj:", c_t, "seconds"
    111         print
    112 
    113         cfile.seek(0)
    114         c_t = 0
    115         for i in xrange(50000):
    116             code = get_code(cfile, 128)
    117             #print to_hex(code)
    118             #print
    119 
    120             t1 = time()
    121             cs_lite(md, code)
    122             c_t += time() - t1
    123 
    124         print "Benchmark - lite:", c_t, "seconds"
    125         print
    126     except CsError as e:
    127         print("ERROR: %s" %e)
    128