Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/benchmark/benchmark_ffis.cpp
4133 views
1
// Copyright 2017 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <memory.h>
7
#include <string.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <algorithm>
11
12
#ifdef __EMSCRIPTEN__
13
#include <emscripten/emscripten.h>
14
#endif
15
16
#include "tick.h"
17
18
// #define BENCHMARK_FOREIGN_FUNCTION
19
20
#if defined(BENCHMARK_FOREIGN_FUNCTION) && defined(__EMSCRIPTEN__)
21
extern "C"
22
{
23
int foreignFunctionThatTakesThreeParameters(int a, int b, int c);
24
}
25
#else
26
int foreignCounter = 0;
27
int __attribute__((noinline)) foreignFunctionThatTakesThreeParameters(int a, int b, int c)
28
{
29
foreignCounter += a + b + c;
30
return foreignCounter;
31
}
32
#endif
33
typedef int (*FuncPtrType)(int, int, int);
34
FuncPtrType pointerToFunction = 0;
35
36
int numRunsDone = 0;
37
const int totalRuns = 1000;
38
tick_t accumulatedTicks = 0;
39
tick_t allTicks[totalRuns] = {};
40
41
double averageBestPercentileMsecs(double p)
42
{
43
tick_t acc = 0;
44
int numSamples = (int)(totalRuns*p);
45
for(int i = 0; i < numSamples; ++i)
46
acc += allTicks[i];
47
return acc * 1000.0 / numSamples / ticks_per_sec();
48
}
49
50
double averageWorstPercentileMsecs(double p)
51
{
52
tick_t acc = 0;
53
int numSamples = (int)(totalRuns*p);
54
for(int i = 0; i < numSamples; ++i)
55
acc += allTicks[totalRuns-1-i];
56
return acc * 1000.0 / numSamples / ticks_per_sec();
57
}
58
59
int counter = 0;
60
void __attribute__((noinline)) main_loop()
61
{
62
tick_t t0 = tick();
63
for(int i = 0; i < 500000; ++i)
64
{
65
#if BENCHMARK_FUNCTION_POINTER
66
counter += pointerToFunction(i, i+1, i+2);
67
#else
68
counter += foreignFunctionThatTakesThreeParameters(i, i+1, i+2);
69
#endif
70
}
71
tick_t t1 = tick();
72
allTicks[numRunsDone] = t1 - t0;
73
++numRunsDone;
74
accumulatedTicks += t1 - t0;
75
printf("Run %d: %f msecs.\n", numRunsDone, (t1 - t0) * 1000.0 / ticks_per_sec());
76
if (numRunsDone >= totalRuns)
77
{
78
double accumulatedTimeMsecs = accumulatedTicks * 1000.0 / ticks_per_sec();
79
std::sort(allTicks, allTicks + totalRuns);
80
printf("Total: %d runs, avg. %f msecs per run. Counter: %d.\n", totalRuns, accumulatedTimeMsecs / totalRuns, counter);
81
printf("Best run: %f msecs.\n", allTicks[0] * 1000.0 / ticks_per_sec());
82
printf("1%% Best percentile: %f msecs. 1%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.01), averageWorstPercentileMsecs(0.01));
83
printf("5%% Best percentile: %f msecs. 5%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.05), averageWorstPercentileMsecs(0.05));
84
printf("10%% Best percentile: %f msecs. 10%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.10), averageWorstPercentileMsecs(0.10));
85
printf("25%% Best percentile: %f msecs. 25%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.25), averageWorstPercentileMsecs(0.25));
86
printf("50%% Best percentile: %f msecs. 50%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.50), averageWorstPercentileMsecs(0.50));
87
printf("75%% Best percentile: %f msecs. 75%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.75), averageWorstPercentileMsecs(0.75));
88
printf("95%% Best percentile: %f msecs. 95%% Worst percentile: %f msecs.\n", averageBestPercentileMsecs(0.95), averageWorstPercentileMsecs(0.95));
89
printf("100%% Best/Worst percentile (==average over all samples): %f msecs.\n", averageBestPercentileMsecs(1.00));
90
91
printf("Median run: %f msecs.\n", allTicks[totalRuns/2] * 1000.0 / ticks_per_sec());
92
printf("Total time: %f\n", accumulatedTimeMsecs);
93
#ifdef __EMSCRIPTEN__
94
emscripten_cancel_main_loop();
95
#endif
96
exit(0);
97
}
98
}
99
100
int main()
101
{
102
// Insist dynamic initialization that the compiler can't possibly optimize away.
103
pointerToFunction = (tick() == 0 && tick() == 1000000) ? 0 : &foreignFunctionThatTakesThreeParameters;
104
105
#if defined(__EMSCRIPTEN__) && !defined(BUILD_FOR_SHELL)
106
emscripten_set_main_loop(main_loop, 0, 0);
107
#else
108
for(int i = 0; i < totalRuns; ++i)
109
main_loop();
110
#endif
111
}
112
113