/// <summary> /// Builds a pooled memory stream. /// </summary> /// <param name="capacity">The capacity of the backing stream.</param> public PooledMemoryStream(int capacity, OutputActionDelegate logAction) { LogAction = logAction; _trackedMemoryStream = new TrackedMemoryStream(capacity) { Parent = this }; OnValidateObject += (ctx) => { if (ctx.Direction == PooledObjectDirection.Outbound) { // We validate only inbound objects, because when they are in the pool they // cannot change their state. return(true); } if (!_trackedMemoryStream.CanRead || !_trackedMemoryStream.CanWrite || !_trackedMemoryStream.CanSeek) { LogAction("[ObjectPool] Memory stream has already been disposed", null, MessageType.Warning); return(false); } var memoryStreamPool = PooledObjectInfo.Handle as IMemoryStreamPool; if (_trackedMemoryStream.Capacity < memoryStreamPool.MinimumMemoryStreamCapacity) { LogAction($"[ObjectPool] Memory stream capacity is {_trackedMemoryStream.Capacity}, while minimum required capacity is {memoryStreamPool.MinimumMemoryStreamCapacity}", null, MessageType.Warning); return(false); } if (_trackedMemoryStream.Capacity > memoryStreamPool.MaximumMemoryStreamCapacity) { LogAction($"[ObjectPool] Memory stream capacity is {_trackedMemoryStream.Capacity}, while maximum allowed capacity is {memoryStreamPool.MaximumMemoryStreamCapacity}", null, MessageType.Warning); return(false); } return(true); // Object is valid. }; OnResetState += (o) => { _trackedMemoryStream.Position = 0L; _trackedMemoryStream.SetLength(0L); }; OnReleaseResource += (o) => { _trackedMemoryStream.Parent = null; _trackedMemoryStream.Dispose(); }; }
public static void Time(string name, int iteration, ActionDelegate action, OutputActionDelegate actionOut, bool outProbability = false) { if (String.IsNullOrEmpty(name)) { return; } if (action == null) { return; } //1. Print name actionOut(name); // 2. Record the latest GC counts //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.Collect(GC.MaxGeneration); int[] gcCounts = new int[GC.MaxGeneration + 1]; for (int i = 0; i <= GC.MaxGeneration; i++) { gcCounts[i] = GC.CollectionCount(i); } // 3. Run action Stopwatch watch = new Stopwatch(); watch.Start(); long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick Dictionary <object, int> dicProbability = new Dictionary <object, int>(); if (outProbability == true) { object v = null; for (int i = 0; i < iteration; i++) { v = action(i); if (v != null) { if (dicProbability.ContainsKey(v)) { dicProbability[v] += 1; } else { dicProbability.Add(v, 1); } } } } else { for (int i = 0; i < iteration; i++) { action(i); } } long ticks = GetCurrentThreadTimes() - ticksFst; watch.Stop(); // 4. Print CPU actionOut("\tTime Elapsed:\t\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms"); actionOut("\tTime Elapsed (one time):" + (watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms"); actionOut("\tCPU time:\t\t" + (ticks * 100).ToString("N0") + "ns"); actionOut("\tCPU time (one time):\t" + (ticks * 100 / iteration).ToString("N0") + "ns"); // 5. Print GC for (int i = 0; i <= GC.MaxGeneration; i++) { int count = GC.CollectionCount(i) - gcCounts[i]; actionOut("\tGen " + i + ": \t\t\t" + count); } // 6. Print probility if (outProbability == true) { actionOut("\tRandCount : \t\t" + dicProbability.Count); int sum = 0; foreach (var item in dicProbability.Values) { sum += item; } foreach (var item in dicProbability) { actionOut("\t\t\t RandItem :" + item.Key.ToString() + "\t Count :" + item.Value + "\t Probability :" + Math.Round((double)((double)item.Value / (double)sum), 3)); } } }
public LoadedDatabaseConfig Load(string path, IDatabaseConfig databaseConfig, OutputActionDelegate _LogAction = null) { lock (olock) { LoadedDatabaseConfig __LoadedDatabaseConfig = null; if (_DbConfigCached.TryGetValue(path, out __LoadedDatabaseConfig)) { return(__LoadedDatabaseConfig); } else { //string fullConfigPath = ""; //lock (olock) //{ // if (_LogAction == null) // { // _LogAction = (str, ex, type) => // { // ConsoleHelper.Instance.OutputMessage(str, ex, type); // }; // } // LogAction = _LogAction; // if (PureDataConfiguration == null) // { // fullConfigPath = FileLoader.GetPath(path); // Log(string.Format("LocalDatabaseConfigLoader Load: {0} Starting", fullConfigPath)); // var configStream = LoadConfigStream(path); // try // { // using (configStream.Stream) // { // fullConfigPath = configStream.Path; // XmlSerializer xmlSerializer = new XmlSerializer(typeof(PureDataConfiguration)); // PureDataConfiguration config = xmlSerializer.Deserialize(configStream.Stream) as PureDataConfiguration; // config.Path = configStream.Path; // PureDataConfiguration = config; // } // } // catch (Exception ex) // { // Log(string.Format("LocalDatabaseConfigLoader Load fail: {0} ", ex.Message + "------" + ex)); // } // } //} //__LoadedDatabaseConfig = new LoadedDatabaseConfig(); //__LoadedDatabaseConfig.PureDataConfiguration = PureDataConfiguration; //__LoadedDatabaseConfig.Path = path; ////设定配置 //var metaInfo = MapToDatabseConfig(PureDataConfiguration, databaseConfig); //databaseConfig.OutputAction = _LogAction; //databaseConfig.InitByPureDataConfiguration = true; //__LoadedDatabaseConfig.DatabaseConfig = databaseConfig; //__LoadedDatabaseConfig.DbConnectionMetaInfo = metaInfo; //_DbConfigCached[path] = __LoadedDatabaseConfig; //Log(string.Format("Init DatabaseConfig Successfully : {0} ", fullConfigPath)); //return __LoadedDatabaseConfig; //#region Watch __LoadedDatabaseConfig = LoadDatabaseConfig(path, databaseConfig, _LogAction); ////watch pure config //var fullpath = FileLoader.GetPath(path); //string dir = System.IO.Path.GetDirectoryName(fullpath); //string filename = System.IO.Path.GetFileName(fullpath); //Log("Watch PureDataConfiguration changed on :" + fullpath); //FileWatcherLoader.Instance.Watch(dir, filename, () => //{ // __LoadedDatabaseConfig = LoadDatabaseConfig(path, databaseConfig, _LogAction); // Log(__LoadedDatabaseConfig.DatabaseConfig.ToString()); // Log(string.Format(" PureDataConfiguration has reloaded at : {0} ", path)); //}, 500); return(__LoadedDatabaseConfig); //#endregion } } }
/// <summary> /// Builds the specialized pool. /// </summary> public MemoryStreamPool(OutputActionDelegate logAction = null) : base(ObjectPool.DefaultPoolMaximumSize, (Func <PooledMemoryStream>)null) { FactoryMethod = () => new PooledMemoryStream(MinimumMemoryStreamCapacity, logAction); }
private LoadedDatabaseConfig LoadDatabaseConfig(string path, IDatabaseConfig databaseConfig, OutputActionDelegate _LogAction = null) { string fullConfigPath = ""; LoadedDatabaseConfig __LoadedDatabaseConfig = null; if (_LogAction == null) { _LogAction = (str, ex, type) => { ConsoleHelper.Instance.OutputMessage(str, ex, type); }; } LogAction = _LogAction; //if (PureDataConfiguration == null) //{ fullConfigPath = FileLoader.GetPath(path); Log(string.Format("LocalDatabaseConfigLoader Load: {0} Starting", fullConfigPath)); try { var configStream = LoadConfigStream(path); using (configStream.Stream) { fullConfigPath = configStream.Path; XmlSerializer xmlSerializer = new XmlSerializer(typeof(PureDataConfiguration)); PureDataConfiguration config = xmlSerializer.Deserialize(configStream.Stream) as PureDataConfiguration; config.Path = configStream.Path; PureDataConfiguration = config; } } catch (Exception ex) { Log(string.Format("LocalDatabaseConfigLoader Load fail: {0} ", ex.Message + "------" + ex)); return(__LoadedDatabaseConfig); } // } __LoadedDatabaseConfig = new LoadedDatabaseConfig(); __LoadedDatabaseConfig.PureDataConfiguration = PureDataConfiguration; __LoadedDatabaseConfig.Path = path; //设定配置 var metaInfo = MapToDatabseConfig(PureDataConfiguration, databaseConfig); databaseConfig.OutputAction = _LogAction; databaseConfig.InitByPureDataConfiguration = true; __LoadedDatabaseConfig.DatabaseConfig = databaseConfig; __LoadedDatabaseConfig.DbConnectionMetaInfo = metaInfo; _DbConfigCached[path] = __LoadedDatabaseConfig; Log(string.Format("Load DatabaseConfig Successfully : {0} ", fullConfigPath)); return(__LoadedDatabaseConfig); }