summaryrefslogtreecommitdiff
path: root/recipes/cpubench/files/openwrt_cpu_bench_v06.c
blob: dfb05a8360e65c6dd1a39442e79f5e176a629af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>




#define VERSION_STR "v0.6"

// This is the overhead of calling gettimeofday() 2 times
double overhead = -1;

double begin_secs = 0;
double real_get_seconds()
{
	struct timeval tv;
	double total;

	gettimeofday(&tv, NULL);
	total = (double)tv.tv_sec;
	total += ((double)tv.tv_usec)/(double)1000000.0;
	
	//printf("Secs: %d / Usecs: %d / FSecs: %f\n", tv.tv_sec, tv.tv_usec, total);

	return total;
}
double get_seconds()
{
	if (begin_secs == 0)
	{
		// First call
		begin_secs = real_get_seconds();
	}

	return (real_get_seconds() - begin_secs);
}


double run_float_bench()
{
	double secs;
	double begin = get_seconds();
	int i;

	double nb=0;
	for (i=0;i<300000;i++)
	{
		nb *= i;
		int n = i >> 10;
		nb /= n;
	}
	
	double end = get_seconds();
	secs = end-begin;
	printf("Time to run float bench: %.2f[secs]\n", secs);

	return secs;
}


double do_run_memory_bench()
{
	double begin, end;
	int i;
	int* buf;
	int len, index;
	double secs;

       	begin = get_seconds();
       
	len = (1<<21)/sizeof(int); // 2Mb
	buf = malloc(len*sizeof(int));

	// Write to memory - sequential
	for (i=0;i<len;i++)
	{
		buf[i] = i;
	}

	// Read memory - sequential
	for (i=0;i<len;i++)
	{
		int a = buf[i];
	}

	// Read memory - random
	for (i=0;i<len;i++)
	{
		index = (i*23)%(len/2);
		int a = buf[index];
	}

	// Write memory - random
	for (i=0;i<len;i++)
	{
		index = (i*23)%(len/2);
		buf[index] = i;
	}
	free(buf);
	
	end = get_seconds();
	secs = (end-begin);

	return secs;
}

double run_memory_bench()
{
	double secs = 0;
	int i;

	for (i=0;i<10;i++)
	{
		secs += do_run_memory_bench();
	}

	printf("Time to run memory bench: %.2f[secs]\n", secs);

	return secs;
}

#define NBD 9009
double run_compute_e()
{
	double begin, end;
	double secs;

	begin = get_seconds();

	int N=NBD, n=N, a[NBD],x;
	while(--n)
	{
		a[n]=1+1/n;
	}

	for(;N>9;)
	{
		for(n=N--;--n; a[n]=x%n, x=10*a[n-1]+x/n)
		{
		}
	}

	end = get_seconds();

	secs = (end-begin);
	printf("Time to run computation of e (%d digits): %.2f[secs]\n", NBD, secs);

	return secs;
}

double run_compute_pi()
{
	double begin, end;
	double secs;
	int i;

	begin = get_seconds();
	//printf("Begin: %f\n", begin);

	for (i=0;i<10;i++)
	{
		int a=10000,b=0,c=8400,d=0,e=0,f[8401],g=0;

		//printf("i:%d\n", i);
		for(;b-c;)
		{
			f[b++]=a/5;
		}

		for(;d=0,g=c*2;c-=14,e=d%a)
		{
			for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b)
			{
			}
		}
		//printf("Mid: %lld\n", (get_seconds()-end));
	}

	end = get_seconds();
	//printf("End: %f\n", end);

	secs = (end-begin);
	printf("Time to run computation of pi (2400 digits, 10 times): %.2f[secs]\n", secs);

	return secs;
}

int main()
{
	printf("This is CPU and memory benchmark for OpenWRT "VERSION_STR". This will then take some time... (typically 30-60 seconds on a 200MHz computer)\n");

	double begin = get_seconds();
	double end = get_seconds();
	overhead = (end-begin)*1000000;
	printf("Overhead for getting time: %.0fus\n", overhead);


	// Nb 1
	double sec_mem = run_memory_bench();

	// Nb 2
	double sec_pi = run_compute_pi();

	// Nb 3
	double sec_e = run_compute_e();

	// Nb 4
	double sec_float = run_float_bench();
	



	printf("Total time: %.1fs\n", (sec_mem+sec_e+sec_pi+sec_float));

	time_t t = time(0);
	struct tm ti;
       	localtime_r(&t, &ti);
	printf("\nYou can copy/paste the following line in the wiki table at: http://wiki.openwrt.org/HardwarePerformance\n");

	printf("|| %04d-%02d-%02d || ''Author'' || %.1fs || %.1fs || %.1fs || %.1fs || " VERSION_STR " || ''OS'' || ''DeviceModel'' || ''CPU model'' || ''CPU Frequency'' || ''LinkToHwPage'' ||\n", (ti.tm_year+1900), (ti.tm_mon+1), ti.tm_mday, sec_mem, sec_pi, sec_e, sec_float);

	return 0;
}