@@ -413,44 +413,46 @@ public async Task Enumerator_ShouldReturnAllStoredItems() {
413413 }
414414 }
415415
416- [ Test ]
417- public async Task PerformanceTest_AddingLargeVolumeOfItems ( ) {
418- // Arrange
419- const int numberOfItems = 1_000_000 ;
420- var store = new TypedValueStore ( ) ;
421- var stopwatch = new Stopwatch ( ) ;
422-
423- // Act
424- stopwatch . Start ( ) ;
425-
426- for ( int i = 0 ; i < numberOfItems ; i ++ ) {
427- store . TryAdd ( $ "key{ i } ", i ) ;
428- }
429-
430- stopwatch . Stop ( ) ;
431- long addingTime = stopwatch . ElapsedMilliseconds ;
432-
433- // Assert
434- await Assert . That ( store ) . IsNotEmpty ( )
435- . And . HasCount ( ) . EqualTo ( numberOfItems ) ;
436- Console . WriteLine ( $ "Time to add { numberOfItems } items: { addingTime } ms") ;
437-
438- stopwatch . Reset ( ) ;
439-
440- // Act
441- stopwatch . Start ( ) ;
442-
443- for ( int i = 0 ; i < numberOfItems ; i ++ ) {
444- bool exists = store . TryGetValue ( $ "key{ i } ", out int value ) ;
445-
446- await Assert . That ( exists ) . IsTrue ( ) ;
447- await Assert . That ( value ) . IsEqualTo ( i ) ;
448- }
449-
450- stopwatch . Stop ( ) ;
451- long retrievalTime = stopwatch . ElapsedMilliseconds ;
416+ [ Test ]
417+ public async Task ParallelTest_AddingLargeVolumeOfItems ( ) {
418+ // Arrange
419+ const int numberOfItems = 1_000_000 ;
420+ var store = new TypedValueStore ( ) ;
421+ var stopwatch = new Stopwatch ( ) ;
422+
423+ // Use a precomputed list of keys to avoid multiple string allocations
424+ string [ ] keys = Enumerable . Range ( 0 , numberOfItems )
425+ . Select ( i => $ "key{ i } ")
426+ . ToArray ( ) ;
427+
428+ // Act - Add items in parallel
429+ stopwatch . Start ( ) ;
430+ Parallel . For ( 0 , numberOfItems , i => store . TryAdd ( keys [ i ] , i ) ) ;
431+ stopwatch . Stop ( ) ;
432+ long addingTime = stopwatch . ElapsedMilliseconds ;
433+
434+ // Assert - Verify count once
435+ await Assert . That ( store )
436+ . IsNotEmpty ( )
437+ . And . HasCount ( )
438+ . EqualTo ( numberOfItems ) ;
439+
440+ Console . WriteLine ( $ "Time to add { numberOfItems } items: { addingTime } ms") ;
441+
442+ var retrievalStopwatch = new Stopwatch ( ) ;
443+
444+ // Act - Retrieve items in parallel
445+ retrievalStopwatch . Start ( ) ;
446+ Parallel . For ( 0 , numberOfItems , ( i , ct ) => {
447+ // Yes usint TUnit would have been fine here as well, but this runs a lot faster. 1s vs 6s with TUnit
448+ bool exists = store . TryGetValue ( keys [ i ] , out int value ) ;
449+ if ( ! exists || value != i ) throw new Exception ( $ "Key { keys [ i ] } had an unexpected value: { value } . Expected: { i } .") ;
450+ } ) ;
451+
452+ retrievalStopwatch . Stop ( ) ;
453+ long retrievalTime = retrievalStopwatch . ElapsedMilliseconds ;
452454
453- // Assert
454- Console . WriteLine ( $ "Time to retrieve { numberOfItems } items: { retrievalTime } ms") ;
455- }
455+ // Assert
456+ Console . WriteLine ( $ "Time to retrieve { numberOfItems } items: { retrievalTime } ms") ;
457+ }
456458}
0 commit comments