public DebuggerClientTests() { _options = new AgentOptions { ProjectId = _projectId, Module = _module, Version = _version, }; _debuggee = new Debuggee { Project = _projectId, Id = _debugId }; _breakpoint = new Debugger.V2.Breakpoint { Id = _breakpointId }; _breakpoint2 = new Debugger.V2.Breakpoint { Id = _breakpointId2 }; _response = new RegisterDebuggeeResponse { Debuggee = _debuggee }; _mockControllerClient = new Mock <Controller2Client>(); _client = new DebuggerClient(_options, _mockControllerClient.Object); }
public void WriteLogEntry_MessageWithComplexSubstitution() { string logMessageFormat = "I lost $$$0 today in the $1."; StackdriverVariable[] evaluatedExpressions = new StackdriverVariable[] { new StackdriverVariable() { Value = "10000" }, new StackdriverVariable() { Value = "stock market" } }; Debugger.V2.Breakpoint breakpoint = new Debugger.V2.Breakpoint() { LogLevel = Debugger.V2.Breakpoint.Types.LogLevel.Warning, LogMessageFormat = logMessageFormat, EvaluatedExpressions = { evaluatedExpressions }, }; LogEntry logEntry = new LogEntry { LogName = _logNameObj.ToString(), Severity = Logging.Type.LogSeverity.Warning, TextPayload = $"LOGPOINT: I lost ${evaluatedExpressions[0].Value} today in the {evaluatedExpressions[1].Value}." }; _client.WriteLogEntry(breakpoint); _mockLoggingClient.Verify(client => client.WriteLogEntries(LogNameOneof.From(_logNameObj), _resource, null, new[] { logEntry }, null), Times.Once()); }
public void WriteLogEntry_MessageWithSubstitution() { string logMessageFormat = "This is a log $0"; StackdriverVariable[] evaluatedExpressions = new StackdriverVariable[] { new StackdriverVariable() { Value = "test1" } }; Debugger.V2.Breakpoint breakpoint = new Debugger.V2.Breakpoint() { LogLevel = Debugger.V2.Breakpoint.Types.LogLevel.Warning, LogMessageFormat = logMessageFormat, EvaluatedExpressions = { evaluatedExpressions }, }; LogEntry logEntry = new LogEntry { LogName = _logNameObj.ToString(), Severity = Logging.Type.LogSeverity.Warning, TextPayload = $"LOGPOINT: This is a log {evaluatedExpressions[0].Value}" }; _client.WriteLogEntry(breakpoint); _mockLoggingClient.Verify(client => client.WriteLogEntries(LogNameOneof.From(_logNameObj), _resource, null, new[] { logEntry }, null), Times.Once()); }
public void WriteLogEntry_ErrorBreakpoint() { string logMessageFormat = "This is a log $0"; Debugger.V2.Breakpoint breakpoint = new Debugger.V2.Breakpoint() { LogLevel = Debugger.V2.Breakpoint.Types.LogLevel.Warning, LogMessageFormat = logMessageFormat, Status = new Debugger.V2.StatusMessage() { Description = new Debugger.V2.FormatMessage { Format = "This is an error" }, IsError = true } }; LogEntry logEntry = new LogEntry { LogName = _logNameObj.ToString(), Severity = Logging.Type.LogSeverity.Warning, TextPayload = "LOGPOINT: Error evaluating logpoint \"This is a log $0\": This is an error." }; _client.WriteLogEntry(breakpoint); _mockLoggingClient.Verify(client => client.WriteLogEntries(LogNameOneof.From(_logNameObj), _resource, null, new[] { logEntry }, null), Times.Once()); }
/// <summary> /// Starts the test application (Google.Cloud.Diagnostics.Debug.TestApp) and /// gets the average memory usage during requests to <see cref="AppUrlEcho"/> url for /// <see cref="NumberOfRequest"/> requests. /// </summary> /// <param name="debugEnabled">True if the debugger should be attached to the application.</param> /// <param name="breakpointLine">Optional, the line number to set the breakpoint on. If none is set no /// breakpoint will be set.</param> /// <param name="hitBreakpoint">Optional, true if the breakpoint is expected to hit. Defaults to false.</param> /// <param name="condition">Optional, a condition to set on the breakpoint. If none is set /// no condition will be set.</param> /// <param name="getUrl">Optional, a function to get the url to hit. Defaults to /// <see cref="TestApplication.GetEchoUrl(TestApplication, int)"/></param> /// <returns>The average memory usage during requests.</returns> public async Task <double> GetAverageMemoryUsageMBAsync( bool debugEnabled, int?breakpointLine = null, bool hitBreakpoint = false, string condition = null, Func <TestApplication, int, string> getUrl = null) { using (var app = StartTestApp(debugEnabled: debugEnabled)) { var appProcess = await app.GetApplicationProcess(); var debugProcess = app.GetDebuggerProcess(); var agentProcess = app.GetAgentProcess(); var debuggee = debugEnabled ? Polling.GetDebuggee(app.Module, app.Version) : null; int counter = 0; long memory = 0; var cts = new CancellationTokenSource(); var task = Task.Run(() => { while (!cts.IsCancellationRequested) { memory += appProcess.WorkingSet64; if (debugEnabled) { memory += debugProcess.WorkingSet64; memory += agentProcess.WorkingSet64; } counter++; Thread.Sleep(TimeSpan.FromMilliseconds(2)); } }); using (HttpClient client = new HttpClient()) { for (int i = 0; i < NumberOfRequest; i++) { Debugger.V2.Breakpoint breakpoint = null; if (breakpointLine != null) { // Set a breakpoint and wait to ensure the debuggee picks it up. breakpoint = SetBreakpointAndSleep( debuggee.Id, TestApplication.MainClass, breakpointLine.Value, condition); Thread.Sleep(TimeSpan.FromSeconds(.5)); } await client.GetAsync($"{app.AppUrlEcho}/{i}"); if (breakpointLine != null) { var newBp = Polling.GetBreakpoint(debuggee.Id, breakpoint.Id, isFinal: hitBreakpoint); Assert.Equal(hitBreakpoint, newBp.IsFinalState); } } cts.Cancel(); task.Wait(); return((memory / counter / NumberOfRequest) / Math.Pow(2, 20)); } } }
/// <summary> /// Starts the test application (Google.Cloud.Diagnostics.Debug.TestApp) and /// gets the average CPU percentage during requests to <see cref="AppUrlEcho"/> url for /// <see cref="NumberOfRequest"/> requests. /// </summary> /// <param name="debugEnabled">True if the debugger should be attached to the application.</param> /// <param name="breakpointLine">Optional, the line number to set the breakpoint on. If none is set no /// breakpoint will be set.</param> /// <param name="hitBreakpoint">Optional, true if the breakpoint is expected to hit. Defaults to false.</param> /// <param name="condition">Optional, a condition to set on the breakpoint. If none is set /// no condition will be set.</param> /// <param name="getUrl">Optional, a function to get the url to hit. Defaults to /// <see cref="TestApplication.GetEchoUrl(TestApplication, int)"/></param> /// <returns>The average CPU percentage during requests.</returns> private async Task <double> GetAverageCpuPercentAsync( bool debugEnabled, int?breakpointLine = null, bool hitBreakpoint = false, string condition = null, Func <TestApplication, int, string> getUrl = null) { getUrl = getUrl ?? TestApplication.GetEchoUrl; using (var app = StartTestApp(debugEnabled: debugEnabled)) { var appProcess = await app.GetApplicationProcess(); var debugProcess = app.GetDebuggerProcess(); var agentProcess = app.GetAgentProcess(); Stopwatch watch = Stopwatch.StartNew(); var startingAppCpu = appProcess.TotalProcessorTime; var startingDebugCpu = TimeSpan.Zero; var startingAgentCpu = TimeSpan.Zero; if (debugEnabled) { startingDebugCpu = debugProcess.TotalProcessorTime; startingAgentCpu = agentProcess.TotalProcessorTime; } var debuggee = debugEnabled ? Polling.GetDebuggee(app.Module, app.Version) : null; using (HttpClient client = new HttpClient()) { for (int i = 0; i < NumberOfRequest; i++) { Debugger.V2.Breakpoint breakpoint = null; if (breakpointLine != null) { // Set a breakpoint and wait to ensure the debuggee picks it up. breakpoint = SetBreakpointAndSleep( debuggee.Id, TestApplication.MainClass, breakpointLine.Value, condition); Thread.Sleep(TimeSpan.FromSeconds(.5)); } await client.GetAsync(getUrl(app, i)); if (breakpointLine != null) { var newBp = Polling.GetBreakpoint(debuggee.Id, breakpoint.Id, isFinal: hitBreakpoint); Assert.Equal(hitBreakpoint, newBp.IsFinalState); } } var totalCpuTime = appProcess.TotalProcessorTime - startingAppCpu; if (debugEnabled) { totalCpuTime += debugProcess.TotalProcessorTime - startingDebugCpu; totalCpuTime += agentProcess.TotalProcessorTime - startingAgentCpu; } return(totalCpuTime.TotalMilliseconds / watch.ElapsedMilliseconds); } } }
public void WriteLogEntry_SimpleMessage() { string logMessageFormat = "This is a log"; Debugger.V2.Breakpoint breakpoint = new Debugger.V2.Breakpoint() { LogLevel = Debugger.V2.Breakpoint.Types.LogLevel.Error, LogMessageFormat = logMessageFormat }; LogEntry logEntry = new LogEntry { LogName = _logNameObj.ToString(), Severity = Logging.Type.LogSeverity.Error, TextPayload = $"LOGPOINT: {logMessageFormat}" }; _client.WriteLogEntry(breakpoint); _mockLoggingClient.Verify(client => client.WriteLogEntries(LogNameOneof.From(_logNameObj), _resource, null, new[] { logEntry }, null), Times.Once()); }
/// <summary> /// Starts the test application (Google.Cloud.Diagnostics.Debug.TestApp) and /// gets the average latency for requests to the <see cref="AppUrlEcho"/> url for /// <see cref="NumberOfRequest"/> requests. /// </summary> /// <param name="debugEnabled">True if the debugger should be attached to the application.</param> /// <param name="breakpointLine">Optional, the line number to set the breakpoint on. If none is set no /// breakpoint will be set.</param> /// <param name="hitBreakpoint">Optional, true if the breakpoint is expected to hit. Defaults to false.</param> /// <param name="condition">Optional, a condition to set on the breakpoint. If none is set /// no condition will be set.</param> /// <param name="getUrl">Optional, a function to get the url to hit. Defaults to /// <see cref="TestApplication.GetEchoUrl(TestApplication, int)"/></param> /// <returns>The average latency of requests to the url.</returns> private async Task <double> GetAverageLatencyAsync( bool debugEnabled, int?breakpointLine = null, bool hitBreakpoint = false, string condition = null, Func <TestApplication, int, string> getUrl = null) { using (var app = StartTestApp(debugEnabled: debugEnabled)) { var debuggee = debugEnabled ? Polling.GetDebuggee(app.Module, app.Version) : null; using (HttpClient client = new HttpClient()) { TimeSpan totalTime = TimeSpan.Zero; for (int i = 0; i < NumberOfRequest; i++) { Debugger.V2.Breakpoint breakpoint = null; if (breakpointLine != null) { // Set a breakpoint and wait to ensure the debuggee picks it up. breakpoint = SetBreakpointAndSleep( debuggee.Id, TestApplication.MainClass, breakpointLine.Value, condition); Thread.Sleep(TimeSpan.FromSeconds(.5)); } Stopwatch watch = Stopwatch.StartNew(); await client.GetAsync($"{app.AppUrlEcho}/{i}"); totalTime += watch.Elapsed; if (breakpointLine != null) { var newBp = Polling.GetBreakpoint(debuggee.Id, breakpoint.Id, isFinal: hitBreakpoint); Assert.Equal(hitBreakpoint, newBp.IsFinalState); } } return(totalTime.TotalMilliseconds / NumberOfRequest); } } }
/// <summary>Create a <see cref="ListActiveBreakpointsResponse"/>.</summary> private ListActiveBreakpointsResponse CreateBreakpointResponse(string waitToken, Debugger.V2.Breakpoint breakpoint) { return(new ListActiveBreakpointsResponse { Breakpoints = { breakpoint }, NextWaitToken = waitToken, }); }
public void WriteLogEntry_MessageWithNestedMembers() { string logMessageFormat = "$0 and $1."; StackdriverVariable[] evaluatedExpressions = new StackdriverVariable[] { new StackdriverVariable() { Members = { new StackdriverVariable { Name = "Key1", Value = "Value1", Type = "System.String" }, new StackdriverVariable { Name = "Key2", Value = "Value2", Type = "System.String" } } }, new StackdriverVariable() { Members = { new StackdriverVariable { Name = "AnotherLevel", Type = "Nested", Members = { new StackdriverVariable { Name = "Name", Value = "Nested", Type = "System.String" } } } } } }; Debugger.V2.Breakpoint breakpoint = new Debugger.V2.Breakpoint() { LogLevel = Debugger.V2.Breakpoint.Types.LogLevel.Warning, LogMessageFormat = logMessageFormat, EvaluatedExpressions = { evaluatedExpressions }, }; LogEntry logEntry = new LogEntry { LogName = _logNameObj.ToString(), Severity = Logging.Type.LogSeverity.Warning, TextPayload = $"LOGPOINT: [ Key1 (System.String): Value1, Key2 (System.String): Value2] and [ AnotherLevel (Nested): [ Name (System.String): Nested]]." }; _client.WriteLogEntry(breakpoint); _mockLoggingClient.Verify(client => client.WriteLogEntries(LogNameOneof.From(_logNameObj), _resource, null, new[] { logEntry }, null), Times.Once()); }