// File: 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
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl -0xc(%rbp), %edi
callq 0x1b2b0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x1b3f0
movq -0x18(%rbp), %rdi
movslq -0x1c(%rbp), %rax
leaq (%rax,%rax,4), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1b4c0
movl -0x20(%rbp), %eax
movq %rax, -0x98(%rbp)
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
leaq (%rax,%rax,2), %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x1565c
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x1fbb0
movq -0xb0(%rbp), %rax
movq -0xa8(%rbp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0xa0(%rbp)
jne 0x1562b
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
leaq -0x28(%rbp), %rdx
movq %rdx, -0x60(%rbp)
leaq -0x18(%rbp), %rdx
movq %rdx, -0x58(%rbp)
movl -0x1c(%rbp), %edx
movl %edx, -0x50(%rbp)
movl -0x20(%rbp), %edx
movl %edx, -0x4c(%rbp)
movq %rcx, -0x48(%rbp)
movq %rax, -0x40(%rbp)
callq 0x1b570
callq 0x24510
movsd %xmm0, -0xb8(%rbp)
jmp 0x156a2
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x20(%rbp), %rsi
movb $0x0, -0x75(%rbp)
leaq -0x60(%rbp), %rdx
leaq -0x75(%rbp), %rcx
callq 0x24550
jmp 0x156ca
callq 0x24510
movsd %xmm0, -0xc0(%rbp)
jmp 0x156d9
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
movsd -0x68(%rbp), %xmm2
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd 0x2c903(%rip), %xmm1    # 0x42008
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movq 0x448a0(%rip), %rdi     # 0x59fb8
leaq 0x2ccfd(%rip), %rsi     # 0x4241c
callq 0x6400
movq %rax, -0xc8(%rbp)
jmp 0x1572d
movq -0xc8(%rbp), %rdi
movsd -0x88(%rbp), %xmm0
callq 0x6820
movq %rax, -0xd0(%rbp)
jmp 0x1574a
movq -0xd0(%rbp), %rdi
leaq 0x2cccc(%rip), %rsi     # 0x42424
callq 0x6400
movq %rax, -0xd8(%rbp)
jmp 0x15766
movq -0xd8(%rbp), %rdi
movq 0x4482c(%rip), %rsi     # 0x59fa0
callq 0x6460
jmp 0x1577b
movq -0x90(%rbp), %rcx
movq -0x98(%rbp), %rax
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0xe0(%rbp)
je 0x157d0
movq -0xe0(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0xe8(%rbp)
callq 0x20180
movq -0x90(%rbp), %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0xe0(%rbp)
jne 0x1579f
movq -0x30(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x90(%rbp), %rcx
movq %rax, %rsi
movq -0x98(%rbp), %rax
movq %rsi, -0x70(%rbp)
movl %edx, -0x74(%rbp)
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0xf0(%rbp)
je 0x1583b
movq -0xf0(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0xf8(%rbp)
callq 0x20180
movq -0x90(%rbp), %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0xf0(%rbp)
jne 0x1580a
jmp 0x1583d
movq -0x70(%rbp), %rdi
callq 0x6780
nopw %cs:(%rax,%rax)