📄 fs_performance.c
字号:
/*
**********************************************************************
* Micrium, Inc.
* 949 Crestview Circle
* Weston, FL 33327-1848
*
* uC/FS
*
* (c) Copyright 2001 - 2006, Micrium, Inc.
* All rights reserved.
*
***********************************************************************
----------------------------------------------------------------------
----------------------------------------------------------------------
File : Performance.c
Purpose : Sample program for measure the performance
---------------------------END-OF-HEADER------------------------------
*/
#include "FS_Int.h"
#include "FS_OS.h"
#include <string.h>
#include <stdio.h>
#define FILE_SIZE (8192 * 1024) // Defines the file size that should be used
#define BLOCK_SIZE (8 * 1024) // Block size for individual read / write operation [byte]
#define NUM_BLOCKS_MEASURE ( 64) // Number of blocks for individual measurement
#define DEVICE "" // Defines the volume that should be used
#define FILE_NAME DEVICE"\\default.txt" // Defines the name of the file to write to
/*********************************************************************
*
* Types
*
**********************************************************************
*/
typedef struct {
const char * sName;
int Min;
int Max;
I32 Av;
I32 Sum;
I32 NumSamples;
U32 NumBytes;
} RESULT;
/*********************************************************************
*
* static data
*
**********************************************************************
*/
static FS_FILE * _pFile;
static U32 _aBuffer[BLOCK_SIZE / 4];
static RESULT _aResult[3];
static int _TestNo = -1;
/*********************************************************************
*
* Local functions
*
**********************************************************************
*/
/*********************************************************************
*
* _WriteFile
*
* Write and measure time
*/
static U32 _WriteFile(const void * pData, U32 NumBytes) {
U32 t;
int i;
t = FS_X_OS_GetTime();
for (i = 0; i < NUM_BLOCKS_MEASURE; i++) {
FS_Write(_pFile, pData, NumBytes);
}
return FS_X_OS_GetTime() - t;
}
/*********************************************************************
*
* _ReadFile
*
* Read and measure time
*/
static U32 _ReadFile(void * pData, U32 NumBytes) {
U32 t;
int i;
t = FS_X_OS_GetTime();
for (i = 0; i < NUM_BLOCKS_MEASURE; i++) {
FS_Read(_pFile, pData, NumBytes);
}
return FS_X_OS_GetTime() - t;
}
/*********************************************************************
*
* _StartTest
*/
static void _StartTest(const char * sName, U32 NumBytes) {
RESULT * pResult;
pResult = &_aResult[++_TestNo];
pResult->sName = sName;
pResult->Min = 0x7fffffff;
pResult->Max = -0x7fffffff;
pResult->NumSamples = 0;
pResult->Sum = 0;
pResult->NumBytes = NumBytes;
}
/*********************************************************************
*
* _StoreResult
*/
static void _StoreResult(I32 t) {
RESULT * pResult;
pResult = &_aResult[_TestNo];
if (t > pResult->Max) {
pResult->Max = t;
}
if (t < pResult->Min) {
pResult->Min = t;
}
pResult->NumSamples++;
pResult->Sum += t;
pResult->Av = pResult->Sum / pResult->NumSamples;
}
/*********************************************************************
*
* _StoreResult
*/
static float _GetAverage(int Index) {
RESULT * pResult;
float v;
pResult = &_aResult[Index];
v = (float)pResult->Av;
if (v == 0) {
return 0;
}
v = (float)1000.0 / v;
v = v * (pResult->NumBytes / 1024);
return v;
}
/*********************************************************************
*
* Global functions
*
**********************************************************************
*/
/*********************************************************************
*
* FSTask
*/
void MainTask(void);
void MainTask(void) {
unsigned i;
char ac[200];
U32 Space;
U32 NumLoops;
U32 NumBytes;
U32 NumBytesAtOnce;
U32 t;
FS_Init();
if (FS_IsLLFormatted("") == 0) {
FS_X_Log("Low level formatting\n");
FS_FormatLow(""); /* Erase & Low-level format the flash */
}
FS_X_Log("High level formatting\n");
FS_FormatSD(DEVICE);
// FS_Format(DEVICE, NULL);
FS_MEMSET((void*)_aBuffer, 'a', sizeof(_aBuffer));
//
// Get some general info
//
Space = FS_GetVolumeFreeSpace(DEVICE);
Space = MIN(Space, FILE_SIZE);
NumBytes = BLOCK_SIZE * NUM_BLOCKS_MEASURE;
NumBytesAtOnce = BLOCK_SIZE;
NumLoops = Space / NumBytes;
//
// Create file of full size
//
_StartTest("First write (Clusters/file size preallocated)", NumBytes);
_pFile = FS_FOpen(FILE_NAME, "a+");
FS_SetFilePos(_pFile, Space, FS_FILE_BEGIN);
FS_SetEndOfFile(_pFile);
FS_SetFilePos(_pFile, 0, FS_FILE_BEGIN);
//
// Check write performance with clusters/file size preallocated
//
sprintf(ac, "W0 Writing chunks of %lu Bytes (Clusters/file size preallocated):\n", NumBytes);
FS_X_Log(ac);
for (i = 0; i < NumLoops ; i++) {
t = _WriteFile(&_aBuffer[0], NumBytesAtOnce);
_StoreResult(t);
FS_X_Log(".");
}
FS_X_Log("OK\n");
FS_FClose(_pFile);
//
// Check write performance with dynamic allocation of clusters
//
_StartTest("W1 Second write (Dynamic allocation of clusters)", NumBytes);
_pFile = FS_FOpen(FILE_NAME, "w");
sprintf(ac, "Second time writing chunks of %lu Bytes (Dynamic allocation of clusters):\n", NumBytes);
FS_X_Log(ac);
for (i = 0; i < NumLoops ; i++) {
t = _WriteFile(&_aBuffer[0], NumBytesAtOnce);
_StoreResult(t);
FS_X_Log(".");
}
FS_X_Log("OK\n");
FS_FClose(_pFile);
//
// Check read performance
//
_StartTest("Read", NumBytes);
sprintf(ac, "R0 Reading chunks of %lu Bytes (80%% fill)\n", NumBytes);
_pFile = FS_FOpen(FILE_NAME, "r");
for (i = 0; i < NumLoops; i++) {
t = _ReadFile(_aBuffer, NumBytesAtOnce);
_StoreResult(t);
sprintf(ac, "Read cycle %d: %lu ms\n", i, t);
FS_X_Log(".");
}
FS_X_Log("OK\n");
FS_FClose(_pFile);
//
// Show results
//
for (i = 0; i <= (unsigned)_TestNo; i++) {
sprintf(ac, "Test: %d (Min/Max/Av): %u/%u/%u; (%s) Speed: %4.2f kByte/s\n", i, _aResult[i].Min, _aResult[i].Max, _aResult[i].Av, _aResult[i].sName, _GetAverage(i));
FS_X_Log(ac);
}
//
// Show results for performance list
//
for (i = 0; i <= (unsigned)_TestNo; i++) {
sprintf(ac, "Test %d Speed: %4.2f kByte/s\n", i, _GetAverage(i));
FS_X_Log(ac);
}
FS_X_Log("Finished...\n");
FS_Unmount("");
while (1) {
}
}
/*************************** End of file ****************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -