// File: /workspace/github_cpp_makefile_star2_O1/yangwanyuan[P]HLKVDS/tools/Benchmark.cc
// Function: Bench_Insert(hlkvds::KVDS*, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, int, LatMgr*)
double Bench_Insert(KVDS *db, int record_num, vector<string> &key_list,
                    int thread_num, LatMgr *total_lat_mgr = NULL) {
    cout << "Start Benchmark Test: Insert , record_num = " << record_num << ", Please wait ..." << endl;

    string data = string(VALUE_SIZE, 'v');

    thread_arg arglist[thread_num];
    pthread_t pidlist[thread_num];
    for (int i = 0; i < thread_num; i++) {
        int start = (record_num / thread_num) * i;
        int end = (record_num / thread_num) * (i + 1) - 1;
        arglist[i].db = db;
        arglist[i].key_start = start;
        arglist[i].key_end = end;
        arglist[i].key_list = &key_list;
        arglist[i].data = &data;
        arglist[i].latency = new uint64_t[end - start + 1];
    }

    KVTime tv_start;
    for (int i = 0; i < thread_num; i++) {
        pthread_create(&pidlist[i], NULL, fun_insert, &arglist[i]);
    }
    //db->printDbStates();
    for (int i=0; i<thread_num; i++) {
        pthread_join(pidlist[i], NULL);
    }

    KVTime tv_end;
    double diff_time = (tv_end - tv_start) / 1000000.0;

    LatMgr lat_mgr;
    for (int i = 0; i < thread_num; i++) {
        lat_mgr.Push_back_batch(arglist[i].latency, record_num/thread_num);

        //if overwrite, save to total_lat_mgr
        if (total_lat_mgr) {
            total_lat_mgr->Push_back_batch(arglist[i].latency, record_num/thread_num);
        }

        delete[] arglist[i].latency;
    }

    Lat_Stats lat_stats;
    lat_mgr.GetStatistics(lat_stats);

    double iops = record_num / diff_time;

    uint64_t total_size = (uint64_t)record_num * (KEY_SIZE + VALUE_SIZE);
    double throughput = total_size / (diff_time * 1024 * 1024);

    uint64_t avg = lat_mgr.GetAvg();
    uint64_t min = lat_mgr.GetMin();
    uint64_t max = lat_mgr.GetMax();

    cout << "Insert Bench Finish. Total time: " << diff_time << "s" <<endl;
    cout << "Performance Report     :   IOPS = " <<  iops << ", Average Latency = "
            << avg << "us, Throughput = " << throughput << "MB/s." << endl;
    cout << "Latency Report(usec)   :   Min = " << min << ", Max = "
            << max << ", Med = " << lat_stats.med << "." << endl;
    cout << "Latency Statistic(usec):   P50 = " << lat_stats.P_50 << ", P75 = "
            << lat_stats.P_75 << ", P99 = " << lat_stats.P_99 << ", P999 = "
            << lat_stats.P_999 << ", P9999 = " << lat_stats.P_9999 << endl;

    return diff_time;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r8, -0x38(%rbp)
movl %ecx, %r15d
movq %rdx, -0x60(%rbp)
movl %esi, %r14d
movq %rdi, -0x88(%rbp)
movq 0x2cfb9(%rip), %rbx     # 0x36fd0
leaq 0x1a186(%rip), %rsi     # 0x241a4
movl $0x2c, %edx
movq %rbx, %rdi
callq 0x64a0
movq %rbx, %rdi
movl %r14d, -0x2c(%rbp)
movl %r14d, %esi
callq 0x6710
movq %rax, %rbx
leaq 0x1a0e3(%rip), %rsi     # 0x24127
movl $0x11, %edx
movq %rax, %rdi
callq 0x64a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x6300
leaq -0xb8(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xc8(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movl $0x76, %edx
callq 0x64c0
movq %rsp, -0x78(%rbp)
movl %r15d, %ecx
movq %rsp, %r14
leaq (%rcx,%rcx,4), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %rdx
movq %rcx, -0x58(%rbp)
leaq 0xf(,%rcx,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, -0x70(%rbp)
movq %rdx, %rsp
movl %r15d, -0x30(%rbp)
testl %r15d, %r15d
jle 0xa16a
movl -0x2c(%rbp), %eax
cltd
idivl -0x30(%rbp)
movl %eax, -0x64(%rbp)
movslq %eax, %rbx
leaq (,%rbx,8), %rax
testl %ebx, %ebx
movq $-0x1, %rcx
cmovnsq %rax, %rcx
movq %rcx, -0x80(%rbp)
decl %ebx
movq -0x58(%rbp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,4), %r13
xorl %r15d, %r15d
xorl %r12d, %r12d
movq -0x88(%rbp), %rax
movq %rax, (%r14,%r15)
movl %r12d, 0x8(%r14,%r15)
leal (%rbx,%r12), %eax
movl %eax, 0xc(%r14,%r15)
movq -0x60(%rbp), %rax
movq %rax, 0x10(%r14,%r15)
leaq -0xc8(%rbp), %rax
movq %rax, 0x18(%r14,%r15)
movq -0x80(%rbp), %rdi
callq 0x6040
movq %rax, 0x20(%r14,%r15)
addl -0x64(%rbp), %r12d
addq $0x28, %r15
cmpq %r15, %r13
jne 0xa121
leaq -0xa8(%rbp), %rdi
callq 0x22ee8
cmpl $0x0, -0x30(%rbp)
jle 0xa1ab
leaq -0x369(%rip), %rbx      # 0x9e1a
movq -0x58(%rbp), %r13
movq -0x70(%rbp), %r15
movq %r14, %r12
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x6780
addq $0x28, %r12
addq $0x8, %r15
decq %r13
jne 0xa18e
movl -0x30(%rbp), %r15d
testl %r15d, %r15d
movq -0x58(%rbp), %r12
movq -0x70(%rbp), %r13
jle 0xa1d2
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
callq 0x6420
incq %rbx
cmpq %rbx, %r12
jne 0xa1be
leaq -0x98(%rbp), %rdi
callq 0x22ee8
leaq -0x98(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x22f54
movq %rax, -0x60(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
testl %r15d, %r15d
jle 0xa261
movl -0x2c(%rbp), %eax
cltd
idivl %r15d
movl %eax, %ebx
shlq $0x3, %r12
leaq (%r12,%r12,4), %r13
xorl %r12d, %r12d
leaq -0x50(%rbp), %r15
movq 0x20(%r14,%r12), %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0xc376
cmpq $0x0, -0x38(%rbp)
je 0xa249
movq 0x20(%r14,%r12), %rsi
movq -0x38(%rbp), %rdi
movl %ebx, %edx
callq 0xc376
movq 0x20(%r14,%r12), %rdi
testq %rdi, %rdi
je 0xa258
callq 0x6530
addq $0x28, %r12
cmpq %r12, %r13
jne 0xa223
leaq -0x50(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xc3bc
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpq %rcx, %rsi
je 0xa28f
movq %rsi, %rdx
addq (%rdx), %rax
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0xa283
movq %rcx, %rdi
subq %rsi, %rdi
sete %r8b
sarq $0x3, %rdi
xorl %edx, %edx
divq %rdi
movq %rax, %r13
leaq 0x8(%rsi), %rax
cmpq %rcx, %rax
sete %dil
orb %r8b, %dil
movq %rsi, %rdx
jne 0xa2d8
movq %rax, %r8
movq %rsi, %r9
movq (%r8), %r10
movq %r8, %rdx
cmpq (%r9), %r10
jb 0xa2cc
movq %r9, %rdx
addq $0x8, %r8
movq %rdx, %r9
cmpq %rcx, %r8
jne 0xa2be
testb %dil, %dil
je 0xa2e2
movq %rsi, %rdi
jmp 0xa2fc
movq (%rsi), %r8
movq %rax, %rdi
cmpq (%rax), %r8
jb 0xa2f0
movq %rsi, %rdi
addq $0x8, %rax
movq %rdi, %rsi
cmpq %rcx, %rax
jne 0xa2e2
movq (%rdx), %r15
movq (%rdi), %r14
movq 0x2ccc7(%rip), %rdi     # 0x36fd0
leaq 0x19ec1(%rip), %rsi     # 0x241d1
movl $0x21, %edx
callq 0x64a0
xorps %xmm0, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm0
divsd 0x19cdd(%rip), %xmm0    # 0x24008
movq 0x2cc9e(%rip), %rdi     # 0x36fd0
movsd %xmm0, -0x38(%rbp)
callq 0x6680
movq %rax, %rbx
leaq 0x1a06a(%rip), %rsi     # 0x243b0
movl $0x1, %edx
movq %rax, %rdi
callq 0x64a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x6300
movq 0x2cc4f(%rip), %rdi     # 0x36fd0
leaq 0x19e6b(%rip), %rsi     # 0x241f3
movl $0x22, %edx
callq 0x64a0
xorps %xmm0, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movq 0x2cc2a(%rip), %rdi     # 0x36fd0
callq 0x6680
movq %rax, %rbx
leaq 0x19e61(%rip), %rsi     # 0x24216
movl $0x14, %edx
movq %rax, %rdi
callq 0x64a0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x62f0
movq %rax, %rbx
leaq 0x19e54(%rip), %rsi     # 0x2422b
movl $0x11, %edx
movq %rax, %rdi
callq 0x64a0
movslq -0x2c(%rbp), %rax
imulq $0x100a, %rax, %rax     # imm = 0x100A
movq %rax, %xmm1
punpckldq 0x19c24(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x19c2c(%rip), %xmm1    # 0x24030
movsd 0x19c04(%rip), %xmm2    # 0x24010
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
movsd -0x38(%rbp), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm0
mulsd %xmm2, %xmm3
divsd %xmm3, %xmm0
movq %rbx, %rdi
callq 0x6680
movq %rax, %rbx
leaq 0x19e02(%rip), %rsi     # 0x2423d
movl $0x5, %edx
movq %rax, %rdi
callq 0x64a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x6300
movq 0x2cb5a(%rip), %rdi     # 0x36fd0
leaq 0x19dc6(%rip), %rsi     # 0x24243
movl $0x21, %edx
callq 0x64a0
movq 0x2cb42(%rip), %rdi     # 0x36fd0
movq %r15, %rsi
callq 0x62f0
movq %rax, %rbx
leaq 0x19dc5(%rip), %rsi     # 0x24265
movl $0x8, %edx
movq %rax, %rdi
callq 0x64a0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x62f0
movq %rax, %rbx
leaq 0x19dac(%rip), %rsi     # 0x2426e
movl $0x8, %edx
movq %rax, %rdi
callq 0x64a0
movq -0xf8(%rbp), %rsi
movq %rbx, %rdi
callq 0x62f0
movq %rax, %rbx
leaq 0x19c4f(%rip), %rsi     # 0x24137
movl $0x1, %edx
movq %rax, %rdi
callq 0x64a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x6300
movq 0x2caad(%rip), %rdi     # 0x36fd0
leaq 0x19d4d(%rip), %rsi     # 0x24277
movl $0x21, %edx
callq 0x64a0
movq -0xf0(%rbp), %rsi
movq 0x2ca8e(%rip), %rdi     # 0x36fd0
callq 0x62f0
movq %rax, %rbx
leaq 0x19d48(%rip), %rsi     # 0x24299
movl $0x8, %edx
movq %rax, %rdi
callq 0x64a0
movq -0xe8(%rbp), %rsi
movq %rbx, %rdi
callq 0x62f0
movq %rax, %rbx
leaq 0x19d2b(%rip), %rsi     # 0x242a2
movl $0x8, %edx
movq %rax, %rdi
callq 0x64a0
movq -0xe0(%rbp), %rsi
movq %rbx, %rdi
callq 0x62f0
movq %rax, %rbx
leaq 0x19d0e(%rip), %rsi     # 0x242ab
movl $0x9, %edx
movq %rax, %rdi
callq 0x64a0
movq -0xd8(%rbp), %rsi
movq %rbx, %rdi
callq 0x62f0
movq %rax, %rbx
leaq 0x19cf2(%rip), %rsi     # 0x242b5
movl $0xa, %edx
movq %rax, %rdi
callq 0x64a0
movq -0xd0(%rbp), %rsi
movq %rbx, %rdi
callq 0x62f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x6300
movq -0x50(%rbp), %rdi
cmpq %rdi, -0x48(%rbp)
je 0xa617
movq %rdi, -0x48(%rbp)
testq %rdi, %rdi
je 0xa621
callq 0x6440
leaq -0x98(%rbp), %rdi
callq 0x22f6e
leaq -0xa8(%rbp), %rdi
callq 0x22f6e
movq -0x78(%rbp), %rsp
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xa655
callq 0x6440
movsd -0x38(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa680
movq %rax, %rbx
jmp 0xa69b
jmp 0xa674
jmp 0xa679
movq %rax, %rbx
jmp 0xa6a7
movq %rax, %rbx
jmp 0xa6b3
jmp 0xa680
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %rdi, -0x48(%rbp)
je 0xa691
movq %rdi, -0x48(%rbp)
testq %rdi, %rdi
je 0xa69b
callq 0x6440
leaq -0x98(%rbp), %rdi
callq 0x22f6e
leaq -0xa8(%rbp), %rdi
callq 0x22f6e
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xa6cb
callq 0x6440
movq %rbx, %rdi
callq 0x6720