// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %r8d, %r14d
movl %ecx, %r15d
movq %rdx, %rbx
movl %esi, %edi
movq %rdx, -0x68(%rbp)
callq 0x7304
movq %rax, -0x60(%rbp)
movq %rbx, %rdi
movl %r15d, %esi
callq 0x73b6
movslq %r15d, %rax
imulq $0x28, %rax, %rsi
addq %rbx, %rsi
movq %rbx, %rdi
callq 0x7438
movq %rsp, -0x48(%rbp)
movl %r14d, %eax
movq %rsp, %rbx
movq %rax, -0x40(%rbp)
imulq $0x18, %rax, %r13
leaq 0xf(%r13), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl %r14d, -0x2c(%rbp)
testl %r14d, %r14d
je 0x6cb2
leaq -0x18(%r13), %rcx
pushq $0x18
popq %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
subq %rdx, %rcx
addq $0x18, %rcx
movq %rbx, %rdi
xorl %esi, %esi
movq %rcx, %rdx
callq 0x20e0
leaq -0x60(%rbp), %r14
leaq -0x90(%rbp), %r12
movq %r14, (%r12)
leaq -0x68(%rbp), %rax
movq %rax, 0x8(%r12)
movl %r15d, -0x34(%rbp)
movl %r15d, 0x10(%r12)
movl -0x2c(%rbp), %r15d
movl %r15d, 0x14(%r12)
movq -0x40(%rbp), %rax
movq %rax, 0x18(%r12)
movq %rbx, 0x20(%r12)
callq 0xad79
movsd %xmm0, -0x58(%rbp)
movq (%r14), %rdi
movslq %r15d, %rsi
leaq -0x2d(%rbp), %rcx
movb $0x0, (%rcx)
movq %r12, %rdx
callq 0xada8
callq 0xad79
movsd %xmm0, -0x50(%rbp)
movq 0xd2a6(%rip), %rdi      # 0x13fc0
leaq 0x5702(%rip), %rsi      # 0xc423
callq 0x21d0
cvtsi2sdl -0x34(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
subsd -0x58(%rbp), %xmm1
divsd %xmm1, %xmm0
divsd 0x5327(%rip), %xmm0     # 0xc068
movq %rax, %rdi
callq 0x22e0
leaq 0x56db(%rip), %rsi      # 0xc42b
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20c0
cmpl $0x0, -0x2c(%rbp)
je 0x6d7e
addq $-0x18, %rbx
imulq $0x18, -0x40(%rbp), %r14
leaq (%rbx,%r14), %rdi
callq 0xaa8c
addq $-0x18, %r14
jne 0x6d6f
movq -0x48(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6d93
movq %rax, %r15
cmpl $0x0, -0x2c(%rbp)
je 0x6daf
addq $-0x18, %rbx
leaq (%rbx,%r13), %rdi
callq 0xaa8c
addq $-0x18, %r13
jne 0x6da0
movq %r15, %rdi
callq 0x2340
nop