// File: /workspace/github_cpp_makefile_star2_O3/chahk0129[P]hash_bench/./bench/benchmark.hpp
// Function: benchmark_t<char [32]>::latency(int, Pair<char [32]>*, int, int)
inline void benchmark_t<Key_t>::latency(int index_type, Pair<Key_t>* init_kv, int init_num, int num_threads){
    Hash<Key_t>* hashtable = getInstance<Key_t>(index_type);
    gen_input(init_kv, init_num);
    std::random_shuffle(init_kv, init_kv+init_num);
    
    std::vector<uint64_t> latencies[num_threads];

    auto latency_func = [&hashtable, &init_kv, init_num, num_threads, &latencies](int thread_id, bool){
	size_t chunk = init_num / num_threads;
	size_t from = chunk * thread_id;
	size_t to = chunk * (thread_id+1);

	struct timespec start, end;
	uint64_t elapsed;
	for(int i=from; i<to; i++){
#ifdef LATENCY
	    clock_gettime(CLOCK_MONOTONIC, &start);
#endif
	    hashtable->Insert(init_kv[i].key, init_kv[i].value);
#ifdef LATENCY
	    clock_gettime(CLOCK_MONOTONIC, &end);
	    elapsed = end.tv_nsec - start.tv_nsec + 1000000000*(end.tv_sec - start.tv_sec);
	    latencies[thread_id].push_back(elapsed);
#endif
	}
	return;
    };

    clear_cache();
    double start_time = get_now();
    start_threads(hashtable, num_threads, latency_func, false);
    double end_time = get_now();
#ifdef LATENCY
    std::vector<uint64_t> aggregated_load_latency;
    for(int i=0; i<num_threads; i++){
	if(!latencies[i].empty()){
	    for(auto it=latencies[i].begin(); it!=latencies[i].end(); it++)
		aggregated_load_latency.push_back(*it);
	}
    }
    std::sort(aggregated_load_latency.begin(), aggregated_load_latency.end());
    std::cout << "Tail latency for load: " << aggregated_load_latency.back()/1000 << " usec" << std::endl;
#endif
    double throughput = init_num / (end_time - start_time) / 1000000; // MOps/sec
    std::cout << "\033[1;31m" << throughput << " MOps/sec" << std::endl;


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, -0x2c(%rbp)
movl %ecx, %r15d
movq %rdx, %r14
movl %esi, %edi
movq %rdx, -0x60(%rbp)
callq 0x903d
movq %rax, -0x58(%rbp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x90f6
cmpl $0x2, %r15d
jb 0x87ff
movslq %r15d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rbx
movl $0x28, %r12d
movabsq $-0x3333333333333333, %r13 # imm = 0xCCCCCCCCCCCCCCCD
callq 0x2060
cltq
movq %r12, %rcx
sarq $0x3, %rcx
imulq %r13, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rax
cmpq %rax, %r12
je 0x87f2
leaq (%r14,%r12), %rcx
addq %r14, %rax
movq 0x20(%rcx), %rdx
movq %rdx, -0x80(%rbp)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movaps %xmm1, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movq 0x20(%rax), %rdx
movq %rdx, 0x20(%rcx)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movq -0x80(%rbp), %rcx
movq %rcx, 0x20(%rax)
movaps -0xa0(%rbp), %xmm0
movapd -0x90(%rbp), %xmm1
movupd %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
addq $0x28, %r12
cmpq %r12, %rbx
jne 0x8772
movq %rsp, -0x40(%rbp)
movl -0x2c(%rbp), %edx
movl %edx, %r13d
movq %rsp, %r14
leaq (,%r13,2), %rax
addq %r13, %rax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r14
movq %r14, %rsp
testl %edx, %edx
je 0x885c
leaq -0x18(,%rax,8), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq 0x18(,%rax,8), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20e0
leaq -0x58(%rbp), %rbx
leaq -0xa0(%rbp), %r12
movq %rbx, (%r12)
leaq -0x60(%rbp), %rax
movq %rax, 0x8(%r12)
movl %r15d, -0x30(%rbp)
movl %r15d, 0x10(%r12)
movl -0x2c(%rbp), %r15d
movl %r15d, 0x14(%r12)
movq %r13, -0x38(%rbp)
movq %r13, 0x18(%r12)
movq %r14, 0x20(%r12)
leaq -0x70(%rbp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x22e0
xorps %xmm0, %xmm0
cvtsi2sdq (%r13), %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
cvtsi2sdq 0x8(%r13), %xmm0
movsd %xmm0, -0x50(%rbp)
movq (%rbx), %rdi
movslq %r15d, %rsi
movb $0x0, (%r13)
leaq -0x70(%rbp), %rcx
movq %r12, %rdx
callq 0xd2e4
leaq -0x70(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x22e0
movq (%r12), %r13
movq 0x8(%r12), %r12
movq 0xc6cd(%rip), %rdi      # 0x14fc0
leaq 0x5b61(%rip), %rsi      # 0xe45b
movl $0x7, %edx
callq 0x2210
movsd -0x50(%rbp), %xmm3
divsd 0x5787(%rip), %xmm3     # 0xe098
addsd -0x48(%rbp), %xmm3
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movsd 0x5772(%rip), %xmm1     # 0xe098
divsd %xmm1, %xmm0
cvtsi2sd %r13, %xmm2
addsd %xmm0, %xmm2
subsd %xmm3, %xmm2
xorps %xmm0, %xmm0
cvtsi2sdl -0x30(%rbp), %xmm0
divsd %xmm2, %xmm0
divsd %xmm1, %xmm0
movq 0xc672(%rip), %rdi      # 0x14fc0
callq 0x22d0
movq %rax, %r15
leaq 0x5b06(%rip), %rsi      # 0xe463
movl $0x9, %edx
movq %rax, %rdi
callq 0x2210
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
cmpl $0x0, -0x2c(%rbp)
je 0x89c0
movq -0x38(%rbp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
movq -0x18(%r14,%rbx), %rdi
testq %rdi, %rdi
je 0x89ba
movq -0x8(%r14,%rbx), %rsi
subq %rdi, %rsi
callq 0x2200
addq $-0x18, %rbx
jne 0x89a3
movq -0x40(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x89d5
movq %rax, %r15
cmpl $0x0, -0x2c(%rbp)
je 0x8a07
movq -0x38(%rbp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
movq -0x18(%r14,%rbx), %rdi
testq %rdi, %rdi
je 0x8a01
movq -0x8(%r14,%rbx), %rsi
subq %rdi, %rsi
callq 0x2200
addq $-0x18, %rbx
jne 0x89ea
movq %r15, %rdi
callq 0x2330
nop