// File: chahk0129[P]hash_bench/./bench/benchmark.hpp
// Function: benchmark_t<char [32]>::mixed(int, Pair<char [32]>*, int, int)
inline void benchmark_t<Key_t>::mixed(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);

    int warmup = init_num/2;
    std::cout << "warming up ... "; 
    for(int i=0; i<warmup; i++){
	hashtable->Insert(init_kv[i].key, init_kv[i].value);
    }
    std::cout << "Done!" << std::endl;

    int insert_ratio = 15;
    int search_ratio = 50;
    int delete_ratio = 10;
    int update_ratio = 25;

    auto mixed_func = [&hashtable, &init_kv, warmup, insert_ratio, search_ratio, delete_ratio, update_ratio, num_threads](int thread_id, bool){
	size_t chunk = warmup / num_threads;
	size_t from = chunk * thread_id;
	size_t to = chunk * (thread_id + 1);
	int insert_idx = warmup + from;
	int search_idx = from;

	for(int i=from; i<to; i+=1000){
	    int r = rand() % 100;
	    if(r < insert_ratio){
		for(int j=insert_idx; j<insert_idx+1000; j++)
		    hashtable->Insert(init_kv[j].key, init_kv[j].value);
		insert_idx += 1000;
	    }
	    else if(r < insert_ratio+search_ratio){
		for(int j=search_idx; j<search_idx+1000; j++)
		    auto ret = hashtable->Get(init_kv[j].key);
		search_idx += 1000;
	    }
	    else if(r < insert_ratio+search_ratio+update_ratio){
		for(int j=search_idx; j<search_idx+1000; j++)
		    auto ret = hashtable->Update(init_kv[j].key, init_kv[j].value);
		search_idx += 1000;
	    }
	    else{
		for(int j=search_idx; j<search_idx+1000; j++)
		    auto ret = hashtable->Delete(init_kv[j].key);
		search_idx += 1000;
	    }
	}
    };

    clear_cache();
    double start_time = get_now();
    start_threads(hashtable, num_threads, mixed_func, false);
    double end_time = get_now();

    double throughput = warmup / (end_time - start_time) / 1000000; // MOps/sec
    std::cout << "\033[1;32m";
    std::cout << "Throughput(MOps/sec)" << throughput << "\033[0m" << std::endl;
    std::cout << "Insert(10%), Search(50%), Delete(10%), Update(25%)" << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
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
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
callq 0x1b4c0
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x2c(%rbp)
movq 0x44706(%rip), %rdi     # 0x59fb8
leaq 0x2cb93(%rip), %rsi     # 0x4244c
callq 0x6400
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x15903
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x30(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rdx
movq (%rdi), %rax
callq *(%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x158c5
movq 0x446ae(%rip), %rdi     # 0x59fb8
leaq 0x2cb4b(%rip), %rsi     # 0x4245c
callq 0x6400
movq %rax, %rdi
movq 0x44680(%rip), %rsi     # 0x59fa0
callq 0x6460
movl $0xf, -0x34(%rbp)
movl $0x32, -0x38(%rbp)
movl $0xa, -0x3c(%rbp)
movl $0x19, -0x40(%rbp)
leaq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x44(%rbp)
callq 0x1b570
callq 0x24510
movsd %xmm0, -0x70(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x20(%rbp), %rsi
movb $0x0, -0x71(%rbp)
leaq -0x68(%rbp), %rdx
leaq -0x71(%rbp), %rcx
callq 0x26000
callq 0x24510
movsd %xmm0, -0x80(%rbp)
cvtsi2sdl -0x2c(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
subsd -0x70(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x2c646(%rip), %xmm1    # 0x42008
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movq 0x445e3(%rip), %rdi     # 0x59fb8
leaq 0x2c821(%rip), %rsi     # 0x421fd
callq 0x6400
movq 0x445d0(%rip), %rdi     # 0x59fb8
leaq 0x2ca73(%rip), %rsi     # 0x42462
callq 0x6400
movq %rax, %rdi
movsd -0x88(%rbp), %xmm0
callq 0x6820
movq %rax, %rdi
leaq 0x2c829(%rip), %rsi     # 0x42237
callq 0x6400
movq %rax, %rdi
movq 0x44583(%rip), %rsi     # 0x59fa0
callq 0x6460
movq 0x4458f(%rip), %rdi     # 0x59fb8
leaq 0x2ca47(%rip), %rsi     # 0x42477
callq 0x6400
movq %rax, %rdi
movq 0x44561(%rip), %rsi     # 0x59fa0
callq 0x6460
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)