// File: yangwanyuan[P]HLKVDS/tools/Benchmark.cc
// Function: Bench_Overwrite(benchmark_arg)
void Bench_Overwrite(benchmark_arg bm_arg) {
    string file_path = bm_arg.file_path;
    int db_size = bm_arg.db_size;
    int record_num =bm_arg.record_num;
    int thread_num = bm_arg.thread_num;
    int segment_K = bm_arg.segment_K;
    int shards_num = bm_arg.shards_num;
    int ds_type = bm_arg.ds_type;
    int aggregate = bm_arg.aggregate;

    vector<string> key_list;
    Create_Keys(record_num, key_list);
    if (Create_DB(file_path, db_size, segment_K, shards_num, ds_type) < 0) {
        cout << "Create DB Fail!!!" <<endl;
        return;
    }

    KVDS *db = Open_DB(file_path, shards_num, aggregate);

    double total_time;
    LatMgr *total_lat_mgr = new LatMgr;
    for (int i = 0; i < OVERWRITE_TIMES; i++) {
        cout << "======================";
        cout << "Insert " << i << " Start!!!";
        cout << "======================" << endl;

        total_time += Bench_Insert(db, record_num, key_list, thread_num, total_lat_mgr);

        cout << "======================";
        cout << "Insert " << i << " Completed!!!";
        cout << "======================" << endl;
    }
    cout << "======================";
    cout << "Finsh All Insert!!!";
    cout << "======================" << endl;

    Lat_Stats lat_stats;
    total_lat_mgr->GetStatistics(lat_stats);

    uint64_t total_record_num = record_num * OVERWRITE_TIMES;
    double iops = total_record_num / total_time;

    uint64_t total_size = (uint64_t)total_record_num * (KEY_SIZE + VALUE_SIZE);
    double throughput = total_size / (total_time * 1024 * 1024);

    uint64_t avg = total_lat_mgr->GetAvg();
    uint64_t min = total_lat_mgr->GetMin();
    uint64_t max = total_lat_mgr->GetMax();

    cout << "Overwrite Bench Finish. Total time: " << total_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;

    delete total_lat_mgr;
    delete db;
}

