add brenchmark

This commit is contained in:
kabeor 2022-01-04 22:34:15 +08:00
parent 26af9f1d36
commit 04ff181151
4 changed files with 152 additions and 0 deletions

3
.gitignore vendored
View File

@ -129,3 +129,6 @@ cstool/cstool
# android
android-ndk-*
# benchmark/
benchmark/cs-benchmark*

7
benchmark/Makefile Normal file
View File

@ -0,0 +1,7 @@
# Modified from https://github.com/athre0z/disas-bench
all:
cc main.c -O3 -o cs-benchmark -I../include ../build/libcapstone.a
clean:
rm -f cs-benchmark* *.o

60
benchmark/load_bin.inc Normal file
View File

@ -0,0 +1,60 @@
// From https://github.com/athre0z/disas-bench
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
int read_file_data(uint8_t** buf, const char* filename, size_t file_offset, size_t bin_len)
{
FILE* f = fopen(filename, "rb");
if (!f) return 0;
// Seek to code section
if (fseek(f, (long)file_offset, SEEK_SET))
{
fclose(f);
return 0;
}
uint8_t* code = malloc(bin_len);
if (!code)
{
fclose(f);
return 0;
}
if (fread(code, 1, bin_len, f) != bin_len)
{
fclose(f);
free(code);
return 0;
}
*buf = code;
return 1;
}
int read_file(int argc, char *argv[], uint8_t **buf, size_t *bin_len, size_t *loop_count)
{
if (argc != 5)
{
fputs("Expected args: <loop-count> <code-offset> <code-len> <filename>\n", stderr);
return 0;
}
*loop_count = (size_t)strtoull(argv[1], NULL, 0);
size_t file_offset = (size_t)strtoull(argv[2], NULL, 0);
*bin_len = (size_t)strtoull(argv[3], NULL, 0);
const char *filename = argv[4];
if (!read_file_data(buf, filename, file_offset, *bin_len))
{
fprintf(stderr, "Couldn't read `%s`\n", filename);
return 0;
}
return 1;
}

82
benchmark/main.c Normal file
View File

@ -0,0 +1,82 @@
// Modified from https://github.com/athre0z/disas-bench
#include "./load_bin.inc"
#include <capstone/capstone.h>
int main(int argc, char* argv[])
{
csh handle = 0;
cs_insn *insn = NULL;
int ret = 0;
const uint8_t *code_iter = NULL;
size_t code_len_iter = 0;
uint64_t ip = 0;
size_t num_valid_insns = 0;
size_t num_bad_insn = 0;
size_t round;
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
{
fputs("Unable to create Capstone handle\n", stderr);
ret = 1;
goto leave;
}
uint8_t *code = NULL;
size_t code_len = 0, loop_count = 0;
if (!read_file(argc, argv, &code, &code_len, &loop_count))
{
ret = 1;
goto leave;
}
insn = cs_malloc(handle);
if (!insn)
{
fputs("Failed to allocate memory\n", stderr);
ret = 1;
goto leave;
}
clock_t start_time = clock();
for (round = 0; round < loop_count; ++round)
{
code_iter = code;
code_len_iter = code_len;
while (code_len_iter > 0)
{
if (!cs_disasm_iter(
handle,
&code_iter,
&code_len_iter,
&ip,
insn
))
{
++code_iter;
--code_len_iter;
++num_bad_insn;
}
else
{
++num_valid_insns;
}
}
}
clock_t end_time = clock();
printf(
"Disassembled %zu instructions (%zu valid, %zu bad), %.2f ms\n",
num_valid_insns + num_bad_insn,
num_valid_insns,
num_bad_insn,
(double)(end_time - start_time) * 1000.0 / CLOCKS_PER_SEC
);
leave:
if (insn) cs_free(insn, 1);
if (handle) cs_close(&handle);
if (code) free(code);
return ret;
}