Path: blob/main/test/benchmark/benchmark_ffis.cpp
4133 views
// Copyright 2017 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45#include <memory.h>6#include <string.h>7#include <stdio.h>8#include <stdlib.h>9#include <algorithm>1011#ifdef __EMSCRIPTEN__12#include <emscripten/emscripten.h>13#endif1415#include "tick.h"1617// #define BENCHMARK_FOREIGN_FUNCTION1819#if defined(BENCHMARK_FOREIGN_FUNCTION) && defined(__EMSCRIPTEN__)20extern "C"21{22int foreignFunctionThatTakesThreeParameters(int a, int b, int c);23}24#else25int foreignCounter = 0;26int __attribute__((noinline)) foreignFunctionThatTakesThreeParameters(int a, int b, int c)27{28foreignCounter += a + b + c;29return foreignCounter;30}31#endif32typedef int (*FuncPtrType)(int, int, int);33FuncPtrType pointerToFunction = 0;3435int numRunsDone = 0;36const int totalRuns = 1000;37tick_t accumulatedTicks = 0;38tick_t allTicks[totalRuns] = {};3940double averageBestPercentileMsecs(double p)41{42tick_t acc = 0;43int numSamples = (int)(totalRuns*p);44for(int i = 0; i < numSamples; ++i)45acc += allTicks[i];46return acc * 1000.0 / numSamples / ticks_per_sec();47}4849double averageWorstPercentileMsecs(double p)50{51tick_t acc = 0;52int numSamples = (int)(totalRuns*p);53for(int i = 0; i < numSamples; ++i)54acc += allTicks[totalRuns-1-i];55return acc * 1000.0 / numSamples / ticks_per_sec();56}5758int counter = 0;59void __attribute__((noinline)) main_loop()60{61tick_t t0 = tick();62for(int i = 0; i < 500000; ++i)63{64#if BENCHMARK_FUNCTION_POINTER65counter += pointerToFunction(i, i+1, i+2);66#else67counter += foreignFunctionThatTakesThreeParameters(i, i+1, i+2);68#endif69}70tick_t t1 = tick();71allTicks[numRunsDone] = t1 - t0;72++numRunsDone;73accumulatedTicks += t1 - t0;74printf("Run %d: %f msecs.\n", numRunsDone, (t1 - t0) * 1000.0 / ticks_per_sec());75if (numRunsDone >= totalRuns)76{77double accumulatedTimeMsecs = accumulatedTicks * 1000.0 / ticks_per_sec();78std::sort(allTicks, allTicks + totalRuns);79printf("Total: %d runs, avg. %f msecs per run. Counter: %d.\n", totalRuns, accumulatedTimeMsecs / totalRuns, counter);80printf("Best run: %f msecs.\n", allTicks[0] * 1000.0 / ticks_per_sec());81printf("1%% Best percentile: %f msecs. 1%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.01), averageWorstPercentileMsecs(0.01));82printf("5%% Best percentile: %f msecs. 5%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.05), averageWorstPercentileMsecs(0.05));83printf("10%% Best percentile: %f msecs. 10%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.10), averageWorstPercentileMsecs(0.10));84printf("25%% Best percentile: %f msecs. 25%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.25), averageWorstPercentileMsecs(0.25));85printf("50%% Best percentile: %f msecs. 50%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.50), averageWorstPercentileMsecs(0.50));86printf("75%% Best percentile: %f msecs. 75%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.75), averageWorstPercentileMsecs(0.75));87printf("95%% Best percentile: %f msecs. 95%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.95), averageWorstPercentileMsecs(0.95));88printf("100%% Best/Worst percentile (==average over all samples): %f msecs.\n", averageBestPercentileMsecs(1.00));8990printf("Median run: %f msecs.\n", allTicks[totalRuns/2] * 1000.0 / ticks_per_sec());91printf("Total time: %f\n", accumulatedTimeMsecs);92#ifdef __EMSCRIPTEN__93emscripten_cancel_main_loop();94#endif95exit(0);96}97}9899int main()100{101// Insist dynamic initialization that the compiler can't possibly optimize away.102pointerToFunction = (tick() == 0 && tick() == 1000000) ? 0 : &foreignFunctionThatTakesThreeParameters;103104#if defined(__EMSCRIPTEN__) && !defined(BUILD_FOR_SHELL)105emscripten_set_main_loop(main_loop, 0, 0);106#else107for(int i = 0; i < totalRuns; ++i)108main_loop();109#endif110}111112113