// Assembly:
subq $0x2b8, %rsp            # imm = 0x2B8
movq %rdi, %rsi
movq %rsi, 0x168(%rsp)
movq %rsi, 0x2b0(%rsp)
leaq 0x290(%rsp), %rdi
callq 0x81b0
movq 0x168(%rsp), %rdi
movl 0x20(%rdi), %eax
movl %eax, 0x28c(%rsp)
movl 0x24(%rdi), %eax
movl %eax, 0x288(%rsp)
movl 0x28(%rdi), %eax
movl %eax, 0x284(%rsp)
movl 0x2c(%rdi), %eax
movl %eax, 0x280(%rsp)
movl 0x30(%rdi), %eax
movl %eax, 0x27c(%rsp)
movl 0x34(%rdi), %eax
movl %eax, 0x278(%rsp)
movl 0x38(%rdi), %eax
movl %eax, 0x274(%rsp)
leaq 0x258(%rsp), %rdi
movq %rdi, 0x170(%rsp)
callq 0xd290
movq 0x170(%rsp), %rsi
movl 0x288(%rsp), %edi
callq 0x9340
jmp 0xba10
leaq 0x228(%rsp), %rdi
leaq 0x290(%rsp), %rsi
callq 0x81b0
jmp 0xba27
movl 0x28c(%rsp), %esi
movl 0x280(%rsp), %edx
movl 0x27c(%rsp), %ecx
movl 0x278(%rsp), %r8d
leaq 0x228(%rsp), %rdi
callq 0x8dc0
movl %eax, 0x164(%rsp)
jmp 0xba5a
leaq 0x228(%rsp), %rdi
callq 0x8998
movl 0x164(%rsp), %eax
cmpl $0x0, %eax
jge 0xbaf9
movq 0x6c552(%rip), %rdi     # 0x77fd0
leaq 0x4b9ca(%rip), %rsi     # 0x5744f
callq 0x8520
movq %rax, 0x158(%rsp)
jmp 0xba94
movq 0x158(%rsp), %rdi
movq 0x6c4fd(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xbaaa
movl $0x1, 0x224(%rsp)
jmp 0xc28b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x250(%rsp)
movl %eax, 0x24c(%rsp)
jmp 0xc2ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x250(%rsp)
movl %eax, 0x24c(%rsp)
leaq 0x228(%rsp), %rdi
callq 0x8998
jmp 0xc2ad
leaq 0x1f8(%rsp), %rdi
leaq 0x290(%rsp), %rsi
callq 0x81b0
jmp 0xbb10
movl 0x27c(%rsp), %esi
movl 0x274(%rsp), %edx
leaq 0x1f8(%rsp), %rdi
callq 0x90d0
movq %rax, 0x150(%rsp)
jmp 0xbb35
leaq 0x1f8(%rsp), %rdi
callq 0x8998
movq 0x150(%rsp), %rax
movq %rax, 0x218(%rsp)
movl $0x18, %edi
callq 0x8530
movq %rax, 0x148(%rsp)
jmp 0xbb66
movq 0x148(%rsp), %rdi
callq 0xcf10
movq 0x148(%rsp), %rax
movq %rax, 0x1e8(%rsp)
movl $0x0, 0x1e4(%rsp)
cmpl $0xa, 0x1e4(%rsp)
jge 0xbd61
movq 0x6c42d(%rip), %rdi     # 0x77fd0
leaq 0x4b8b7(%rip), %rsi     # 0x57461
callq 0x8520
jmp 0xbbb1
movq 0x6c418(%rip), %rdi     # 0x77fd0
leaq 0x4b8b9(%rip), %rsi     # 0x57478
callq 0x8520
movq %rax, 0x140(%rsp)
jmp 0xbbce
movq 0x140(%rsp), %rdi
movl 0x1e4(%rsp), %esi
callq 0x8870
movq %rax, 0x138(%rsp)
jmp 0xbbec
movq 0x138(%rsp), %rdi
leaq 0x4b885(%rip), %rsi     # 0x57480
callq 0x8520
jmp 0xbc02
movq 0x6c3c7(%rip), %rdi     # 0x77fd0
leaq 0x4b851(%rip), %rsi     # 0x57461
callq 0x8520
movq %rax, 0x130(%rsp)
jmp 0xbc1f
movq 0x130(%rsp), %rdi
movq 0x6c372(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xbc35
movq 0x218(%rsp), %rdi
movl 0x288(%rsp), %esi
movl 0x284(%rsp), %ecx
movq 0x1e8(%rsp), %r8
leaq 0x258(%rsp), %rdx
callq 0x9820
movsd %xmm0, 0x128(%rsp)
jmp 0xbc6b
movsd 0x128(%rsp), %xmm1
movsd 0x1f0(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x1f0(%rsp)
movq 0x6c33f(%rip), %rdi     # 0x77fd0
leaq 0x4b7c9(%rip), %rsi     # 0x57461
callq 0x8520
jmp 0xbc9f
movq 0x6c32a(%rip), %rdi     # 0x77fd0
leaq 0x4b7cb(%rip), %rsi     # 0x57478
callq 0x8520
movq %rax, 0x120(%rsp)
jmp 0xbcbc
movq 0x120(%rsp), %rdi
movl 0x1e4(%rsp), %esi
callq 0x8870
movq %rax, 0x118(%rsp)
jmp 0xbcda
movq 0x118(%rsp), %rdi
leaq 0x4b7a1(%rip), %rsi     # 0x5748a
callq 0x8520
jmp 0xbcf0
movq 0x6c2d9(%rip), %rdi     # 0x77fd0
leaq 0x4b763(%rip), %rsi     # 0x57461
callq 0x8520
movq %rax, 0x110(%rsp)
jmp 0xbd0d
movq 0x110(%rsp), %rdi
movq 0x6c284(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xbd23
jmp 0xbd25
movl 0x1e4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1e4(%rsp)
jmp 0xbb8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x250(%rsp)
movl %eax, 0x24c(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x8998
jmp 0xc2ad
movq 0x6c268(%rip), %rdi     # 0x77fd0
leaq 0x4b6f2(%rip), %rsi     # 0x57461
callq 0x8520
jmp 0xbd76
movq 0x6c253(%rip), %rdi     # 0x77fd0
leaq 0x4b714(%rip), %rsi     # 0x57498
callq 0x8520
jmp 0xbd8b
movq 0x6c23e(%rip), %rdi     # 0x77fd0
leaq 0x4b6c8(%rip), %rsi     # 0x57461
callq 0x8520
movq %rax, 0x108(%rsp)
jmp 0xbda8
movq 0x108(%rsp), %rdi
movq 0x6c1e9(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xbdbe
movq 0x1e8(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0xcf80
jmp 0xbdd5
movl 0x288(%rsp), %ecx
addl %ecx, %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %eax
cltq
movq %rax, 0x1a8(%rsp)
movq 0x1a8(%rsp), %xmm0
movaps 0x4b243(%rip), %xmm2    # 0x57040
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x4b247(%rip), %xmm1    # 0x57050
subpd %xmm1, %xmm0
movaps %xmm0, %xmm3
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm3, %xmm0
movsd 0x1f0(%rsp), %xmm3
divsd %xmm3, %xmm0
movsd %xmm0, 0x1a0(%rsp)
movq 0x1a8(%rsp), %rax
imulq $0x100a, %rax, %rax     # imm = 0x100A
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x1f0(%rsp), %xmm1
movsd 0x4b19e(%rip), %xmm2    # 0x57010
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x190(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0xd110
movq %rax, 0x100(%rsp)
jmp 0xbe9e
movq 0x100(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0xd1c0
movq %rax, 0xf8(%rsp)
jmp 0xbec5
movq 0xf8(%rsp), %rax
movq %rax, 0x180(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0xd210
movq %rax, 0xf0(%rsp)
jmp 0xbeec
movq 0xf0(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x6c0cd(%rip), %rdi     # 0x77fd0
leaq 0x4b5a2(%rip), %rsi     # 0x574ac
callq 0x8520
movq %rax, 0xe8(%rsp)
jmp 0xbf19
movq 0xe8(%rsp), %rdi
movsd 0x1f0(%rsp), %xmm0
callq 0x8940
movq %rax, 0xe0(%rsp)
jmp 0xbf39
movq 0xe0(%rsp), %rdi
leaq 0x4b48f(%rip), %rsi     # 0x573d7
callq 0x8520
movq %rax, 0xd8(%rsp)
jmp 0xbf57
movq 0xd8(%rsp), %rdi
movq 0x6c03a(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xbf6d
movq 0x6c05c(%rip), %rdi     # 0x77fd0
leaq 0x4b29f(%rip), %rsi     # 0x5721a
callq 0x8520
movq %rax, 0xd0(%rsp)
jmp 0xbf8a
movq 0xd0(%rsp), %rdi
movsd 0x1a0(%rsp), %xmm0
callq 0x8940
movq %rax, 0xc8(%rsp)
jmp 0xbfaa
movq 0xc8(%rsp), %rdi
leaq 0x4b284(%rip), %rsi     # 0x5723d
callq 0x8520
movq %rax, 0xc0(%rsp)
jmp 0xbfc8
movq 0xc0(%rsp), %rdi
movq 0x188(%rsp), %rsi
callq 0x80f0
movq %rax, 0xb8(%rsp)
jmp 0xbfe7
movq 0xb8(%rsp), %rdi
leaq 0x4b25c(%rip), %rsi     # 0x57252
callq 0x8520
movq %rax, 0xb0(%rsp)
jmp 0xc005
movq 0xb0(%rsp), %rdi
movsd 0x190(%rsp), %xmm0
callq 0x8940
movq %rax, 0xa8(%rsp)
jmp 0xc025
movq 0xa8(%rsp), %rdi
leaq 0x4b230(%rip), %rsi     # 0x57264
callq 0x8520
movq %rax, 0xa0(%rsp)
jmp 0xc043
movq 0xa0(%rsp), %rdi
movq 0x6bf4e(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xc059
movq 0x6bf70(%rip), %rdi     # 0x77fd0
leaq 0x4b203(%rip), %rsi     # 0x5726a
callq 0x8520
movq %rax, 0x98(%rsp)
jmp 0xc076
movq 0x98(%rsp), %rdi
movq 0x180(%rsp), %rsi
callq 0x80f0
movq %rax, 0x90(%rsp)
jmp 0xc095
movq 0x90(%rsp), %rdi
leaq 0x4b1e8(%rip), %rsi     # 0x5728c
callq 0x8520
movq %rax, 0x88(%rsp)
jmp 0xc0b3
movq 0x88(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x80f0
movq %rax, 0x80(%rsp)
jmp 0xc0d2
movq 0x80(%rsp), %rdi
leaq 0x4b1b4(%rip), %rsi     # 0x57295
callq 0x8520
movq %rax, 0x78(%rsp)
jmp 0xc0ed
movq 0x78(%rsp), %rdi
movq 0x1b0(%rsp), %rsi
callq 0x80f0
movq %rax, 0x70(%rsp)
jmp 0xc106
movq 0x70(%rsp), %rdi
leaq 0x4b04c(%rip), %rsi     # 0x5715e
callq 0x8520
movq %rax, 0x68(%rsp)
jmp 0xc11e
movq 0x68(%rsp), %rdi
movq 0x6be76(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xc131
movq 0x6be98(%rip), %rdi     # 0x77fd0
leaq 0x4b15f(%rip), %rsi     # 0x5729e
callq 0x8520
movq %rax, 0x60(%rsp)
jmp 0xc14b
movq 0x60(%rsp), %rdi
movq 0x1b8(%rsp), %rsi
callq 0x80f0
movq %rax, 0x58(%rsp)
jmp 0xc164
movq 0x58(%rsp), %rdi
leaq 0x4b150(%rip), %rsi     # 0x572c0
callq 0x8520
movq %rax, 0x50(%rsp)
jmp 0xc17c
movq 0x50(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
callq 0x80f0
movq %rax, 0x48(%rsp)
jmp 0xc195
movq 0x48(%rsp), %rdi
leaq 0x4b128(%rip), %rsi     # 0x572c9
callq 0x8520
movq %rax, 0x40(%rsp)
jmp 0xc1ad
movq 0x40(%rsp), %rdi
movq 0x1c8(%rsp), %rsi
callq 0x80f0
movq %rax, 0x38(%rsp)
jmp 0xc1c6
movq 0x38(%rsp), %rdi
leaq 0x4b100(%rip), %rsi     # 0x572d2
callq 0x8520
movq %rax, 0x30(%rsp)
jmp 0xc1de
movq 0x30(%rsp), %rdi
movq 0x1d0(%rsp), %rsi
callq 0x80f0
movq %rax, 0x28(%rsp)
jmp 0xc1f7
movq 0x28(%rsp), %rdi
leaq 0x4b0d9(%rip), %rsi     # 0x572dc
callq 0x8520
movq %rax, 0x20(%rsp)
jmp 0xc20f
movq 0x20(%rsp), %rdi
movq 0x1d8(%rsp), %rsi
callq 0x80f0
movq %rax, 0x18(%rsp)
jmp 0xc228
movq 0x18(%rsp), %rdi
movq 0x6bd6c(%rip), %rsi     # 0x77fa0
callq 0x8560
jmp 0xc23b
movq 0x1e8(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xc262
movq 0x10(%rsp), %rdi
callq 0xd260
movq 0x10(%rsp), %rdi
callq 0x84e0
movq 0x218(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xc280
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x0, 0x224(%rsp)
leaq 0x258(%rsp), %rdi
callq 0xd2a0
leaq 0x290(%rsp), %rdi
callq 0x8998
addq $0x2b8, %rsp            # imm = 0x2B8
retq
leaq 0x258(%rsp), %rdi
callq 0xd2a0
leaq 0x290(%rsp), %rdi
callq 0x8998
movq 0x250(%rsp), %rdi
callq 0x8880
nopw %cs:(%rax,%rax)