* Moved "randomread" out of the bfs directory, as it's not BFS specific.

* Also renamed it to random_read, but SVN needs two revisions for that...


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28400 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-10-31 14:49:43 +00:00
parent a8cecea091
commit 144d884dad
5 changed files with 73 additions and 54 deletions
@@ -5,5 +5,6 @@ SubInclude HAIKU_TOP src tests add-ons kernel file_systems cdda ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems fs_shell ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems fragmenter ;
#SubInclude HAIKU_TOP src tests add-ons kernel file_systems iso9660 ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems random_read ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems udf ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems userlandfs ;
@@ -17,6 +17,5 @@ SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs fragmenter ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs mkbfs ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs queries ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs r5 ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs randomread ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs rename ;
SubInclude HAIKU_TOP src tests add-ons kernel file_systems bfs structureSizes ;
@@ -1,6 +0,0 @@
SubDir HAIKU_TOP src tests add-ons kernel file_systems bfs randomread ;
SimpleTest randomread
: randomread.cpp
: be ;
@@ -0,0 +1,5 @@
SubDir HAIKU_TOP src tests add-ons kernel file_systems random_read ;
SimpleTest random_read
: random_read.cpp
: be ;
@@ -1,18 +1,21 @@
/* randomread - tests if the read functions of a file system work correctly
** by reading arbitrary bytes at arbitrary positions of a file previously
** created.
** The idea is to be able to calculate the contents on each position
** of the file. It currently only counts numbers, beginning from zero,
** which is probably not a really good test.
** But if there is a bug, it would be very likely to show up after some
** thousand (or even million) runs.
** Works only on little-endian processors, such as x86 (or else the partial
** read numbers wouldn't be correctly compared).
**
** Use the --help option to see how it's used.
**
** Initial version by Axel Dörfler, axeld@pinc-software.de
** This file may be used under the terms of the OpenBeOS License.
/*
* Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
/*! Tests if the read functions of a file system work correctly by reading
arbitrary bytes at arbitrary positions of a file previously created.
The idea is to be able to calculate the contents on each position
of the file. It currently only counts numbers, beginning from zero,
which is probably not a really good test.
But if there is a bug, it would be very likely to show up after some
thousand (or even million) runs.
Works only on little-endian processors, such as x86 (or else the partial
read numbers wouldn't be correctly compared).
Use the --help option to see how it's used.
*/
#include <File.h>
@@ -23,7 +26,7 @@
#include <string.h>
#include <ctype.h>
#define FILE_NAME "RANDOMREAD_TEST_FILE"
#define FILE_NAME "RANDOM_READ_TEST_FILE"
#define FILE_SIZE (1024 * 1024)
#define BUFFER_SIZE (64 * 1024)
#define NUMBER_OF_LOOPS 1000
@@ -33,18 +36,20 @@
typedef uint32 test_t;
void createFile(const char *name,size_t size)
void
createFile(const char *name, size_t size)
{
BFile file;
status_t status = file.SetTo(name,B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
status_t status = file.SetTo(name,
B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (status < B_OK) {
fprintf(stderr,"Could not create test file: %s!\n",strerror(status));
fprintf(stderr, "Could not create test file: %s!\n", strerror(status));
return;
}
test_t max = size / sizeof(test_t);
for (uint32 i = 0;i < max;i++) {
if (file.Write(&i,sizeof(test_t)) != sizeof(test_t)) {
for (uint32 i = 0; i < max; i++) {
if (file.Write(&i, sizeof(test_t)) != sizeof(test_t)) {
fprintf(stderr,"Could not create the whole test file!\n");
break;
}
@@ -52,31 +57,34 @@ void createFile(const char *name,size_t size)
}
void readTest(const char *name,int32 loops)
void
readTest(const char *name, int32 loops)
{
BFile file;
status_t status = file.SetTo(name,B_READ_ONLY);
status_t status = file.SetTo(name, B_READ_ONLY);
if (status < B_OK) {
fprintf(stderr,"Could not open test file! Run \"randomread create [size]\" first.\n"
"This will create a file named \"%s\" in the current directory.\n",name);
fprintf(stderr, "Could not open test file! Run \"randomread create "
"[size]\" first.\n"
"This will create a file named \"%s\" in the current directory.\n",
name);
return;
}
off_t size;
if ((status = file.GetSize(&size)) < B_OK) {
fprintf(stderr,"Could not get file size: %s!\n",strerror(status));
fprintf(stderr, "Could not get file size: %s!\n", strerror(status));
return;
}
char *buffer = (char *)malloc(BUFFER_SIZE);
if (buffer == NULL) {
fprintf(stderr,"no memory to create read buffer.\n");
fprintf(stderr, "no memory to create read buffer.\n");
return;
}
srand(time(NULL));
int32 faults = 0;
for (int32 i = 0;i < loops;i++) {
for (int32 i = 0; i < loops; i++) {
off_t pos = rand() % size;
// we are lazy tester, minimum read size is 4 bytes
int32 bytes = (rand() % BUFFER_SIZE) + 4;
@@ -86,12 +94,15 @@ void readTest(const char *name,int32 loops)
else
bytes = max;
ssize_t bytesRead = file.ReadAt(pos,buffer,bytes);
if (bytesRead < B_OK)
printf(" Could not read %ld bytes at offset %Ld: %s\n",bytes,pos,strerror(bytesRead));
else if (bytesRead != max)
printf(" Could only read %ld bytes instead of %ld at offset %Ld\n",bytesRead,bytes,pos);
ssize_t bytesRead = file.ReadAt(pos, buffer, bytes);
if (bytesRead < B_OK) {
printf(" Could not read %ld bytes at offset %Ld: %s\n",
bytes, pos, strerror(bytesRead));
} else if (bytesRead != max) {
printf(" Could only read %ld bytes instead of %ld at offset %Ld\n",
bytesRead, bytes, pos);
}
// test contents
off_t bufferPos = pos;
@@ -115,7 +126,9 @@ void readTest(const char *name,int32 loops)
break;
}
if (!correct) {
printf("[%Ld,%ld] Bytes at %Ld don't match (partial begin = %ld, should be %08lx, is %08lx)!\n",pos,bytes,bufferPos,partial,num,read);
printf("[%Ld,%ld] Bytes at %Ld don't match (partial begin = "
"%ld, should be %08lx, is %08lx)!\n", pos, bytes,
bufferPos, partial, num, read);
faults++;
}
bufferPos += sizeof(test_t) - partial;
@@ -124,19 +137,21 @@ void readTest(const char *name,int32 loops)
test_t *numBuffer = (test_t *)(buffer + sizeof(test_t) - partial);
// test full numbers
for (;bufferPos < bytesRead - sizeof(test_t);bufferPos += sizeof(test_t)) {
for (; bufferPos < bytesRead - sizeof(test_t);
bufferPos += sizeof(test_t)) {
if (faults > MAX_FAULTS) {
printf("maximum number of faults reached, bail out.\n");
return;
}
if (num != *numBuffer) {
printf("[%Ld,%ld] Bytes at %Ld don't match (should be %08lx, is %08lx)!\n",pos,bytes,bufferPos,num,*numBuffer);
printf("[%Ld,%ld] Bytes at %Ld don't match (should be %08lx, "
"is %08lx)!\n", pos, bytes, bufferPos, num, *numBuffer);
faults++;
}
num++;
numBuffer++;
}
// test last partial number
partial = bytesRead - bufferPos;
if (partial > 0) {
@@ -155,7 +170,9 @@ void readTest(const char *name,int32 loops)
break;
}
if (!correct) {
printf("[%Ld,%ld] Bytes at %Ld don't match (partial end = %ld, should be %08lx, is %08lx)!\n",pos,bytes,bufferPos,partial,num,read);
printf("[%Ld,%ld] Bytes at %Ld don't match (partial end = "
"%ld, should be %08lx, is %08lx)!\n", pos, bytes,
bufferPos, partial, num, read);
faults++;
}
}
@@ -163,14 +180,15 @@ void readTest(const char *name,int32 loops)
}
int main(int argc,char **argv)
int
main(int argc, char **argv)
{
size_t size = FILE_SIZE;
int32 loops = NUMBER_OF_LOOPS;
bool create = false;
if (argv[1]) {
if (!strcmp(argv[1],"create")) {
if (!strcmp(argv[1], "create")) {
create = true;
if (argv[2])
size = atol(argv[2]);
@@ -179,7 +197,8 @@ int main(int argc,char **argv)
loops = atol(argv[1]);
else {
// get a nice filename of the program
char *filename = strrchr(argv[0],'/') ? strrchr(argv[0],'/') + 1 : argv[0];
char *filename = strrchr(argv[0], '/')
? strrchr(argv[0] , '/') + 1 : argv[0];
printf("You can either create a test file or perform the test.\n"
" Create:\t%s create [filesize]\n"
@@ -192,14 +211,15 @@ int main(int argc,char **argv)
}
if (size == 0) {
fprintf(stderr,"%s: given file size too small, set to 1 MB.\n",argv[0]);
fprintf(stderr, "%s: given file size too small, set to 1 MB.\n",
argv[0]);
size = FILE_SIZE;
}
if (create)
createFile(FILE_NAME,size);
createFile(FILE_NAME, size);
else
readTest(FILE_NAME,loops);
readTest(FILE_NAME, loops);
return 0;
}