private void PrintEvents(List <DatabaseEvent> events) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < events.Count; i++) { DatabaseEvent e = events[i]; sb.AppendLine(e.ToString()); sb.AppendLine("---"); } File.WriteAllText(@"C:\Users\ThePhuong\Desktop\out.txt", sb.ToString()); }
public virtual void Run(CancellationToken?token = null) { Stopwatch.Reset(); try { for (int i = 0; i < Events.Count; i++) { if (token != null && token.Value.IsCancellationRequested) { break; } DatabaseEvent e = Events[i]; e.Execute(); ExecutedEvents = i + 1; if (simulateDelay && i > 0) { DatabaseEvent prevEvent = Events[i - 1]; if ((prevEvent.EventType == DatabaseEvent.NONQUERY || prevEvent.EventType == DatabaseEvent.QUERY) && (e.EventType == DatabaseEvent.NONQUERY || e.EventType == DatabaseEvent.QUERY) && e.StartTime != null && prevEvent.StartTime != null) { double delay = (e.StartTime - prevEvent.StartTime).Value.TotalMilliseconds; if (delay > 0) { Thread.Sleep((int)delay); } } } } } finally { foreach (OdbcConnection conn in Connections.Values) { conn.Close(); conn.Dispose(); } OdbcConnection.ReleaseObjectPool(); Connections.Clear(); } }
public override void Run(CancellationToken?token) { Stopwatch.Reset(); using (OdbcConnection conn = ExistingConnection ?? new OdbcConnection(ConnectionString)) { if (conn != ExistingConnection) { conn.Open(); } for (int i = 0; i < Events.Count; i++) { if (token != null && token.Value.IsCancellationRequested) { break; } DatabaseEvent e = Events[i]; if (e is QueryEvent || e is NonQueryEvent) { using (OdbcCommand cmd = new OdbcCommand(e.Text, conn)) { cmd.CommandTimeout = 300; Stopwatch.Start(); if (e.Text.Trim().Substring(0, "select".Length).ToLower().Equals("select")) { cmd.ExecuteReader().Close(); } else { cmd.ExecuteNonQuery(); } Stopwatch.Stop(); } } ExecutedEvents = i + 1; } } }
public static void Build(DatabaseEventExecutionContext context, DataTable traceTable) { IList <DatabaseEvent> list = context.Events; for (int i = 0; i < traceTable.Rows.Count; i++) { string text = traceTable.Rows[i]["TextData"].ToString(); DateTime?startTime = null; object obj = traceTable.Rows[i]["StartTime"]; if (!(obj is DBNull)) { startTime = (DateTime)obj; } DateTime?endTime = null; obj = traceTable.Rows[i]["EndTime"]; if (!(obj is DBNull)) { endTime = (DateTime)obj; } string databaseName = ""; obj = traceTable.Rows[i]["DatabaseName"]; if (!(obj is DBNull)) { databaseName = (string)obj; } int spid = (int)traceTable.Rows[i]["SPID"]; int eventClass = (int)traceTable.Rows[i]["EventClass"]; long eventSequence = (long)traceTable.Rows[i]["EventSequence"]; DatabaseEvent e = null; switch (eventClass) { case AUDIT_LOGIN: e = new LoginEvent(context, spid, text, startTime, eventSequence); break; case AUDIT_LOGOUT: e = new LogoutEvent(context, spid, text, startTime, eventSequence); break; case EXISTING_CONNECTION: e = new ExistingConnectionEvent(context, spid, text, startTime, eventSequence); break; case SQL_BATCH_STARTING: if (text.Substring(0, 6).ToLower().Equals("select")) { e = new QueryEvent(context, spid, text, databaseName, startTime, eventSequence); } else { e = new NonQueryEvent(context, spid, text, databaseName, startTime, eventSequence); } break; case RPC_STARTING: e = new NonQueryEvent(context, spid, text, databaseName, startTime, eventSequence); break; default: break; } list.Add(e); } }