// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, (%rsp)
movl %ecx, %ebp
movq %rdx, %r14
movl %esi, %edi
movq %rdx, 0x18(%rsp)
callq 0x903d
movq %rax, %r15
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0x90f6
cmpl $0x1, %ebp
ja 0x8a6a
movq 0xc56f(%rip), %rdi      # 0x14fc0
leaq 0x5a33(%rip), %rsi      # 0xe48b
movl $0xf, %edx
callq 0x2210
xorl %r13d, %r13d
jmp 0x8b4f
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r12
movl $0x28, %r13d
movabsq $-0x3333333333333333, %rbx # imm = 0xCCCCCCCCCCCCCCCD
callq 0x2060
cltq
movq %r13, %rcx
sarq $0x3, %rcx
imulq %rbx, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rax
cmpq %rax, %r13
je 0x8afd
leaq (%r14,%r13), %rcx
addq %r14, %rax
movq 0x20(%rcx), %rdx
movq %rdx, 0x50(%rsp)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq 0x20(%rax), %rdx
movq %rdx, 0x20(%rcx)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movq 0x50(%rsp), %rcx
movq %rcx, 0x20(%rax)
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
addq $0x28, %r13
cmpq %r13, %r12
jne 0x8a85
movl %ebp, %r13d
shrl $0x1f, %r13d
addl %ebp, %r13d
sarl %r13d
movq 0xc4a2(%rip), %rdi      # 0x14fc0
leaq 0x5966(%rip), %rsi      # 0xe48b
movl $0xf, %edx
callq 0x2210
cmpl $0x2, %ebp
jl 0x8b4f
movl %r13d, %ebx
movq 0x20(%r14), %rdx
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *(%rax)
addq $0x28, %r14
decq %rbx
jne 0x8b37
movq 0xc46a(%rip), %r14      # 0x14fc0
leaq 0x593e(%rip), %rsi      # 0xe49b
movl $0x5, %edx
movq %r14, %rdi
callq 0x2210
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
leaq 0x10(%rsp), %rbx
leaq 0x30(%rsp), %r15
movq %rbx, (%r15)
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%r15)
movl %r13d, 0x10(%r15)
movaps 0x54ae(%rip), %xmm0     # 0xe060
movups %xmm0, 0x14(%r15)
movl (%rsp), %ebp
movl %ebp, 0x24(%r15)
leaq 0x20(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x22e0
xorps %xmm0, %xmm0
cvtsi2sdq (%r12), %xmm0
cvtsi2sdq 0x8(%r12), %xmm2
divsd 0x54b3(%rip), %xmm2     # 0xe098
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movq (%rbx), %rdi
movslq %ebp, %rsi
movb $0x0, (%r12)
leaq 0x20(%rsp), %rcx
movq %r15, %rdx
callq 0xd754
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x22e0
xorps %xmm0, %xmm0
cvtsi2sdq (%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x8(%rbx), %xmm1
movsd 0x5469(%rip), %xmm2     # 0xe098
divsd %xmm2, %xmm1
addsd %xmm0, %xmm1
subsd 0x8(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
divsd %xmm1, %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
leaq 0x5604(%rip), %rsi      # 0xe25d
movl $0x7, %edx
movq %r14, %rdi
callq 0x2210
leaq 0x5834(%rip), %rsi      # 0xe4a1
movl $0x14, %edx
movq %r14, %rdi
callq 0x2210
movq %r14, %rdi
movsd (%rsp), %xmm0
callq 0x22d0
movq %rax, %rbx
leaq 0x5606(%rip), %rsi      # 0xe297
movl $0x4, %edx
movq %rax, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
leaq 0x57ea(%rip), %rsi      # 0xe4b6
movl $0x32, %edx
movq %r14, %rdi
callq 0x2210
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop