diff --git a/src/lib/newchecksum.dasl b/src/lib/newchecksum.dasl index 19ff4c78c7..6487e27c9f 100644 --- a/src/lib/newchecksum.dasl +++ b/src/lib/newchecksum.dasl @@ -146,12 +146,15 @@ function selftest () end local function benchmark (fn, times) local now = os.clock() - local temp + local csum for i=1,times do - temp = fn() + csum, pkt = fn() end - local ret = {os.clock() - now, temp} - return ret[1] + local elapse = os.clock() - now + local ns_per_byte = elapse * 10e9 / pkt.length + local ns_per_csum = elapse * 10e9 / times + local ret = {elapse = elapse, ns_per_byte = ns_per_byte, ns_per_csum = ns_per_csum, csum = csum} + return ("elapse: %.6f; ns_per_byte: %.2f; ns_per_csum: %.2f"):format(ret.elapse, ret.ns_per_byte, ret.ns_per_csum) end local function hex (num) return ("0x%.2x"):format(num) @@ -167,10 +170,10 @@ function selftest () assert(hex(ntohs(newchecksum(pkt.data, pkt.length))) == hex(ntohs(checksum_lua(pkt.data, pkt.length)))) assert(hex(ntohs(newchecksum(pkt.data, pkt.length))) == hex(C.cksum_generic(pkt.data, pkt.length, 0))) -- Benchmark for different architectures. - print("Gen: ", benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0) end, times)) - print("SSE2: ", benchmark(function() return C.cksum_sse2(pkt.data, pkt.length, 0) end, times)) - print("AVX2: ", benchmark(function() return C.cksum_avx2(pkt.data, pkt.length, 0) end, times)) - print("New: ", benchmark(function() return newchecksum(pkt.data, pkt.length) end, times)) + print("Gen: ", benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0), pkt end, times)) + print("SSE2: ", benchmark(function() return C.cksum_sse2(pkt.data, pkt.length, 0), pkt end, times)) + print("AVX2: ", benchmark(function() return C.cksum_avx2(pkt.data, pkt.length, 0), pkt end, times)) + print("New: ", benchmark(function() return newchecksum(pkt.data, pkt.length), pkt end, times)) size = 550 print("2M; "..size.." bytes") @@ -180,10 +183,10 @@ function selftest () assert(hex(ntohs(newchecksum(pkt.data, pkt.length))) == hex(ntohs(checksum_lua(pkt.data, pkt.length)))) assert(hex(ntohs(newchecksum(pkt.data, pkt.length))) == hex(C.cksum_generic(pkt.data, pkt.length, 0))) -- Benchmark for different architectures. - print("Gen: ", benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0) end, times)) - print("SSE2: ", benchmark(function() return C.cksum_sse2(pkt.data, pkt.length, 0) end, times)) - print("AVX2: ", benchmark(function() return C.cksum_avx2(pkt.data, pkt.length, 0) end, times)) - print("New: ", benchmark(function() return newchecksum(pkt.data, pkt.length) end, times)) + print("Gen: ", benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0), pkt end, times)) + print("SSE2: ", benchmark(function() return C.cksum_sse2(pkt.data, pkt.length, 0), pkt end, times)) + print("AVX2: ", benchmark(function() return C.cksum_avx2(pkt.data, pkt.length, 0), pkt end, times)) + print("New: ", benchmark(function() return newchecksum(pkt.data, pkt.length), pkt end, times)) size = 1500 print("1M; "..size.." bytes") @@ -193,8 +196,8 @@ function selftest () assert(hex(ntohs(newchecksum(pkt.data, pkt.length))) == hex(ntohs(checksum_lua(pkt.data, pkt.length)))) assert(hex(ntohs(newchecksum(pkt.data, pkt.length))) == hex(C.cksum_generic(pkt.data, pkt.length, 0))) -- Benchmark for different architectures. - print("Gen: ", benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0) end, times)) - print("SSE2: ", benchmark(function() return C.cksum_sse2(pkt.data, pkt.length, 0) end, times)) - print("AVX2: ", benchmark(function() return C.cksum_avx2(pkt.data, pkt.length, 0) end, times)) - print("New: ", benchmark(function() return newchecksum(pkt.data, pkt.length) end, times)) + print("Gen: ", benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0), pkt end, times)) + print("SSE2: ", benchmark(function() return C.cksum_sse2(pkt.data, pkt.length, 0), pkt end, times)) + print("AVX2: ", benchmark(function() return C.cksum_avx2(pkt.data, pkt.length, 0), pkt end, times)) + print("New: ", benchmark(function() return newchecksum(pkt.data, pkt.length), pkt end, times)) end