Test program for SDHCFileSystem performance test.

Dependencies:   FatFileSystem mbed

Fork of SDHCFileSystem by Klaus Bu

SDHCFileSystem performance test.

Using SDHCFileSystem/FatFileSystem via fopen()/fwrite()/fclose() . With simple code of reading/writing (fgets()/fputs()) text file.

in performance test of fwrite()

  • File size is changeable
  • Block size is changeable
  • SPI Frequency of SDHCFileSytesm is changeable

You can change SPI Frequency by changing the SDHC_SPI_FREQUENCY in SDHCFileSystem.h

// spi frequency (Hz) 100000->100kHz
#define SDHC_SPI_FREQUENCY 2000000

performance test funcion

in main.cpp

void speedtest(char *path, int totallen, int blocklen, bool unlinkfile);
path
full path of the file
totallen
total length (byte) of test data
blocklen
length (byte) of the data writing once by fwrite()
unlinkfile
true->remove the file as ending this function
Committer:
hayashiisme
Date:
Thu Feb 14 02:07:38 2013 +0000
Revision:
1:f69b56cac865
Parent:
0:90601632692f
Commit for publishing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hayashiisme 1:f69b56cac865 1 #include "mbed.h"
hayashiisme 1:f69b56cac865 2 #include "string.h"
hayashiisme 1:f69b56cac865 3 #include "SDHCFileSystem.h"
hayashiisme 1:f69b56cac865 4
hayashiisme 1:f69b56cac865 5 #define PSQ 35
hayashiisme 1:f69b56cac865 6
hayashiisme 1:f69b56cac865 7 #define TEST_FPUTSFGETS 1
hayashiisme 1:f69b56cac865 8 #define TEST_FWRITEFREAD 1
hayashiisme 1:f69b56cac865 9 #define TEST_PRINTBYTEUS 0
hayashiisme 1:f69b56cac865 10
hayashiisme 1:f69b56cac865 11 Serial pc(USBTX, USBRX);
hayashiisme 1:f69b56cac865 12
hayashiisme 1:f69b56cac865 13 void speedtest(char *path, int totallen, int blocklen, bool unlinkfile){
hayashiisme 1:f69b56cac865 14 char *data = (char *)malloc(blocklen);
hayashiisme 1:f69b56cac865 15 for(int i=0; i < blocklen; i++)
hayashiisme 1:f69b56cac865 16 data[i] = 0x30 + (i % 10);
hayashiisme 1:f69b56cac865 17
hayashiisme 1:f69b56cac865 18 int wrotelen = 0, avgus = 0, maxus = 0, blavgus = 0, blmaxus = 0;
hayashiisme 1:f69b56cac865 19 int maxi = 0, blmaxi = 0;
hayashiisme 1:f69b56cac865 20 Timer t;
hayashiisme 1:f69b56cac865 21 t.reset();
hayashiisme 1:f69b56cac865 22 t.start();
hayashiisme 1:f69b56cac865 23 int beginus = t.read_us();
hayashiisme 1:f69b56cac865 24 FILE *fp = fopen(path, "w");
hayashiisme 1:f69b56cac865 25 int i = 0, j = 0;
hayashiisme 1:f69b56cac865 26 while(wrotelen < totallen){
hayashiisme 1:f69b56cac865 27 int beforeus = t.read_us();
hayashiisme 1:f69b56cac865 28 int wl = fwrite((void *)data, 1, blocklen, fp);
hayashiisme 1:f69b56cac865 29 int afterus = t.read_us();
hayashiisme 1:f69b56cac865 30 int blockus = afterus - beforeus;
hayashiisme 1:f69b56cac865 31 int byteus = blockus / wl;
hayashiisme 1:f69b56cac865 32 avgus = (avgus ? (avgus + byteus) / 2 : byteus);
hayashiisme 1:f69b56cac865 33 if(maxus < byteus){
hayashiisme 1:f69b56cac865 34 maxus = byteus;
hayashiisme 1:f69b56cac865 35 maxi = i;
hayashiisme 1:f69b56cac865 36 }
hayashiisme 1:f69b56cac865 37 blavgus = (blavgus ? (blavgus + blockus) / 2 : blockus);
hayashiisme 1:f69b56cac865 38 if(blmaxus < blockus){
hayashiisme 1:f69b56cac865 39 blmaxus = blockus;
hayashiisme 1:f69b56cac865 40 blmaxi = i;
hayashiisme 1:f69b56cac865 41 }
hayashiisme 1:f69b56cac865 42 wrotelen += wl;
hayashiisme 1:f69b56cac865 43 i++;
hayashiisme 1:f69b56cac865 44 if(TEST_PRINTBYTEUS){
hayashiisme 1:f69b56cac865 45 if(byteus > 2){
hayashiisme 1:f69b56cac865 46 printf("%s %08d", (j++ ? "" : "\r\n"), byteus);
hayashiisme 1:f69b56cac865 47 }else{
hayashiisme 1:f69b56cac865 48 j = 0;
hayashiisme 1:f69b56cac865 49 }
hayashiisme 1:f69b56cac865 50 }
hayashiisme 1:f69b56cac865 51 }
hayashiisme 1:f69b56cac865 52 if(TEST_PRINTBYTEUS) printf("\r\n");
hayashiisme 1:f69b56cac865 53 fclose(fp);
hayashiisme 1:f69b56cac865 54
hayashiisme 1:f69b56cac865 55 int endus = t.read_us();
hayashiisme 1:f69b56cac865 56 t.stop();
hayashiisme 1:f69b56cac865 57
hayashiisme 1:f69b56cac865 58 printf("SPIFreq=%dHz TotalBytes=%d (Block)=%dbytes TotalTime=%dus\r\n",
hayashiisme 1:f69b56cac865 59 SDHC_SPI_FREQUENCY, wrotelen, blocklen, endus-beginus);
hayashiisme 1:f69b56cac865 60 printf("Time(us): max=%d(%d) avg=%d max(block)=%d(%d) avg(block)=%d\r\n",
hayashiisme 1:f69b56cac865 61 maxus, maxi, avgus, blmaxus, blmaxi, blavgus);
hayashiisme 1:f69b56cac865 62 printf("\r\n");
hayashiisme 1:f69b56cac865 63
hayashiisme 1:f69b56cac865 64 if(unlinkfile) remove(path);
hayashiisme 1:f69b56cac865 65 free(data);
hayashiisme 1:f69b56cac865 66 }
hayashiisme 1:f69b56cac865 67
hayashiisme 1:f69b56cac865 68
hayashiisme 1:f69b56cac865 69 int main(){
hayashiisme 1:f69b56cac865 70 pc.baud(921600);
hayashiisme 1:f69b56cac865 71 SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs
hayashiisme 1:f69b56cac865 72 printf("%s [START %04d]\r\n", __FILE__, PSQ);
hayashiisme 1:f69b56cac865 73
hayashiisme 1:f69b56cac865 74 char *str = "Hello, World!\n";
hayashiisme 1:f69b56cac865 75 int l = strlen(str) + 1;
hayashiisme 1:f69b56cac865 76 char *path;
hayashiisme 1:f69b56cac865 77 FILE *fp;
hayashiisme 1:f69b56cac865 78 char buf[512];
hayashiisme 1:f69b56cac865 79
hayashiisme 1:f69b56cac865 80 if(TEST_FWRITEFREAD){
hayashiisme 1:f69b56cac865 81 printf("[fwrite()/fread()]\r\n");
hayashiisme 1:f69b56cac865 82 printf("write data of %d bytes\r\n", l);
hayashiisme 1:f69b56cac865 83 memset(buf, 0x00, sizeof(buf));
hayashiisme 1:f69b56cac865 84 path = "/sd/fwrite_fread.txt";
hayashiisme 1:f69b56cac865 85
hayashiisme 1:f69b56cac865 86 fp = fopen(path, "w");
hayashiisme 1:f69b56cac865 87 int r = fwrite((void *)str, 1, l, fp);
hayashiisme 1:f69b56cac865 88 fclose(fp);
hayashiisme 1:f69b56cac865 89 printf("fwrite() not wrote whole data %d/%d\r\n", r, l);
hayashiisme 1:f69b56cac865 90 fp = fopen(path, "r");
hayashiisme 1:f69b56cac865 91 int s = fread((void *)buf, 1, r, fp);
hayashiisme 1:f69b56cac865 92 printf("fread() not read whole data %d/%d\r\n", s, r);
hayashiisme 1:f69b56cac865 93 for(int i=0; i < s; i++){
hayashiisme 1:f69b56cac865 94 printf("%c", buf[i]);
hayashiisme 1:f69b56cac865 95 }
hayashiisme 1:f69b56cac865 96 printf("\r\n");
hayashiisme 1:f69b56cac865 97 fclose(fp);
hayashiisme 1:f69b56cac865 98
hayashiisme 1:f69b56cac865 99 printf("speed test...write\r\n");
hayashiisme 1:f69b56cac865 100
hayashiisme 1:f69b56cac865 101 speedtest("/sd/fwr_5120000_0001.txt", 5120000, 1, true);
hayashiisme 1:f69b56cac865 102 speedtest("/sd/fwr_5120000_0002.txt", 5120000, 2, true);
hayashiisme 1:f69b56cac865 103 speedtest("/sd/fwr_5120000_0005.txt", 5120000, 5, true);
hayashiisme 1:f69b56cac865 104 speedtest("/sd/fwr_5120000_0010.txt", 5120000, 10, true);
hayashiisme 1:f69b56cac865 105 speedtest("/sd/fwr_5120000_0020.txt", 5120000, 20, true);
hayashiisme 1:f69b56cac865 106 speedtest("/sd/fwr_5120000_0050.txt", 5120000, 50, true);
hayashiisme 1:f69b56cac865 107 speedtest("/sd/fwr_5120000_0100.txt", 5120000, 100, true);
hayashiisme 1:f69b56cac865 108 speedtest("/sd/fwr_5120000_0256.txt", 5120000, 256, true);
hayashiisme 1:f69b56cac865 109 speedtest("/sd/fwr_5120000_0512.txt", 5120000, 512, true);
hayashiisme 1:f69b56cac865 110 speedtest("/sd/fwr_5120000_1024.txt", 5120000, 1024, true);
hayashiisme 1:f69b56cac865 111 speedtest("/sd/fwr_5120000_2048.txt", 5120000, 2048, true);
hayashiisme 1:f69b56cac865 112 speedtest("/sd/fwr_5120000_4096.txt", 5120000, 4096, true);
hayashiisme 1:f69b56cac865 113 }
hayashiisme 1:f69b56cac865 114
hayashiisme 1:f69b56cac865 115 if(TEST_FPUTSFGETS){
hayashiisme 1:f69b56cac865 116 printf("[fputs()/fgets()]\r\n");
hayashiisme 1:f69b56cac865 117 memset(buf, 0x00, sizeof(buf));
hayashiisme 1:f69b56cac865 118 path = "/sd/fputs_fgets.txt";
hayashiisme 1:f69b56cac865 119
hayashiisme 1:f69b56cac865 120 fp = fopen(path, "w");
hayashiisme 1:f69b56cac865 121 fputs(str, fp);
hayashiisme 1:f69b56cac865 122 fclose(fp);
hayashiisme 1:f69b56cac865 123 fp = fopen(path, "r");
hayashiisme 1:f69b56cac865 124 if(fgets(buf, sizeof(buf), fp)){
hayashiisme 1:f69b56cac865 125 printf("%s\r\n", buf);
hayashiisme 1:f69b56cac865 126 }
hayashiisme 1:f69b56cac865 127 fclose(fp);
hayashiisme 1:f69b56cac865 128 }
hayashiisme 1:f69b56cac865 129
hayashiisme 1:f69b56cac865 130 printf("%s [END %04d]\r\n", __FILE__, PSQ);
hayashiisme 1:f69b56cac865 131 }