Skip to content

Commit ac4664c

Browse files
Added maxPoolSize and minPoolSize
1 parent ba9b869 commit ac4664c

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

samples/csharp/end-to-end-apps/Regression-SalesForecast/src/eShopDashboard/Forecast/MLModel.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class MLModel<TData, TPrediction>
1414
private readonly ITransformer _model;
1515
private readonly ObjectPool<PredictionFunction<TData, TPrediction>> _predictionEnginePool;
1616
private readonly int _minPredictionEngineObjectsInPool;
17+
private readonly int _maxPredictionEngineObjectsInPool;
1718

1819
//Constructor with modelFilePathName to load
19-
public MLModel(MLContext mlContext, string modelFilePathName, int minPredictionEngineObjectsInPool = 10)
20+
public MLModel(MLContext mlContext, string modelFilePathName, int minPredictionEngineObjectsInPool = 10, int maxPredictionEngineObjectsInPool = 1000)
2021
{
2122
_mlContext = mlContext;
2223

@@ -27,25 +28,29 @@ public MLModel(MLContext mlContext, string modelFilePathName, int minPredictionE
2728
}
2829

2930
_minPredictionEngineObjectsInPool = minPredictionEngineObjectsInPool;
31+
_maxPredictionEngineObjectsInPool = maxPredictionEngineObjectsInPool;
3032

3133
//Create PredictionEngine Object Pool
3234
_predictionEnginePool = CreatePredictionEngineObjectPool();
3335
}
3436

3537
//Constructor with ITransformer model already created
36-
public MLModel(MLContext mlContext, ITransformer model, int minPredictionEngineObjectsInPool = 10)
38+
public MLModel(MLContext mlContext, ITransformer model, int minPredictionEngineObjectsInPool = 10, int maxPredictionEngineObjectsInPool = 1000)
3739
{
3840
_mlContext = mlContext;
3941
_model = model;
4042
_minPredictionEngineObjectsInPool = minPredictionEngineObjectsInPool;
43+
_maxPredictionEngineObjectsInPool = maxPredictionEngineObjectsInPool;
4144

4245
//Create PredictionEngine Object Pool
4346
_predictionEnginePool = CreatePredictionEngineObjectPool();
4447
}
4548

4649
private ObjectPool<PredictionFunction<TData, TPrediction>> CreatePredictionEngineObjectPool()
4750
{
48-
return new ObjectPool<PredictionFunction<TData, TPrediction>>(() => _model.MakePredictionFunction<TData, TPrediction>(_mlContext), _minPredictionEngineObjectsInPool);
51+
return new ObjectPool<PredictionFunction<TData, TPrediction>>(() => _model.MakePredictionFunction<TData, TPrediction>(_mlContext),
52+
_minPredictionEngineObjectsInPool,
53+
_maxPredictionEngineObjectsInPool);
4954
}
5055

5156
public TPrediction Predict(TData dataSample)

samples/csharp/end-to-end-apps/Regression-SalesForecast/src/eShopDashboard/Forecast/ObjectPool.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ public class ObjectPool<T>
1010
{
1111
private ConcurrentBag<T> _objects;
1212
private Func<T> _objectGenerator;
13+
private int _maxPoolSize;
1314

14-
public ObjectPool(Func<T> objectGenerator, int minObjects = 5)
15+
public ObjectPool(Func<T> objectGenerator, int minPoolSize = 5, int maxPoolSize = 5000)
1516
{
1617
if (objectGenerator == null) throw new ArgumentNullException("objectGenerator");
1718
_objects = new ConcurrentBag<T>();
1819
_objectGenerator = objectGenerator;
19-
20+
_maxPoolSize = maxPoolSize;
21+
2022
//Measure total time of minimum objects creation
2123
var watch = System.Diagnostics.Stopwatch.StartNew();
2224

2325
//Create minimum number of objects in pool
24-
for (int i = 0; i < minObjects; i++)
26+
for (int i = 0; i < minPoolSize; i++)
2527
{
2628
_objects.Add(_objectGenerator());
2729
}
@@ -35,9 +37,16 @@ public T GetObject()
3537
{
3638
T item;
3739
if (_objects.TryTake(out item))
40+
{
3841
return item;
42+
}
3943
else
40-
return _objectGenerator();
44+
{
45+
if(_objects.Count <= _maxPoolSize)
46+
return _objectGenerator();
47+
else
48+
throw new InvalidOperationException("MaxPoolSize reached");
49+
}
4150
}
4251

4352
public void PutObject(T item)

samples/csharp/end-to-end-apps/Regression-SalesForecast/src/eShopDashboard/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void ConfigureServices(IServiceCollection services)
6161
MLContext mlContext = ctx.GetRequiredService<MLContext>();
6262
string modelFolder = Configuration["ForecastModelsPath"];
6363
string modelFilePathName = $"{modelFolder}/country_month_fastTreeTweedie.zip";
64-
return new MLModel<CountryData, CountrySalesPrediction>(mlContext, modelFilePathName, minPredictionEngineObjectsInPool:15);
64+
return new MLModel<CountryData, CountrySalesPrediction>(mlContext, modelFilePathName, minPredictionEngineObjectsInPool:50);
6565
});
6666

6767
services.Configure<CatalogSettings>(Configuration.GetSection("CatalogSettings"));

0 commit comments

Comments
 (0)