/// <summary> /// /// </summary> /// <param name="rowColumns"></param> /// <param name="columnNames"></param> /// <returns></returns> public async Task InsertManyAsync(object[][] rowColumns, string[] columnNames, CancellationToken token = default) { _logger.Write("[InsertMany] Executing"); await using (var writer = await _connection.CreateColumnWriterAsync(_configuration.InsertManyScript, token)) { var columnIndex = 0; var columns = rowColumns.Aggregate(new object[columnNames.Length][], (current, column) => { /* * Swap struct like: * [{ column1, column2, column3 }, { column1, column2, column3 }] * into: * [{ column1, column1, column1 }, { column2, column2, column2 }] */ for (var row = 0; row < column.Length; row++) { current[row] = current[row] ?? new object[rowColumns.Length]; current[row][columnIndex] = column[row]; } columnIndex++; return(current); }); using (_watcher.Watch(TimeWatcher.Operation.InsertMany)) { await writer.WriteTableAsync(columns, columns.Length, token); } } _logger.Write("[InsertMany] Executed"); }
/// <summary> /// /// </summary> /// <param name="columns"></param> /// <returns></returns> public async Task InsertOneAsync(IReadOnlyDictionary <string, object> columns, CancellationToken token = default) { _logger.Write("[InsertOne] Executing"); using var command = new NpgsqlCommand(_configuration.InsertOneScript, _connection); foreach (var column in columns) { command.Parameters.AddWithValue(column.Key, column.Value); } using (_watcher.Watch(TimeWatcher.Operation.InsertOne)) { await command.ExecuteNonQueryAsync(token); } _logger.Write("[InsertOne] Executed"); }
/// <summary> /// 异步执行查询文本,并返回第一行的第一列。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="parameters">查询参数集合。</param> /// <returns>第一行的第一列数据。</returns> public async virtual Task <object> ExecuteScalarAsync(IQueryCommand queryCommand, ParameterCollection parameters = null) { Guard.ArgumentNull(queryCommand, "queryCommand"); return(await UsingConnection(() => { using (var command = CreateDbCommand(queryCommand, parameters)) { try { Task <object> result = null; OnCommandExecuted(command, null, parameters, TimeWatcher.Watch(() => result = command.ExecuteScalarAsync())); command.SyncParameters(parameters); command.ClearParameters(); return result; } catch (DbException exp) { if (ConnectionString.IsTracking && Track != null) { Track.Fail(command, exp); } throw new CommandException(command, exp); } } })); }
/// <summary> /// 执行查询文本并返回一个 <see cref="IDataReader"/>。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="segment">数据分段对象。</param> /// <param name="parameters">查询参数集合。</param> /// <returns>一个 <see cref="IDataReader"/> 对象。</returns> public virtual IDataReader ExecuteReader(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null) { Guard.ArgumentNull(queryCommand, "queryCommand"); return(UsingConnection(() => { var command = CreateDbCommand(queryCommand, parameters); try { var cmdBehavior = CommandBehavior.Default; IDataReader reader = null; var context = new CommandContext(this, command, segment, parameters); OnCommandExecuted(command, segment, parameters, TimeWatcher.Watch(() => ProcessSegmentCommand(context, () => reader = command.ExecuteReader(cmdBehavior)))); command.SyncParameters(parameters); command.ClearParameters(); return reader; } catch (DbException exp) { TrackFailedLog(command, exp); throw new CommandException(command, exp); } })); }
/// <summary> /// 执行查询文本,返回受影响的记录数。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="parameters">查询参数集合。</param> /// <returns>所影响的记录数。</returns> public virtual int ExecuteNonQuery(IQueryCommand queryCommand, ParameterCollection parameters = null) { Guard.ArgumentNull(queryCommand, "queryCommand"); return(UsingConnection(() => { using (var command = CreateDbCommand(queryCommand, parameters)) { try { var result = 0; OnCommandExecuted(command, null, parameters, TimeWatcher.Watch(() => result = command.ExecuteNonQuery())); command.SyncParameters(parameters); command.ClearParameters(); return result; } catch (DbException exp) { TrackFailedLog(command, exp); throw new CommandException(command, exp); } } })); }
public void Test() { var dic1 = new Dictionary <string, string>(); var dic2 = new Dictionary <Type, string>(); foreach (var assn in typeof(EntityTest).Assembly.GetReferencedAssemblies().Distinct()) { var ass = Assembly.Load(assn); foreach (var type in ass.GetTypes()) { dic1.Add(type.AssemblyQualifiedName, type.FullName); dic2.Add(type, type.FullName); } } var k = dic2.Last().Key; var t = TimeWatcher.Watch(() => { dic1[k.AssemblyQualifiedName] = "dd"; }); Console.WriteLine(t); t = TimeWatcher.Watch(() => { dic2[k] = "dd"; }); Console.WriteLine(t); }
public void Test() { var t1 = TimeWatcher.Watch(() => { for (var i = 0; i < 10000; i++) { Console.WriteLine($"{2222}fadfasdfasf{4454545}ffdafdf{33}ffdfad{44}"); } }); Console.WriteLine(t1); var t2 = TimeWatcher.Watch(() => { for (var i = 0; i < 10000; i++) { Console.WriteLine(string.Concat(2222, "fadfasdfasf", 4454545, "ffdafdf", 33, "ffdfad", 44)); } }); Console.WriteLine(t2); var t3 = TimeWatcher.Watch(() => { var sb = new StringBuilder(); for (var i = 0; i < 10000; i++) { Console.WriteLine(string.Format("{0}fadfasdfasf{1}ffdafdf{2}ffdfad{3}", 2222, 4454545, 33, 44)); } }); Console.WriteLine(t3); }
public void TestCache2() { using (var db = new DbContext()) { var a1 = new DataPager(10, 2); var a2 = new DataPager(10, 2); Console.WriteLine(TimeWatcher.Watch(() => { var list1 = db.Orders .Segment(a1) .CacheParsing(true, TimeSpan.FromDays(1)) .CacheExecution(true, TimeSpan.FromDays(1)) .AsNoTracking() .ToList(); })); Console.WriteLine(TimeWatcher.Watch(() => { var list2 = db.Orders .Segment(a2) .CacheParsing(true, TimeSpan.FromDays(1)) .CacheExecution(true, TimeSpan.FromDays(1)) .AsNoTracking() .ToList(); })); } }
/// <summary> /// 在后台启动一个任务,并显示一个进度提示框。 /// </summary> /// <param name="owner">进度提示框的所有者。</param> /// <param name="taskAct">要启动的任务。</param> /// <param name="cancelAct">取消后进行的动作。</param> /// <returns></returns> public static TimeSpan Run(IWin32Window owner, Action taskAct, Action cancelAct = null) { return(TimeWatcher.Watch(() => { new frmProcessor(taskAct, cancelAct).ShowDialog(owner); })); }
public void TestNew1() { var t1 = TimeWatcher.Watch(() => { for (var i = 0; i < 100000; i++) { var tt = Activator.CreateInstance(typeof(TestClass), "aa"); } }); var t2 = TimeWatcher.Watch(() => { for (var i = 0; i < 100000; i++) { var tt = typeof(TestClass).New(null, 11); } }); var t3 = TimeWatcher.Watch(() => { for (var i = 0; i < 100000; i++) { var tt = new TestClass("aa"); } }); Console.WriteLine("反射:" + t1); Console.WriteLine("缓存:" + t2); Console.WriteLine("直接:" + t3); }
public void TestInvoke() { var n = typeof(TestClass).GetMethod("Call"); var ins = new TestClass("bb"); var t1 = TimeWatcher.Watch(() => { for (var i = 0; i < 100000; i++) { n.Invoke(ins, new[] { "aaaaaa" }); } }); var t2 = TimeWatcher.Watch(() => { for (var i = 0; i < 100000; i++) { n.FastInvoke(ins, "aaaaaa"); } }); var t3 = TimeWatcher.Watch(() => { for (var i = 0; i < 100000; i++) { ins.Call("aaaaaa"); } }); Console.WriteLine("反射:" + t1); Console.WriteLine("缓存:" + t2); Console.WriteLine("直接:" + t3); }
/// <summary> /// 异步执行查询文本并返回一个 <see cref="IDataReader"/>。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="segment">数据分段对象。</param> /// <param name="parameters">查询参数集合。</param> /// <returns>一个 <see cref="IDataReader"/> 对象。</returns> public async virtual Task <IDataReader> ExecuteReaderAsync(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null) { Guard.ArgumentNull(queryCommand, "queryCommand"); return(await UsingConnection(() => { using (var command = CreateDbCommand(queryCommand, parameters)) { try { Task <DbDataReader> reader = null; var context = new CommandContext(this, command, segment, parameters); OnCommandExecuted(command, segment, parameters, TimeWatcher.Watch(() => ProcessSegmentCommand(context, () => reader = command.ExecuteReaderAsync()))); command.SyncParameters(parameters); command.ClearParameters(); return reader; } catch (DbException exp) { TrackFailedLog(command, exp); throw new CommandException(command, exp); } } })); }
private void OnCloseToNode(Event_ e) { var npcId = (NpcTypeID)e.param1; var watcher = TimeWatcher.Watch("show star window"); _starWindow.Initialize(npcId); watcher.See("Initialize"); watcher.Stop(); watcher.UnWatch(); }
/// <summary> /// 通知应用程序,一个 <see cref="DbCommand"/> 已经执行。 /// </summary> /// <param name="command">所执行的 <see cref="IDbCommand"/> 对象。</param> /// <param name="func">执行的方法。</param> protected virtual T HandleCommandExecute <T>(IDbCommand command, Func <T> func) { var result = default(T); var period = TimeWatcher.Watch(() => result = func()); Debug.WriteLine("Execute: " + command.Output() + "\nDissipation times: " + period); HandleLog(command, period); return(result); }
public void WatchTest() { var time = TimeWatcher.Watch( () => { Thread.Sleep(1000); }); Assert.IsNotNull(time); Console.WriteLine(time); }
/// <summary> /// 通知应用程序,一个 <see cref="DbCommand"/> 已经执行。 /// </summary> /// <param name="command">所执行的 <see cref="DbCommand"/> 对象。</param> /// <param name="func">执行的方法。</param> private T AfterCommandExecuted <T>(DbCommand command, ParameterCollection parameters, Func <T> func) { var result = default(T); var period = TimeWatcher.Watch(() => result = func()); HandleLogAsync(command, period); command.SyncParameters(parameters); command.ClearParameters(); return(result); }
public void TestQueryFireasyAll() { using (var db = new FireasyDbContext()) { var t = TimeWatcher.Watch(() => { var list1 = db.Users.Where(s => s.UserID != 0).AsNoTracking().ToList(); }); Console.WriteLine(t); } }
private void OnFarAwayNode(Event_ e) { var npcId = (NpcTypeID)e.param1; var watcher = TimeWatcher.Watch("show detail window"); _detailWindow.Initialize(npcId); watcher.See("Initialize"); watcher.Stop(); watcher.UnWatch(); moduleGlobal.ShowGlobalLayerDefault(1, false); _root.SafeSetActive(true); }
/// <summary> /// 解压缩数据 /// </summary> /// <param name="buffer">要解压的数据</param> /// <returns>若解压成功 返回解压后的数据 否则返回 null</returns> public static byte[] DecompressData(byte[] buffer, int off = 0) { var watcher = TimeWatcher.Watch("Util.DecompressData"); var inStream = new MemoryStream(buffer, off, buffer.Length - 1); var decoder = new Decoder(); var properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) { Logger.LogError("Util::DecompressData: Invalid compress data."); inStream.Dispose(); watcher.UnWatch(false); return(null); } decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { var v = inStream.ReadByte(); if (v < 0) { inStream.Dispose(); watcher.UnWatch(false); return(null); } outSize |= ((long)(byte)v) << (8 * i); } var outStream = new MemoryStream(); var compressedSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize, null); outStream.Position = 0; var data = new byte[outStream.Length]; outStream.Read(data, 0, data.Length); inStream.Dispose(); outStream.Dispose(); watcher.UnWatch(); return(data); }
public void TestQueryEFAll() { using (var db = new EfDbContext()) { var t = TimeWatcher.Watch(() => { var list1 = db.Users.Where(s => s.UserID != 0).ToList(); list1[0].Name = "aa"; }); db.SaveChanges(); Console.WriteLine(t); } }
private void OnNodeGetFocus(Event_ e) { var npcId = (NpcTypeID)e.param1; var watcher = TimeWatcher.Watch("show detail window2"); _detailWindow.Initialize(npcId); watcher.See("Initialize"); watcher.Stop(); watcher.UnWatch(); if (_npcDict.ContainsKey(npcId)) { _npcDict[npcId].isOn = true; } }
public void TestQuerySqlSugarAll() { var db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = connstr, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); var t = TimeWatcher.Watch(() => { var list1 = db.Queryable <SysUser>().ToList(); }); Console.WriteLine(t); }
private void button5_Click(object sender, EventArgs e) { var time = TimeWatcher.Watch(() => { treeList1.Items.Clear(); for (var i = 0; i < 5000; i++) { var item = new TreeListItem(); treeList1.Items.Add(item); item.Cells[0].Value = "祖国要举行建国" + (i + 1) + "周年纪念活动"; item.Cells[1].Value = DateTime.Today; item.Cells[2].Value = (i + 1); } }); MessageBox.Show("5000条数据需要" + time.TotalMilliseconds.ToString() + "毫秒"); }
/// <summary> /// 当摄像机靠近时触发 /// </summary> public void OnCameraCloseTo() { var watcher = TimeWatcher.Watch("OnCameraCloseTo"); watcher.See("lineGray"); RefreshStarState(); watcher.See("RefreshStarState"); starRoot?.SafeSetActive(true); AddListener(); Module_Awake.instance?.DispatchEvent(Module_Awake.Event_CloseToNode, Event_.Pop(npcId)); watcher.See("Event_CloseToNode"); watcher.Stop(); watcher.UnWatch(); animEffect.SafeSetActive(true); }
private int UpdateSimple(DataTable dataTable, ParameterCollection parameters, SqlCommand sqlCommand) { const string COLUMN_RESULT = "_Result"; if (dataTable.Columns[COLUMN_RESULT] == null) { dataTable.Columns.Add(COLUMN_RESULT, typeof(int)); } var result = 0; UsingConnection(connection => { BeginTransaction(); using (var command = CreateDbCommand(connection, sqlCommand, parameters)) { try { foreach (DataRow row in dataTable.Rows) { UpdateParameters(command.Parameters, row); var time = TimeWatcher.Watch(() => row[COLUMN_RESULT] = command.ExecuteScalar() ?? 0); HandleLog(command, time); result++; } CommitTransaction(); } catch (DbException exp) { HandleFailedLog(command, exp); RollbackTransaction(); throw new CommandException(command, exp); } } }, mode: DistributedMode.Master); return(result); }
public override bool Initialize(params object[] p) { var watcher = TimeWatcher.Watch("star panel init"); if (!base.Initialize(p)) { return(false); } _npcId = (NpcTypeID)p[0]; _npc = moduleNpc.GetTargetNpc(_npcId); AtlasHelper.SetAvatar(_headIcon, _npc?.icon); watcher.See("111"); excuteButton?.onClick.AddListener(OnExcute); RefreshUI(); watcher.See("222"); return(true); }
private void button6_Click(object sender, EventArgs e) { var time = TimeWatcher.Watch(() => { treeList1.BeginUpdate(); treeList1.Items.Clear(); for (var i = 0; i < 5000; i++) { treeList1.Items.AddCells(new object[] { "祖国要举行建国" + (i + 1) + "周年纪念活动", DateTime.Today, (i + 1) }); } treeList1.EndUpdate(); }); MessageBox.Show("5000条数据需要" + time.TotalMilliseconds.ToString() + "毫秒"); treeList1.Items[treeList1.Items.Count - 1].Selected = true; treeList1.Items[treeList1.Items.Count - 1].EnsureVisible(); }
/// <summary> /// 压缩数据流 /// 压缩数据将使用缓存的静态数据流作为缓冲区 /// 即此函数只能在单线程环境下使用 /// </summary> /// <param name="buffer">待压缩的数据</param> /// <param name="flag">若为非负数,该标志将添加到压缩后的数据头部</param> /// <returns>若压缩成功 返回压缩后的数据 否则返回 null</returns> public static byte[] CompressData(byte[] buffer, int flag = -1) { if (buffer == null || buffer.Length < 1) { return(null); } var watcher = TimeWatcher.Watch("Util.CompressData"); m_sOStream.Position = 0; m_sIStream.Position = 0; m_sOStream.SetLength(0); m_sIStream.SetLength(buffer.Length); m_sIStream.Write(buffer, 0, buffer.Length); if (flag > -1) { m_sOStream.WriteByte((byte)flag); } m_sEncoder.SetCoderProperties(m_ids, m_props); m_sEncoder.WriteCoderProperties(m_sOStream); long dataSize = m_sIStream.Length; for (int i = 0; i < 8; i++) { m_sOStream.WriteByte((byte)(dataSize >> (8 * i))); } m_sIStream.Position = 0; m_sEncoder.Code(m_sIStream, m_sOStream, -1, -1, null); m_sOStream.Position = 0; var data = new byte[m_sOStream.Length]; m_sOStream.Read(data, 0, data.Length); watcher.UnWatch(); return(data); }
/// <summary> /// 将 <see cref="DataTable"/> 的更改保存到数据库中。 /// </summary> /// <param name="dataTable">要更新的数据表对象。</param> /// <param name="insertCommand"></param> /// <param name="updateCommand"></param> /// <param name="deleteCommand"></param> /// <returns></returns> public int Update(DataTable dataTable, SqlCommand insertCommand, SqlCommand updateCommand, SqlCommand deleteCommand) { Guard.ArgumentNull(dataTable, nameof(dataTable)); var result = -1; UsingConnection(connection => { var parameters = GetTableParameters(dataTable); var adapter = Provider.DbProviderFactory.CreateDataAdapter(); if (insertCommand != null) { adapter.InsertCommand = CreateDbCommand(connection, insertCommand, parameters); adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both; } var period = TimeWatcher.Watch(() => result = adapter.Update(dataTable)); HandleLog(adapter.InsertCommand, period); }, false); return(result); }
/// <summary> /// 执行查询文本并将结果填充到指定的 <see cref="DataSet"/> 对象中。 /// </summary> /// <param name="dataSet">要填充的 <see cref="DataSet"/>。</param> /// <param name="queryCommand">查询命令。</param> /// <param name="tableName">表的名称,多个表名称使用逗号分隔。</param> /// <param name="segment">数据分段对象。</param> /// <param name="parameters">查询参数集合。</param> public virtual void FillDataSet(DataSet dataSet, IQueryCommand queryCommand, string tableName = null, IDataSegment segment = null, ParameterCollection parameters = null) { Guard.ArgumentNull(queryCommand, "queryCommand"); var adapter = Provider.DbProviderFactory.CreateDataAdapter(); Guard.NullReference(adapter); UsingConnection(() => { using (var command = CreateDbCommand(queryCommand, parameters)) { adapter.SelectCommand = command; //如果要使用Update更新DataSet,则必须指定MissingSchemaAction.AddWithKey, //但在Oracle使用分页时,却不能设置该属性,否则抛出“应为标识符或带引号的标识符” //因此,如果要实现Update,只有手动添加DataSet的PrimaryKeys //adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; dataSet.EnforceConstraints = false; ProcessAdapterTableMapping(adapter, tableName); try { var context = new CommandContext(this, command, segment, parameters); OnCommandExecuted(command, segment, parameters, TimeWatcher.Watch(() => ProcessSegmentCommand(context, () => adapter.Fill(dataSet), () => adapter.Fill(dataSet, segment.Start.Value, segment.End.Value, "Table"), () => adapter.Fill(dataSet) ))); } catch (DbException exp) { TrackFailedLog(command, exp); throw new CommandException(command, exp); } } }, false); }