plugins/testsuite/benchmark.sp
author David Anderson <dvander@alliedmods.net>
Sun Oct 28 08:24:47 2007 +0000 (2007-10-28)
changeset 1649 37373aea4dfe
parent 914 cd241af51653
permissions -rw-r--r--
fixed macro bug in the sdk
     1 #include <sourcemod>
     2 #include <profiler>
     3 
     4 public Plugin:myinfo = 
     5 {
     6 	name = "Benchmarks",
     7 	author = "AlliedModders LLC",
     8 	description = "Basic benchmarks",
     9 	version = "1.0.0.0",
    10 	url = "http://www.sourcemod.net/"
    11 };
    12 
    13 #define MATH_INT_LOOPS		2000
    14 #define MATH_FLOAT_LOOPS	2000
    15 #define STRING_OP_LOOPS		2000
    16 #define STRING_FMT_LOOPS	2000
    17 #define STRING_ML_LOOPS		2000
    18 #define STRING_RPLC_LOOPS	2000
    19 
    20 new Float:g_dict_time
    21 new Handle:g_Prof = INVALID_HANDLE
    22 
    23 public OnPluginStart()
    24 {
    25 	RegServerCmd("bench", Benchmark);
    26 	g_Prof = CreateProfiler();
    27 	StartProfiling(g_Prof);
    28 	LoadTranslations("fakedict-sourcemod.cfg");
    29 	StopProfiling(g_Prof);
    30 	g_dict_time = GetProfilerTime(g_Prof);
    31 }
    32 
    33 public Action:Benchmark(args)
    34 {
    35 	PrintToServer("dictionary time: %f seconds", g_dict_time);
    36 	StringBench();
    37 	MathBench();
    38 	return Plugin_Handled;
    39 }
    40 
    41 MathBench()
    42 {
    43 	StartProfiling(g_Prof);
    44 	new iter = MATH_INT_LOOPS;
    45 	new a, b, c;
    46 	while(iter--)
    47 	{
    48 		a = iter * 7;
    49 		b = 5 + iter;
    50 		c = 6 / (iter + 3);
    51 		a = 6 * (iter);
    52 		b = a * 185;
    53 		a = b / 25;
    54 		c = b - a + 3;
    55 		b = b*b;
    56 		a = (a + c) / (b - c);
    57 		b = 6;
    58 		c = 1;
    59 		b = a * 128 - c;
    60 		c = b * (a + 16) * b;
    61 		if (!a)
    62 		{
    63 			a = 5;
    64 		}
    65 		a = c + (28/a) - c;
    66 	}
    67 	StopProfiling(g_Prof);
    68 	PrintToServer("int benchmark: %f seconds", GetProfilerTime(g_Prof));
    69 
    70 	StartProfiling(g_Prof);	
    71 	new Float:fa, Float:fb, Float:fc
    72 	new int1
    73 	iter = MATH_FLOAT_LOOPS;
    74 	while (iter--)
    75 	{
    76 		fa = iter * 0.7;
    77 		fb = 5.1 + iter;
    78 		fc = 6.1 / (float(iter) + 2.5);
    79 		fa = 6.1 * (iter);
    80 		fb = fa * 185.26;
    81 		fa = fb / 25.56;
    82 		fc = fb - a + float(3);
    83 		fb = fb*fb;
    84 		fa = (fa + fc) / (fb - fc);
    85 		fb = 6.2;
    86 		fc = float(1);
    87 		int1 = RoundToNearest(fa);
    88 		fb = fa * float(128) - int1;
    89 		fc = fb * (a + 16.85) * float(RoundToCeil(fb));
    90 		if (fa == 0.0)
    91 		{
    92 			fa = 5.0;
    93 		}
    94 		fa = fc + (float(28)/fa) - RoundToFloor(fc);
    95 	}
    96 	StopProfiling(g_Prof);
    97 	PrintToServer("float benchmark: %f seconds", GetProfilerTime(g_Prof));
    98 }
    99 
   100 #define KEY1	"LVWANBAGVXSXUGB"
   101 #define KEY2	"IDYCVNWEOWNND"
   102 #define KEY3	"UZWTRNHY"
   103 #define KEY4	"EPRHAFCIUOIG"
   104 #define KEY5	"RMZCVWIEY"
   105 #define KEY6	"ZHPU"
   106 
   107 StringBench()
   108 {
   109 	new i = STRING_FMT_LOOPS;
   110 	new String:buffer[255];
   111 	
   112 	StartProfiling(g_Prof);
   113 	new end
   114 	while (i--)
   115 	{
   116 		end = 0;
   117 		Format(buffer, sizeof(buffer), "%d", i);
   118 		Format(buffer, sizeof(buffer), "%d %s %d %f %d %-3.4s %s", i, "gaben", 30, 10.0, 20, "hello", "What a gaben");
   119 		end = Format(buffer, sizeof(buffer), "Well, that's just %-17.18s!", "what.  this isn't a valid string! wait it is");
   120 		end += Format(buffer[end], sizeof(buffer)-end, "There are %d in this %d", i, end);
   121 		end += Format(buffer[end], sizeof(buffer)-end, "There are %d in this %d", i, end);
   122 	}
   123 	StopProfiling(g_Prof);
   124 	PrintToServer("format() benchmark: %f seconds", GetProfilerTime(g_Prof));
   125 	
   126 	StartProfiling(g_Prof);
   127 	i = STRING_ML_LOOPS;
   128 	new String:fmtbuf[2048];	/* don't change to decl, amxmodx doesn't use it */
   129 	while (i--)
   130 	{
   131 		Format(fmtbuf, 2047, "%T %T %d %s %f %T", KEY1, LANG_SERVER, KEY2, LANG_SERVER, 50, "what the", 50.0, KEY3, LANG_SERVER);
   132 		Format(fmtbuf, 2047, "%s %T %s %T %T", "gaben", KEY4, LANG_SERVER, "what TIME is it", KEY5, LANG_SERVER, KEY6, LANG_SERVER);
   133 	}
   134 	StopProfiling(g_Prof);
   135 	PrintToServer("ml benchmark: %f seconds", GetProfilerTime(g_Prof));
   136 	
   137 	StartProfiling(g_Prof);
   138 	i = STRING_OP_LOOPS;
   139 	while (i--)
   140 	{
   141 		StringToInt(fmtbuf)
   142 	}
   143 	StopProfiling(g_Prof);
   144 	PrintToServer("str benchmark: %f seconds", GetProfilerTime(g_Prof));
   145 	
   146 	StartProfiling(g_Prof);
   147 	i = STRING_RPLC_LOOPS;
   148 	while (i--)
   149 	{
   150 		strcopy(fmtbuf, 2047, "This is a test string for you.");
   151 		ReplaceString(fmtbuf, sizeof(fmtbuf), " ", "ASDF")
   152 		ReplaceString(fmtbuf, sizeof(fmtbuf), "SDF", "")
   153 		ReplaceString(fmtbuf, sizeof(fmtbuf), "string", "gnirts")
   154 	}
   155 	StopProfiling(g_Prof);
   156 	PrintToServer("replace benchmark: %f seconds", GetProfilerTime(g_Prof));
   157 }