1 #region Copyright notice and license 2 3 // Copyright 2015 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Collections.Generic; 21 using System.Diagnostics; 22 using System.IO; 23 using System.Linq; 24 using System.Text.RegularExpressions; 25 using System.Threading; 26 using System.Threading.Tasks; 27 using Google.Protobuf; 28 using Grpc.Core; 29 using Grpc.Core.Utils; 30 using NUnit.Framework; 31 using Grpc.Testing; 32 33 namespace Grpc.IntegrationTesting 34 { 35 /// <summary> 36 /// Snapshottable time statistics. 37 /// </summary> 38 public class TimeStats 39 { 40 readonly object myLock = new object(); 41 DateTime lastWallClock; 42 TimeSpan lastUserTime; 43 TimeSpan lastPrivilegedTime; 44 TimeStats()45 public TimeStats() 46 { 47 lastWallClock = DateTime.UtcNow; 48 lastUserTime = Process.GetCurrentProcess().UserProcessorTime; 49 lastPrivilegedTime = Process.GetCurrentProcess().PrivilegedProcessorTime; 50 } 51 GetSnapshot(bool reset)52 public Snapshot GetSnapshot(bool reset) 53 { 54 lock (myLock) 55 { 56 var wallClock = DateTime.UtcNow; 57 var userTime = Process.GetCurrentProcess().UserProcessorTime; 58 var privilegedTime = Process.GetCurrentProcess().PrivilegedProcessorTime; 59 var snapshot = new Snapshot(wallClock - lastWallClock, userTime - lastUserTime, privilegedTime - lastPrivilegedTime); 60 61 if (reset) 62 { 63 lastWallClock = wallClock; 64 lastUserTime = userTime; 65 lastPrivilegedTime = privilegedTime; 66 } 67 return snapshot; 68 } 69 } 70 71 public class Snapshot 72 { 73 public TimeSpan WallClockTime { get; } 74 public TimeSpan UserProcessorTime { get; } 75 public TimeSpan PrivilegedProcessorTime { get; } 76 Snapshot(TimeSpan wallClockTime, TimeSpan userProcessorTime, TimeSpan privilegedProcessorTime)77 public Snapshot(TimeSpan wallClockTime, TimeSpan userProcessorTime, TimeSpan privilegedProcessorTime) 78 { 79 this.WallClockTime = wallClockTime; 80 this.UserProcessorTime = userProcessorTime; 81 this.PrivilegedProcessorTime = privilegedProcessorTime; 82 } 83 ToString()84 public override string ToString() 85 { 86 return string.Format("[TimeStats.Snapshot: wallClock {0}, userProcessor {1}, privilegedProcessor {2}]", WallClockTime, UserProcessorTime, PrivilegedProcessorTime); 87 } 88 } 89 } 90 } 91