diff --git a/benchmarks/benchmarks.cpp b/benchmarks/benchmarks.cpp index e391dfcf..902068bd 100644 --- a/benchmarks/benchmarks.cpp +++ b/benchmarks/benchmarks.cpp @@ -4,40 +4,25 @@ #include #include #include +#include + +#define MAX_LOOPS 500000000 +#define MAX_MATCHES 10 int main(){ - int sizes[]= { 16000, 32000, 64000, 120000, 1600000, 2000000, 2500000, 3500000, 150000000, 250000000, 350000000, 500000000}; - int f_loops[]= { 70000, 50000, 30000, 10000, 1000, 1000, 1000, 1000, 7, 7, 5, 3}; - int t_loops[]= { 200000, 150000, 100000, 70000, 5000, 5000, 5000, 5000, 50, 50, 50, 50}; - int exp_len[]= { 15, 15, 15, 15, 5, 5, 5, 5, 5, 5, 5, 5}; - int nd_loops[]= { 250000, 150000, 100000, 100000, 10000, 1000, 1000, 1000, 100, 100, 100, 100}; - const char charset[] = "aAaAaAaAAAaaaaAAAAaaaaAAAAAAaaaAAaaa"; - std::string labels[] = {"\x1B[33m Benchmarks(kbytes) \x1B[0m\n", "\x1B[33m Benchmarks(kbytes) \x1B[0m\n", - "\x1B[33m Benchmarks(kbytes) \x1B[0m\n", "\x1B[33m Benchmarks(kbytes) \x1B[0m\n", - "\x1B[33m Benchmarks(Mbytes) \x1B[0m\n", "\x1B[33m Benchmarks(Mbytes) \x1B[0m\n", - "\x1B[33m Benchmarks(Mbytes) \x1B[0m\n", "\x1B[33m Benchmarks(Mbytes) \x1B[0m\n", - "\x1B[33m Benchmarks(Gbytes) \x1B[0m\n", "\x1B[33m Benchmarks(Gbytes) \x1B[0m\n", - "\x1B[33m Benchmarks(Gbytes) \x1B[0m\n", "\x1B[33m Benchmarks(Gbytes) \x1B[0m\n" - }; - std::function functions[] = { shufti_benchmarks, rshufti_benchmarks, truffle_benchmarks, rtruffle_benchmarks }; - for (int i=11; i<12; i++) { - std::cout << labels[i]; - for(int j=0; j<4; j++){ - functions[j](sizes[i],f_loops[i],exp_len[i],false); - functions[j](sizes[i],t_loops[i],exp_len[i],true); + int sizes[] = { 16000, 32000, 64000, 120000, 1600000, 2000000, 2500000, 3500000, 150000000, 250000000, 350000000, 500000000 }; + const char charset[] = "aAaAaAaAAAaaaaAAAAaaaaAAAAAAaaaAAaaa"; + /* + for (size_t i = 0; i < std::size(sizes); i++) { + for(int j = 0; j < 4; j++) { + functions[j](sizes[i], MAX_LOOPS / sizes[i], MAX_MATCHES, false); + functions[j](sizes[i], MAX_LOOPS / sizes[i], MAX_MATCHES, true); } } - for(int i=0; i<12; i++){ - if(i==0){ - std::cout<(end - start).count(); + total_sec += std::chrono::duration_cast(end - start).count(); /*calculate transferred size*/ - trans_size = size * M; - /*convert to sec*/ - bw = trans_size / total_sec; + transferred_size = size * loops; + /*calculate average time*/ + avg_time = total_sec / loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; + /*calculate maximum bandwidth*/ + max_bw = transferred_size / total_sec; /*convert to MB/s*/ - bw /=1048576; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout << "\x1B[35m Case with match in random pos and size: "<< size <<" lit_len: "<< lit_len <<" nocase: "<< (int)nocase - << "\x1B[36m noodExec elapsetime: \x1B[0m" << (ms/M) << " (μs) \x1B[36m bandwidth: \x1B[0m" << bw <<" (MB/s)" << std::endl; + max_bw /=1048576.0; + /*calculate average bandwidth*/ + bandwitdh = max_bw / loops; + printf(KMAG "Case with %u matches in random pos with %u * %u iterations," KBLU " total elapsed time =" RST " %.3f s, " + KBLU "average time per call =" RST " %.3f μs," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n", + lit_len, size ,loops, total_sec, avg_time, max_bw, bandwitdh); delete [] data; } \ No newline at end of file diff --git a/benchmarks/shufti.cpp b/benchmarks/shufti.cpp index ce576a0f..85a45e21 100644 --- a/benchmarks/shufti.cpp +++ b/benchmarks/shufti.cpp @@ -7,142 +7,144 @@ #include #include -/* -#define RST "\x1B[0m" -#define KRED "\x1B[31m" -#define KGRN "\x1B[32m" -#define KYEL "\x1B[33m" -#define KBLU "\x1B[34m" -#define KMAG "\x1B[35m" -#define KCYN "\x1B[36m" -#define KWHT "\x1B[37m" -*/ - - void shufti_benchmarks(int size, int loops, int M, bool has_match) { - size_t real_size = size; m128 lo, hi; ue2::CharReach chars; chars.set('a'); int ret = shuftiBuildMasks(chars, (u8 *)&lo, (u8 *)&hi); u8 *kt1 = new u8[size]; memset(kt1,'b',size); - long double total_sec = 0; - long double trans_size = 0; - long double bw = 0; - if (has_match){ + double total_sec = 0.0; + u64a transferred_size = 0; + double bandwitdh = 0.0; + double max_bw = 0.0; + double avg_time = 0.0; + if (has_match) { int pos = 0; - for(int j=0; j(end - start).count(); + double dt = std::chrono::duration_cast(end - start).count(); total_sec += dt; - /*calculate transferred size*/ - trans_size += act_size * loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; /*calculate bandwidth*/ - bw += trans_size / total_sec; + bandwitdh += act_size / total_sec; /*convert to MB/s*/ - bw += bw / 1048576; - /*calculte average time*/ - total_sec /= loops; + bandwitdh = bandwitdh / 1048576.0; + max_bw = std::max(bandwitdh ,max_bw); + /*calculate average time*/ + avg_time += total_sec / loops; } - total_sec /= M; - bw /= M; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout << "\x1B[35m Case with match in random pos and size: " << real_size << " for " << loops<<" loops (" - << M << " random possisions checked): \x1B[36m shuftiExec elapsetime: \x1B[0m" << (ms/M) - <<" (μs) \x1B[36m bandwidth: \x1B[0m" << bw <<" (MB/s)"<(end - start).count(); + total_sec += std::chrono::duration_cast(end - start).count(); /*calculate transferred size*/ - trans_size = size * loops ; - /*calculate bandwidth*/ - bw = trans_size / total_sec; + transferred_size = size * loops; + /*calculate average time*/ + avg_time = total_sec / loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; + /*calculate maximum bandwidth*/ + max_bw = transferred_size / total_sec; /*convert to MB/s*/ - bw /=1048576; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout<<"\x1B[35m Case with no match in random pos and size: "<(end - start).count(); + double dt = std::chrono::duration_cast(end - start).count(); total_sec += dt; - /*calculate transferred size*/ - trans_size += act_size * loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; /*calculate bandwidth*/ - bw += trans_size / total_sec; + bandwitdh += act_size / total_sec; /*convert to MB/s*/ - bw += bw / 1048576; - /*calculte average time*/ - total_sec /= loops; + bandwitdh = bandwitdh / 1048576.0; + max_bw = std::max(bandwitdh ,max_bw); + /*calculate average time*/ + avg_time += total_sec / loops; } - total_sec /= M; - bw /= M; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout << "\x1B[35m Case with match in random pos and size: " << real_size << " for " << loops<<" loops (" - << M << " random possisions checked): \x1B[36m rshuftiExec elapsetime: \x1B[0m" << (ms/M) - <<" (μs) \x1B[36m bandwidth: \x1B[0m" << bw <<" (MB/s)"<(end - start).count(); + total_sec += std::chrono::duration_cast(end - start).count(); /*calculate transferred size*/ - trans_size = size * loops ; - /*calculate bandwidth*/ - bw = trans_size / total_sec; + transferred_size = size * loops; + /*calculate average time*/ + avg_time = total_sec / loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; + /*calculate maximum bandwidth*/ + max_bw = transferred_size / total_sec; /*convert to MB/s*/ - bw /=1048576; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout<<"\x1B[35m Case with no match in random pos and size: "< #include -/* -#define RST "\x1B[0m" -#define KRED "\x1B[31m" -#define KGRN "\x1B[32m" -#define KYEL "\x1B[33m" -#define KBLU "\x1B[34m" -#define KMAG "\x1B[35m" -#define KCYN "\x1B[36m" -#define KWHT "\x1B[37m" -*/ - - void truffle_benchmarks(int size, int loops, int M, bool has_match) { - size_t real_size = size; - m128 lo, hi; - ue2::CharReach chars; - chars.set('a'); - truffleBuildMasks(chars, (u8 *)&lo, (u8 *)&hi); - u8*kt1 = new u8[size]; - memset(kt1,'b',size); - long double total_sec = 0; - long double trans_size = 0; - long double bw = 0; - if (has_match){ - int pos = 0; - for(int j=0; j(end - start).count(); - total_sec += dt; - /*calculate transferred size*/ - trans_size += act_size * loops; - /*calculate bandwidth*/ - bw += trans_size / total_sec; - /*convert to MB/s*/ - bw += bw / 1048576; - /*calculte average time*/ - total_sec /= loops; - } - total_sec /= M; - bw /= M; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout << "\x1B[35m Case with match in random pos and size: " << real_size << " for "<< loops <<" loops (" - << M <<" random possisions checked): \x1B[36m truffleExec elapsetime: \x1B[0m" << (ms/M) - << "(μs) \x1B[36m bandwidth: \x1B[0m"<< bw << "(MB/μs)" <(end - start).count(); - /*calculate transferred size*/ - trans_size = size * loops ; - /*calculate bandwidth*/ - bw = trans_size / total_sec; - /*convert to MB/s*/ - bw /=1048576; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout<<"\x1B[35m Case with no match in random pos and size: "<(end - start).count(); + total_sec += dt; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; + /*calculate bandwidth*/ + bandwitdh += act_size / total_sec; + /*convert to MB/s*/ + bandwitdh = bandwitdh / 1048576.0; + max_bw = std::max(bandwitdh ,max_bw); + /*calculate average time*/ + avg_time += total_sec / loops; + } + avg_time /= M; + bandwitdh /= M; + /*convert average time to us*/ + avg_time *= 1000000.0; + printf(KMAG "TruffleExec: case with %u matches, %u * %u iterations," KBLU " total elapsed time =" RST " %.3f s, " + KBLU "average time per call =" RST " %.3f μs," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n", + M, size ,loops, total_sec, avg_time, max_bw, bandwitdh); + } else { + auto start = std::chrono::steady_clock::now(); + for (int i = 0; i < loops; i++) { + truffleExec(lo, hi, kt1, kt1 + size); + } + auto end = std::chrono::steady_clock::now(); + total_sec += std::chrono::duration_cast(end - start).count(); + /*calculate transferred size*/ + transferred_size = size * loops; + /*calculate average time*/ + avg_time = total_sec / loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; + /*calculate maximum bandwidth*/ + max_bw = transferred_size / total_sec; + /*convert to MB/s*/ + max_bw /=1048576.0; + /*calculate average bandwidth*/ + bandwitdh = max_bw / loops; + printf(KMAG "TruffleExec case without matches, %u * %u iterations," KBLU " total elapsed time =" RST " %.3f s, " + KBLU "average time per call =" RST " %.3f μs ," KBLU " bandwidth = " RST " %.3f MB/s," KBLU " average bandwidth =" RST " %.3f MB/s \n", + size ,loops, total_sec, avg_time, max_bw, bandwitdh); + } + delete [] kt1; +} + + +void rtruffle_benchmarks(int size, int loops, int M, bool has_match) { + m128 lo, hi; + ue2::CharReach chars; + chars.set('a'); + truffleBuildMasks(chars, (u8 *)&lo, (u8 *)&hi); + u8 *kt1 = new u8[size]; + memset(kt1,'b',size); + double total_sec = 0.0; + u64a transferred_size = 0; + double bandwitdh = 0.0; + double max_bw = 0.0; + double avg_time = 0.0; + if (has_match) { + int pos = 0; + for(int j = 0; j < M; j++) { + kt1[pos] = 'b'; + srand (time(NULL)); + pos = rand() % size + 0; + kt1[pos] = 'a'; + unsigned long act_size = 0; + auto start = std::chrono::steady_clock::now(); + for(int i = 0; i < loops; i++) { const u8 *res = rtruffleExec(lo, hi, kt1, kt1 + size); act_size += res - kt1; } auto end = std::chrono::steady_clock::now(); - long double dt = std::chrono::duration_cast(end - start).count(); + double dt = std::chrono::duration_cast(end - start).count(); total_sec += dt; - /*calculate transferred size*/ - trans_size += act_size * loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; /*calculate bandwidth*/ - bw += trans_size / total_sec; + bandwitdh += act_size / total_sec; /*convert to MB/s*/ - bw += bw / 1048576; - /*calculte average time*/ - total_sec /= loops; + bandwitdh = bandwitdh / 1048576.0; + max_bw = std::max(bandwitdh ,max_bw); + /*calculate average time*/ + avg_time += total_sec / loops; } - total_sec /= M; - bw /= M; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout<<"\x1B[35m Case with match in random pos and size: "<< real_size <<" for "<(end - start).count(); + total_sec += std::chrono::duration_cast(end - start).count(); /*calculate transferred size*/ - trans_size = size * loops ; - /*calculate bandwidth*/ - bw = trans_size / total_sec; + transferred_size = size * loops; + /*calculate average time*/ + avg_time = total_sec / loops; + /*convert microseconds to seconds*/ + total_sec /= 1000000.0; + /*calculate maximum bandwidth*/ + max_bw = transferred_size / total_sec; /*convert to MB/s*/ - bw /=1048576; - /*covert average time to μs*/ - long double ms = total_sec * 1000000; - std::cout<<"\x1B[35m Case with no match in random pos and size: "<