// File: /workspace/github_cpp_makefile_star2_O3/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 0x2ce29(%rip), %rbx     # 0x36fd0
leaq 0x19ff6(%rip), %rsi     # 0x241a4
movl $0x2c, %edx
movq %rbx, %rdi
callq 0x6490
movq %rbx, %rdi
movl %r14d, -0x2c(%rbp)
movl %r14d, %esi
callq 0x6700
movq %rax, %rbx
leaq 0x19f53(%rip), %rsi     # 0x24127
movl $0x11, %edx
movq %rax, %rdi
callq 0x6490
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6440
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x62f0
leaq -0xb8(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xc8(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movl $0x76, %edx
callq 0x64b0
movq %rsp, -0x70(%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, -0x68(%rbp)
movq %rdx, %rsp
movl %r15d, -0x30(%rbp)
testl %r15d, %r15d
jle 0xa2fa
movl -0x2c(%rbp), %eax
cltd
idivl -0x30(%rbp)
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rbx
cmovnsq %rcx, %rbx
movq %rax, -0x78(%rbp)
decl %eax
movq %rax, -0x80(%rbp)
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)
movq -0x80(%rbp), %rax
addl %r12d, %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 %rbx, %rdi
callq 0x6040
movq %rax, 0x20(%r14,%r15)
addl -0x78(%rbp), %r12d
addq $0x28, %r15
cmpq %r15, %r13
jne 0xa2af
leaq -0xa8(%rbp), %rdi
callq 0x22e38
movl -0x30(%rbp), %r15d
testl %r15d, %r15d
movq -0x58(%rbp), %r13
movq -0x68(%rbp), %rax
jle 0xa367
leaq -0x374(%rip), %rbx      # 0x9faa
movq %rax, %r15
movq %r14, %r12
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x6770
addq $0x28, %r12
addq $0x8, %r15
decq %r13
jne 0xa324
movl -0x30(%rbp), %r15d
testl %r15d, %r15d
movq -0x58(%rbp), %r13
movq -0x68(%rbp), %r12
jle 0xa367
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
xorl %esi, %esi
callq 0x6410
incq %rbx
cmpq %rbx, %r13
jne 0xa354
leaq -0x98(%rbp), %rdi
callq 0x22e38
leaq -0x98(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x22ea4
movq %rax, -0x60(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
testl %r15d, %r15d
jle 0xa3f7
movl -0x2c(%rbp), %eax
cltd
idivl %r15d
movl %eax, %ebx
shlq $0x3, %r13
leaq (%r13,%r13,4), %r13
xorl %r12d, %r12d
leaq -0x50(%rbp), %r15
movq 0x20(%r14,%r12), %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0xc4ea
cmpq $0x0, -0x38(%rbp)
je 0xa3df
movq 0x20(%r14,%r12), %rsi
movq -0x38(%rbp), %rdi
movl %ebx, %edx
callq 0xc4ea
movq 0x20(%r14,%r12), %rdi
testq %rdi, %rdi
je 0xa3ee
callq 0x6520
addq $0x28, %r12
cmpq %r12, %r13
jne 0xa3b9
leaq -0x50(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xc530
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rsi
cmpq %rsi, %rcx
je 0xa47f
xorl %eax, %eax
movq %rcx, %rdx
addq (%rdx), %rax
addq $0x8, %rdx
cmpq %rsi, %rdx
jne 0xa419
movq %rsi, %rdi
subq %rcx, %rdi
sarq $0x3, %rdi
xorl %edx, %edx
divq %rdi
movq %rax, %r13
leaq 0x8(%rcx), %rax
cmpq %rsi, %rax
je 0xa482
movq (%rcx), %rdx
movq %rdx, %rdi
movq %rax, %r9
movq %rcx, %r8
movq (%r9), %r10
cmpq %rdi, %r10
cmovbq %r10, %rdi
cmovbq %r9, %r8
addq $0x8, %r9
cmpq %rsi, %r9
jne 0xa44c
movq (%r8), %r14
movq (%rax), %rdi
cmpq %rdi, %rdx
cmovbeq %rdi, %rdx
cmovbq %rax, %rcx
addq $0x8, %rax
cmpq %rsi, %rax
jne 0xa466
jmp 0xa485
xorl %r13d, %r13d
movq (%rcx), %r14
movq (%rcx), %r15
movq 0x2cb41(%rip), %rdi     # 0x36fd0
leaq 0x19d3b(%rip), %rsi     # 0x241d1
movl $0x21, %edx
callq 0x6490
xorps %xmm0, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm0
divsd 0x19b57(%rip), %xmm0    # 0x24008
movq 0x2cb18(%rip), %rdi     # 0x36fd0
movsd %xmm0, -0x38(%rbp)
callq 0x6670
movq %rax, %rbx
leaq 0x19ee4(%rip), %rsi     # 0x243b0
movl $0x1, %edx
movq %rax, %rdi
callq 0x6490
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6440
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x62f0
movq 0x2cac9(%rip), %rdi     # 0x36fd0
leaq 0x19ce5(%rip), %rsi     # 0x241f3
movl $0x22, %edx
callq 0x6490
xorps %xmm0, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movq 0x2caa4(%rip), %rdi     # 0x36fd0
callq 0x6670
movq %rax, %rbx
leaq 0x19cdb(%rip), %rsi     # 0x24216
movl $0x14, %edx
movq %rax, %rdi
callq 0x6490
movq %rbx, %rdi
movq %r13, %rsi
callq 0x62e0
movq %rax, %rbx
leaq 0x19cce(%rip), %rsi     # 0x2422b
movl $0x11, %edx
movq %rax, %rdi
callq 0x6490
movslq -0x2c(%rbp), %rax
imulq $0x100a, %rax, %rax     # imm = 0x100A
movq %rax, %xmm1
punpckldq 0x19a9e(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x19aa6(%rip), %xmm1    # 0x24030
movsd 0x19a7e(%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 0x6670
movq %rax, %rbx
leaq 0x19c7c(%rip), %rsi     # 0x2423d
movl $0x5, %edx
movq %rax, %rdi
callq 0x6490
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6440
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x62f0
movq 0x2c9d4(%rip), %rdi     # 0x36fd0
leaq 0x19c40(%rip), %rsi     # 0x24243
movl $0x21, %edx
callq 0x6490
movq 0x2c9bc(%rip), %rdi     # 0x36fd0
movq %r14, %rsi
callq 0x62e0
movq %rax, %rbx
leaq 0x19c3f(%rip), %rsi     # 0x24265
movl $0x8, %edx
movq %rax, %rdi
callq 0x6490
movq %rbx, %rdi
movq %r15, %rsi
callq 0x62e0
movq %rax, %rbx
leaq 0x19c26(%rip), %rsi     # 0x2426e
movl $0x8, %edx
movq %rax, %rdi
callq 0x6490
movq -0xf8(%rbp), %rsi
movq %rbx, %rdi
callq 0x62e0
movq %rax, %rbx
leaq 0x19ac9(%rip), %rsi     # 0x24137
movl $0x1, %edx
movq %rax, %rdi
callq 0x6490
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6440
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x62f0
movq 0x2c927(%rip), %rdi     # 0x36fd0
leaq 0x19bc7(%rip), %rsi     # 0x24277
movl $0x21, %edx
callq 0x6490
movq -0xf0(%rbp), %rsi
movq 0x2c908(%rip), %rdi     # 0x36fd0
callq 0x62e0
movq %rax, %rbx
leaq 0x19bc2(%rip), %rsi     # 0x24299
movl $0x8, %edx
movq %rax, %rdi
callq 0x6490
movq -0xe8(%rbp), %rsi
movq %rbx, %rdi
callq 0x62e0
movq %rax, %rbx
leaq 0x19ba5(%rip), %rsi     # 0x242a2
movl $0x8, %edx
movq %rax, %rdi
callq 0x6490
movq -0xe0(%rbp), %rsi
movq %rbx, %rdi
callq 0x62e0
movq %rax, %rbx
leaq 0x19b88(%rip), %rsi     # 0x242ab
movl $0x9, %edx
movq %rax, %rdi
callq 0x6490
movq -0xd8(%rbp), %rsi
movq %rbx, %rdi
callq 0x62e0
movq %rax, %rbx
leaq 0x19b6c(%rip), %rsi     # 0x242b5
movl $0xa, %edx
movq %rax, %rdi
callq 0x6490
movq -0xd0(%rbp), %rsi
movq %rbx, %rdi
callq 0x62e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6440
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6050
movq %rax, %rdi
callq 0x62f0
movq -0x50(%rbp), %rdi
cmpq %rdi, -0x48(%rbp)
je 0xa79d
movq %rdi, -0x48(%rbp)
testq %rdi, %rdi
je 0xa7a7
callq 0x6430
leaq -0x98(%rbp), %rdi
callq 0x22ebe
leaq -0xa8(%rbp), %rdi
callq 0x22ebe
movq -0x70(%rbp), %rsp
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xa7db
callq 0x6430
movsd -0x38(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa806
movq %rax, %rbx
jmp 0xa821
jmp 0xa7fa
jmp 0xa7ff
movq %rax, %rbx
jmp 0xa82d
movq %rax, %rbx
jmp 0xa839
jmp 0xa806
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %rdi, -0x48(%rbp)
je 0xa817
movq %rdi, -0x48(%rbp)
testq %rdi, %rdi
je 0xa821
callq 0x6430
leaq -0x98(%rbp), %rdi
callq 0x22ebe
leaq -0xa8(%rbp), %rdi
callq 0x22ebe
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xa851
callq 0x6430
movq %rbx, %rdi
callq 0x6710