// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x50, %rsp
movl %r8d, %ebx
movl %ecx, %ebp
movq %rdx, %r14
movl %esi, %edi
movq %rdx, 0x20(%rsp)
callq 0x7304
movq %rax, %r15
movq %rax, 0x18(%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0x73b6
movslq %ebp, %r12
imulq $0x28, %r12, %rsi
addq %r14, %rsi
movq %r14, %rdi
callq 0x7438
pushq $0x2
popq %rcx
movl %r12d, %eax
cltd
idivl %ecx
movl %eax, %ebp
movq 0xd1b2(%rip), %rdi      # 0x13fc0
leaq 0x563e(%rip), %rsi      # 0xc453
callq 0x21d0
xorl %r12d, %r12d
testl %ebp, %ebp
cmovgl %ebp, %r12d
subq $0x1, %r12
jb 0x6e3e
movq 0x20(%r14), %rdx
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *(%rax)
addq $0x28, %r14
jmp 0x6e23
movq 0xd17b(%rip), %r14      # 0x13fc0
leaq 0x5617(%rip), %rsi      # 0xc463
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20c0
leaq 0x18(%rsp), %r12
leaq 0x28(%rsp), %r15
movq %r12, (%r15)
leaq 0x20(%rsp), %rax
movq %rax, 0x8(%r15)
movl %ebp, 0x10(%r15)
movapd 0x51b2(%rip), %xmm0     # 0xc030
movupd %xmm0, 0x14(%r15)
movl %ebx, 0x24(%r15)
callq 0xad79
movsd %xmm0, 0x10(%rsp)
movq (%r12), %rdi
movslq %ebx, %rsi
leaq 0xf(%rsp), %rcx
movb $0x0, (%rcx)
movq %r15, %rdx
callq 0xb2dd
callq 0xad79
cvtsi2sd %ebp, %xmm1
subsd 0x10(%rsp), %xmm0
divsd %xmm0, %xmm1
divsd 0x51a3(%rip), %xmm1     # 0xc068
movsd %xmm1, 0x10(%rsp)
leaq 0x5353(%rip), %rsi      # 0xc225
movq %r14, %rdi
callq 0x21d0
leaq 0x5588(%rip), %rsi      # 0xc469
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x22e0
leaq 0x5361(%rip), %rsi      # 0xc25f
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20c0
leaq 0x5569(%rip), %rsi      # 0xc47e
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20c0
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq