AmendHub

Download

nulleric

/

SpeedTestr

/

main.c

 

(View History)

eric   1.0 - Docs and a bit of cleanup. Latest amendment: 2 on 2022-08-10

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ShutDown.h>
4
5 /* SpeedTestr
6 A very simple ANSI C app to run a device
7 through it's paces and ensure data integrety,
8 then shutdown the machine if it passes.
9
10 Originally idea came from Tom to help test his BlueSCSI's because
11 he couldnt reach the keyboard to doubleclick on SCSI Director...
12
13 This should not be used (in it's current form) as a speed testing tool.
14 It provides a rough idea but the calls could be optomized more. This also does
15 not test through direct SCSI calls, just via stdlib file API.
16
17 Copyright Eric Helgeson
18
19 1.0 - It works! Basics done.
20 */
21
22
23 #define SIZE 1024
24
25 // Prototypes
26 unsigned int IntPow(unsigned int x, unsigned int times);
27 short writeFile(unsigned int times);
28 short readFile(unsigned int times);
29 short verifyFile(void);
30
31 // Buffer and pattern.
32 char const gPattern = 85; //1010101
33 char gBuff[SIZE];
34
35 void main()
36 {
37 unsigned int i = 1;
38 unsigned int p = 0;
39 short err = 0;
40
41 // Fill buffer with pattern
42 for(i = 0; i < SIZE ; i++)
43 {
44 gBuff[i] = gPattern;
45 }
46
47 for(i = 1; i < 15; i++)
48 {
49 p = IntPow(2, i);
50
51 printf("Writing: %d kb ", p);
52 err = writeFile(p);
53 if(err)
54 {
55 printf("Error writing file: %d", err);
56 return;
57 }
58
59 printf(" - Verifiying: %d kb ", p);
60 err = verifyFile();
61 if(err)
62 {
63 printf("Error verifying file: %d", err);
64 return;
65 }
66 printf(" - Reading: %d kb ", p);
67 err = readFile(p);
68 if(err)
69 {
70 printf("Error reading file: %d", err);
71 return;
72 }
73 remove("testfile.bin");
74 }
75
76 printf("\nSuccess, Shutting down...");
77 // Uncomment if you want the machine to shutdown right after test.
78 //ShutDwnPower();
79 }
80
81 /* Blindly read the contents of the file n * SIZE */
82 short readFile(unsigned int times)
83 {
84 FILE * fp;
85 unsigned int i;
86 long beginTick = 0;
87 long endTick = 0;
88 int result;
89
90 fp = fopen("testfile.bin", "r");
91 if(fp == NULL)
92 return -2;
93 rewind(fp);
94 beginTick = TickCount();
95
96 for(i = 0; i != times; i++)
97 {
98 result = fread(gBuff, SIZE, 1, fp);
99 if(result != 1)
100 {
101 fclose(fp);
102 return -1;
103 }
104 }
105 endTick = TickCount();
106 fclose(fp);
107 printf(" took - %lu.%lus\n", (endTick - beginTick) / 60, (endTick - beginTick) % 60);
108 return 0;
109 }
110
111 /* Verify the contents of the file match the pattern */
112 short verifyFile()
113 {
114 FILE * fp;
115 char c;
116
117 fp = fopen("testfile.bin", "r");
118 if(fp == NULL)
119 return -2;
120 rewind(fp);
121
122 while(true)
123 {
124 c = fgetc(fp);
125 if(c == EOF) break;
126 if(c != gPattern)
127 {
128 printf("VERIFY PATTERN ERR - expected %c, got %c", gPattern, c);
129 fclose(fp);
130 return -1;
131 }
132 }
133 printf("Verified OK\n");
134 fclose(fp);
135 return 0;
136 }
137
138 /* Write a file with buffer n times */
139 short writeFile(unsigned int times)
140 {
141 FILE * fp;
142 unsigned int i;
143 int result;
144 long beginTick = 0;
145 long endTick = 0;
146
147 fp = fopen("testfile.bin", "w+");
148 if(fp == NULL)
149 return -2;
150 beginTick = TickCount();
151 for(i = 0; i != times; i++)
152 {
153 result = fwrite(gBuff, SIZE, 1, fp);
154 if(result != 1)
155 {
156 fclose(fp);
157 return -1;
158 }
159 }
160 endTick = TickCount();
161 fclose(fp);
162 // TickCount's are 1/60th of a second.
163 printf(" took - %lu.%lus\n", (endTick - beginTick) / 60, (endTick - beginTick) % 60);
164 return 0;
165 }
166
167 /* Calculate power for two ints */
168 unsigned int IntPow(unsigned int x, unsigned int times)
169 {
170 unsigned int result = 1;
171 unsigned int l = 0;
172 for(l = 1; l < times + 1; l++)
173 {
174 result = result * x;
175 }
176 return result;
177 }