// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %r8, -0x70(%rbp)
movl %ecx, %r15d
movq %rdx, -0x48(%rbp)
movl %esi, %ebx
movq %rdi, %r13
movq 0x2d040(%rip), %rdi     # 0x33fd0
leaq 0x1920f(%rip), %rsi     # 0x201a6
callq 0x6420
movq %rax, %rdi
movl %ebx, -0x2c(%rbp)
movl %ebx, %esi
callq 0x66e0
leaq 0x19179(%rip), %rsi     # 0x20129
movq %rax, %rdi
callq 0x6420
movq %rax, %rdi
callq 0x61c0
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xb0(%rbp), %rdi
pushq $0x76
popq %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x6480
movq %rsp, -0x68(%rbp)
movl %r15d, %eax
movq %rsp, %rcx
imulq $0x28, %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rcx, %rsi
subq %rdx, %rsi
movq %rsi, -0x40(%rbp)
movq %rsi, %rsp
movq %rsp, %rsi
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, -0x38(%rbp)
movq %rsi, %rsp
xorl %ebx, %ebx
testl %r15d, %r15d
movl $0x0, %r14d
cmovgl %r15d, %r14d
negq %rdx
leaq (%rcx,%rdx), %r12
addq $0x20, %r12
cmpq %rbx, %r14
je 0x7097
movl -0x2c(%rbp), %eax
cltd
idivl %r15d
leal 0x1(%rbx), %ecx
imull %eax, %ecx
imull %ebx, %eax
decl %ecx
movq %r13, -0x20(%r12)
movl %eax, -0x18(%r12)
movl %ecx, -0x14(%r12)
movq -0x48(%rbp), %rdx
movq %rdx, -0x10(%r12)
leaq -0xb0(%rbp), %rdx
movq %rdx, -0x8(%r12)
subl %eax, %ecx
pushq $-0x1
popq %rdi
cmpl $-0x1, %ecx
jl 0x7085
movslq %ecx, %rax
leaq 0x8(,%rax,8), %rdi
callq 0x6040
movq %rax, (%r12)
addq $0x28, %r12
incq %rbx
jmp 0x7035
movl %r15d, -0x48(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x1f856
leaq -0x2a9(%rip), %r12      # 0x6e05
movq -0x38(%rbp), %r13
movq -0x40(%rbp), %rbx
movq %r14, %r15
subq $0x1, %r15
jb 0x70d9
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6750
addq $0x28, %rbx
addq $0x8, %r13
jmp 0x70b9
xorl %ebx, %ebx
movq -0x38(%rbp), %r15
cmpq %rbx, %r14
je 0x70f4
movq (%r15,%rbx,8), %rdi
xorl %esi, %esi
callq 0x63f0
incq %rbx
jmp 0x70df
leaq -0x80(%rbp), %rdi
callq 0x1f856
leaq -0x80(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x1f8c2
cvtsi2sd %rax, %xmm0
divsd 0x18eee(%rip), %xmm0    # 0x20008
movsd %xmm0, -0x38(%rbp)
leaq -0x60(%rbp), %r12
andq $0x0, 0x10(%r12)
xorpd %xmm0, %xmm0
movapd %xmm0, (%r12)
imulq $0x28, %r14, %r15
xorl %r13d, %r13d
movq -0x70(%rbp), %r14
cmpq %r13, %r15
je 0x7190
movq -0x40(%rbp), %rax
movq 0x20(%rax,%r13), %rsi
movl -0x2c(%rbp), %eax
cltd
idivl -0x48(%rbp)
movl %eax, %ebx
movq %r12, %rdi
movl %eax, %edx
callq 0x87f8
testq %r14, %r14
je 0x7177
movq -0x40(%rbp), %rax
movq 0x20(%rax,%r13), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x87f8
movq -0x40(%rbp), %rax
movq 0x20(%rax,%r13), %rdi
testq %rdi, %rdi
je 0x718a
callq 0x64f0
addq $0x28, %r13
jmp 0x713e
leaq -0x60(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x882a
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
callq 0x88ee
movq %rax, %r12
movq %rbx, %rdi
callq 0x8916
movq %rax, %r15
leaq -0x60(%rbp), %rdi
callq 0x892c
movq %rax, %r14
movq 0x2ce03(%rip), %rdi     # 0x33fd0
leaq 0x18fff(%rip), %rsi     # 0x201d3
callq 0x6420
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x6650
leaq 0x191c5(%rip), %rsi     # 0x203b2
movq %rax, %rdi
callq 0x6420
movq %rax, %rdi
callq 0x61c0
movq 0x2cdcc(%rip), %rdi     # 0x33fd0
leaq 0x18fea(%rip), %rsi     # 0x201f5
callq 0x6420
cvtsi2sdl -0x2c(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movq %rax, %rdi
callq 0x6650
leaq 0x18fef(%rip), %rsi     # 0x20218
movq %rax, %rdi
callq 0x6420
movq %rax, %rdi
movq %r12, %rsi
callq 0x62c0
leaq 0x18fea(%rip), %rsi     # 0x2022d
movq %rax, %rdi
callq 0x6420
movslq -0x2c(%rbp), %rcx
imulq $0x100a, %rcx, %rcx     # imm = 0x100A
movq %rcx, %xmm1
punpckldq 0x18dbd(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x18dc5(%rip), %xmm1    # 0x20030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x18d91(%rip), %xmm1    # 0x20010
movsd -0x38(%rbp), %xmm2
mulsd %xmm1, %xmm2
mulsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movq %rax, %rdi
callq 0x6650
leaq 0x18fa0(%rip), %rsi     # 0x2023f
movq %rax, %rdi
callq 0x6420
movq %rax, %rdi
callq 0x61c0
movq 0x2cd1a(%rip), %rdi     # 0x33fd0
leaq 0x18f88(%rip), %rsi     # 0x20245
callq 0x6420
movq %rax, %rdi
movq %r15, %rsi
callq 0x62c0
leaq 0x18f93(%rip), %rsi     # 0x20267
movq %rax, %rdi
callq 0x6420
movq %rax, %rdi
movq %r14, %rsi
callq 0x62c0
leaq 0x18f82(%rip), %rsi     # 0x20270
movq %rax, %rdi
callq 0x6420
movq -0xe0(%rbp), %rsi
movq %rax, %rdi
callq 0x62c0
leaq 0x18e2d(%rip), %rsi     # 0x20139
movq %rax, %rdi
callq 0x6420
movq %rax, %rdi
callq 0x61c0
movq 0x2ccad(%rip), %rdi     # 0x33fd0
leaq 0x18f4f(%rip), %rsi     # 0x20279
callq 0x6420
movq -0xd8(%rbp), %rsi
movq %rax, %rdi
callq 0x62c0
leaq 0x18f56(%rip), %rsi     # 0x2029b
movq %rax, %rdi
callq 0x6420
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
callq 0x62c0
leaq 0x18f41(%rip), %rsi     # 0x202a4
movq %rax, %rdi
callq 0x6420
movq -0xc8(%rbp), %rsi
movq %rax, %rdi
callq 0x62c0
leaq 0x18f2c(%rip), %rsi     # 0x202ad
movq %rax, %rdi
callq 0x6420
movq -0xc0(%rbp), %rsi
movq %rax, %rdi
callq 0x62c0
leaq 0x18f18(%rip), %rsi     # 0x202b7
movq %rax, %rdi
callq 0x6420
movq -0xb8(%rbp), %rsi
movq %rax, %rdi
callq 0x62c0
movq %rax, %rdi
callq 0x61c0
leaq -0x60(%rbp), %rdi
callq 0x8942
leaq -0x80(%rbp), %rdi
callq 0x1f8dc
leaq -0x90(%rbp), %rdi
callq 0x1f8dc
movq -0x68(%rbp), %rsp
leaq -0xb0(%rbp), %rdi
callq 0x67f8
movsd -0x38(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7419
jmp 0x7419
movq %rax, %rbx
jmp 0x7425
jmp 0x740d
jmp 0x7412
movq %rax, %rbx
jmp 0x742e
movq %rax, %rbx
jmp 0x743a
jmp 0x7419
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x8942
leaq -0x80(%rbp), %rdi
callq 0x1f8dc
leaq -0x90(%rbp), %rdi
callq 0x1f8dc
leaq -0xb0(%rbp), %rdi
callq 0x67f8
movq %rbx, %rdi
callq 0x66f0