@@ -13,95 +13,102 @@ namespace SQLBench
1313 //
1414 class FileTests
1515 {
16+ const int RECORD_COUNT = 100000 ;
17+ const int RECORD_SIZE = 512 ;
18+ const int LOOP_COUNT_A = 500000 ;
19+ const int LOOP_COUNT_B = 100 ;
20+
1621 //Create all the timers
1722 Random intrand ;
1823 byte [ ] array ;
1924 public string path ;
2025 public FileTests ( )
2126 {
2227 intrand = new Random ( ) ; //create random seed
23- array = new byte [ 512 ] ; //create byte array of 512
24- path = Environment . CurrentDirectory + " \\ test123.bin"; //create path for file manipulation 4
28+ array = new byte [ RECORD_SIZE ] ; //create byte array of 512
29+ path = Environment . CurrentDirectory + @"\ test123.bin"; //create path for file manipulation 4
2530 }
2631 public string createFile ( )
2732 {
2833 //Create stopwatch to track time
2934 Stopwatch createfile = new Stopwatch ( ) ;
3035 createfile . Start ( ) ;
31- FileStream fileStream1 = System . IO . File . Create ( path , 512 , FileOptions . RandomAccess ) ; //file stream array
36+ FileStream fileStream1 = System . IO . File . Create ( path , RECORD_SIZE , FileOptions . RandomAccess ) ; //file stream array
3237 //loop file stream write
33- for ( int a = 0 ; a < 100000 ; a ++ )
38+ for ( int a = 0 ; a < RECORD_COUNT ; a ++ )
3439 {
35- fileStream1 . Write ( array , 0 , 512 ) ;
40+ fileStream1 . Write ( array , 0 , RECORD_SIZE ) ;
3641 }
3742 fileStream1 . Close ( ) ;
3843 createfile . Stop ( ) ;
3944 TimeSpan mytime = createfile . Elapsed ; //get time elapsed
40- return ( mytime . TotalMilliseconds / 5.0 ) . ToString ( "#,##0" ) ; //calculate operations er second
45+ return ( RECORD_COUNT * 1000.0f / mytime . TotalMilliseconds ) . ToString ( "#,##0" ) ; //calculate operations per second
4146 }
4247 public string readFile ( )
4348 {
4449 //Read File
4550 Stopwatch READfile = new Stopwatch ( ) ; //create timer
4651 READfile . Start ( ) ; //Start timer
47- FileStream fileStream3 = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , 512 , FileOptions . SequentialScan ) ; //reate file stream
52+ FileStream fileStream3 = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , RECORD_SIZE , FileOptions . SequentialScan ) ; //reate file stream
4853 //loop for stream reader
49- for ( int a = 0 ; a < 100000 ; a ++ )
54+ for ( int a = 0 ; a < RECORD_COUNT ; a ++ )
5055 {
51- fileStream3 . Read ( array , 0 , 512 ) ;
56+ fileStream3 . Read ( array , 0 , RECORD_SIZE ) ;
5257 }
5358 fileStream3 . Close ( ) ;
5459 READfile . Stop ( ) ;
5560 TimeSpan mytime = READfile . Elapsed ; //get time elapsed
56- return ( 500000.0 / mytime . TotalMilliseconds * 1000.0 ) . ToString ( "#,##0" ) ; //caclupate operations per second
61+ return ( RECORD_COUNT * 1000.0f / mytime . TotalMilliseconds ) . ToString ( "#,##0" ) ; //caclupate operations per second
5762 }
5863 public string SequenceWrite ( )
5964 {
6065 //write sequecially
6166 Stopwatch SeqWritefile = new Stopwatch ( ) ; //create timer
6267 SeqWritefile . Start ( ) ; //start timer
63- FileStream fileStream4 = new FileStream ( path , FileMode . Open , FileAccess . ReadWrite , FileShare . None , 512 , FileOptions . SequentialScan ) ; //create strim with sequential option
68+ FileStream fileStream4 = new FileStream ( path , FileMode . Open , FileAccess . ReadWrite , FileShare . None , RECORD_SIZE , FileOptions . SequentialScan ) ; //create strim with sequential option
6469 //loop writing a file sequentially
65- for ( int a = 0 ; a < 100000 ; a ++ )
70+ for ( int a = 0 ; a < RECORD_COUNT ; a ++ )
6671 {
67- fileStream4 . Write ( array , 0 , 512 ) ;
72+ fileStream4 . Write ( array , 0 , RECORD_SIZE ) ;
6873 }
6974 fileStream4 . Close ( ) ; //close stream
7075 SeqWritefile . Stop ( ) ; //stop timer
7176 TimeSpan mytime = SeqWritefile . Elapsed ; //get elapsed time
72- return ( 500000.0 / mytime . TotalMilliseconds * 1000.0 ) . ToString ( "#,##0" ) ; //calculate operations per second
77+ return ( RECORD_COUNT * 1000.0f / mytime . TotalMilliseconds ) . ToString ( "#,##0" ) ; //calculate operations per second
7378 }
7479 public string randomRead ( )
7580 {
7681 //Random File Reads.
7782 Stopwatch RndReadfile = new Stopwatch ( ) ; //create timer
7883 RndReadfile . Start ( ) ; //Start timer
79- FileStream fileStream5 = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , 512 , FileOptions . RandomAccess ) ; //create stream with random access option
84+ FileStream fileStream5 = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , RECORD_SIZE , FileOptions . RandomAccess ) ; //create stream with random access option
8085 //loop while randomly reading parts of the file
81- for ( int a = 0 ; a < 100000 ; a ++ )
86+ for ( int a = 0 ; a < RECORD_COUNT ; a ++ )
8287 {
83- fileStream5 . Seek ( ( long ) Math . Round ( ( 512f * ( double ) ( intrand . Next ( ) * 100000f ) ) ) , SeekOrigin . Begin ) ;
84- fileStream5 . Read ( array , 0 , 512 ) ;
88+ long SeekPos = RECORD_SIZE * intrand . Next ( RECORD_COUNT ) ; //(long)Math.Round((512f * (double)(intrand.Next() * 100000f)));
89+ fileStream5 . Seek ( SeekPos , SeekOrigin . Begin ) ;
90+ fileStream5 . Read ( array , 0 , RECORD_SIZE ) ;
8591 }
8692 fileStream5 . Close ( ) ;
8793 RndReadfile . Stop ( ) ;
8894 TimeSpan mytime = RndReadfile . Elapsed ;
89- return ( 5000.0 / mytime . TotalMilliseconds * 1000.0 ) . ToString ( "#. ##0" ) ;
95+ return ( RECORD_COUNT * 1000.0f / mytime . TotalMilliseconds ) . ToString ( "#, ##0" ) ;
9096 }
9197 public string randomWrite ( )
9298 {
9399 Stopwatch RndWritefile = new Stopwatch ( ) ;
94100 RndWritefile . Start ( ) ;
95- FileStream fileStream6 = new FileStream ( path , FileMode . Open , FileAccess . ReadWrite , FileShare . None , 512 , FileOptions . RandomAccess ) ;
96- for ( int a = 0 ; a < 100 ; a ++ )
101+ FileStream fileStream6 = new FileStream ( path , FileMode . Open , FileAccess . ReadWrite , FileShare . None , RECORD_SIZE , FileOptions . RandomAccess ) ;
102+ for ( int a = 0 ; a < RECORD_COUNT ; a ++ )
97103 {
98- fileStream6 . Seek ( ( long ) Math . Round ( ( 512f * ( double ) ( intrand . Next ( ) * 100000f ) ) ) , SeekOrigin . Begin ) ;
99- fileStream6 . Write ( array , 0 , 512 ) ;
104+ long SeekPos = RECORD_SIZE * intrand . Next ( RECORD_COUNT ) ; //(long)Math.Round((512f * (double)(intrand.Next() * 100000f)));
105+ fileStream6 . Seek ( SeekPos , SeekOrigin . Begin ) ;
106+ fileStream6 . Write ( array , 0 , RECORD_SIZE ) ;
100107 }
101108 fileStream6 . Close ( ) ;
102109 RndWritefile . Stop ( ) ;
103110 TimeSpan mytime = RndWritefile . Elapsed ;
104- return ( 5000.0 / mytime . TotalMilliseconds * 1000.0 ) . ToString ( "#. ##0" ) ;
111+ return ( RECORD_COUNT * 1000.0f / mytime . TotalMilliseconds ) . ToString ( "#, ##0" ) ;
105112 }
106113 }
107114}
0 commit comments