File tree Expand file tree Collapse file tree
tests/Faker.NET.Tests/Files Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- using Faker . NET . Attributes ;
1+ using System . Collections . Concurrent ;
2+ using Faker . NET . Attributes ;
23
34namespace Faker . NET . Files . Csv
45{
@@ -161,6 +162,22 @@ public IEnumerable<string> Generate()
161162 }
162163 }
163164
165+ public string [ ] GenerateParallel ( int batchSize = 1000 )
166+ {
167+ var arr = new string [ _rowsCountToGenerate ] ;
168+ arr [ 0 ] = JoinColumns ( _headers . Keys ) ;
169+
170+ Parallel . ForEach ( Partitioner . Create ( 1 , arr . Length , batchSize ) , range =>
171+ {
172+ for ( int i = range . Item1 ; i < range . Item2 ; i ++ )
173+ {
174+ arr [ i ] = GenerateRow ( ) ;
175+ }
176+ } ) ;
177+
178+ return arr ;
179+ }
180+
164181 /// <summary>
165182 /// Outputs the data to the <see cref="BaseStream"/>.
166183 /// Writes to a <see cref="MemoryStream"/> if a file is not given.
Original file line number Diff line number Diff line change @@ -33,9 +33,20 @@ public void GenerateCsv()
3333 [ TestCase ( 500_000 ) ]
3434 public void GenerateMassiveRows ( int rowCount )
3535 {
36+ // more of a proof of concept rather than testing because Generate() returns an IEnumerable
37+ // at time of writing will generate 3,000,000 values in about 2.5 seconds
3638 var faker = CreateCsvFaker ( ) . Iterations ( ( uint ) rowCount ) ;
39+ faker . Generate ( ) . ToArray ( ) ;
40+ }
3741
38- faker . Generate ( ) ;
42+ [ Test ]
43+ [ TestCase ( 100_000 ) ]
44+ [ TestCase ( 200_000 ) ]
45+ [ TestCase ( 500_000 ) ]
46+ public void GenerateParallelMassiveRows ( int rowCount )
47+ {
48+ var faker = CreateCsvFaker ( ) . Iterations ( ( uint ) rowCount ) ;
49+ faker . GenerateParallel ( ) ;
3950 }
4051
4152 [ Test ]
You can’t perform that action at this time.
0 commit comments