public static void Compress(Call call, FileInfo fileToCompress) { if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) == FileAttributes.Hidden) { return; } if (fileToCompress.Extension == ".gz") { return; } using CallTimer compressCall = call.Timer("Compressing", new Tag("File", fileToCompress.FullName)); using FileStream originalFileStream = fileToCompress.OpenRead(); using FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"); using GZipStream compressionStream = new(compressedFileStream, CompressionMode.Compress); originalFileStream.CopyTo(compressionStream); compressCall.Log.Add("Finished Compressing", new Tag("File", fileToCompress.Name), new Tag("Original Size", fileToCompress.Length), new Tag("Compressed Size", compressedFileStream.Length) ); }
public static void Decompress(Call call, FileInfo fileToDecompress) { using CallTimer decompressCall = call.Timer("Decompressing", new Tag("File", fileToDecompress.FullName)); if (fileToDecompress.Extension == ".zip") { string targetPath = Path.ChangeExtension(fileToDecompress.FullName, null); if (Directory.Exists(targetPath)) { Directory.Delete(targetPath, true); } ZipFile.ExtractToDirectory(fileToDecompress.FullName, targetPath); } else if (fileToDecompress.Extension == ".gz") { using FileStream originalFileStream = fileToDecompress.OpenRead(); string currentFileName = fileToDecompress.FullName; string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length); using FileStream decompressedFileStream = File.Create(newFileName); using GZipStream decompressionStream = new(originalFileStream, CompressionMode.Decompress); decompressionStream.CopyTo(decompressedFileStream); decompressCall.Log.Add("Finished Decompressing", new Tag("File", fileToDecompress.Name), new Tag("Original Size", fileToDecompress.Length), new Tag("Compressed Size", decompressedFileStream.Length) ); } }
/// <summary> /// Invokes repeatedly the specified callback function on the specified interval and/or delay. /// </summary> /// <param name="interval">Specifies the interval of the repetition.</param> /// <param name="count">Specifies the maximum amount of calls. (0 = unlimited)</param> /// <param name="callback">Specifies the callback to invoke.</param> /// <returns>Returns the timer object used for this operation.</returns> public static Timer PeriodicCall(TimeSpan interval, int count, TimerCallback callback) { Timer t = new CallTimer(TimeSpan.Zero, interval, 0, callback); t.Priority = GetPriority(interval); t.Start(); return(t); }
/// <summary> /// Invokes the specified callback function after the specified delay. /// </summary> /// <param name="delay">Specifies the amount of time to wait before invoking the callback.</param> /// <param name="callback">Specifies the callback to invoke.</param> /// <returns>Returns the timer object used for this operation.</returns> public static Timer DelayCall(TimeSpan delay, TimerCallback callback) { Timer t = new CallTimer(delay, TimeSpan.Zero, 1, callback); t.Priority = GetPriority(delay); t.Start(); return(t); }
/// <summary> /// Invokes repeatedly the specified callback function on the specified interval and/or delay. /// </summary> /// <param name="delay">Specifies the amount of time to wait before invoking the first callback.</param> /// <param name="interval">Specifies the interval of the repetition.</param> /// <param name="count">Specifies the maximum amount of calls. (0 = unlimited)</param> /// <param name="callback">Specifies the callback to invoke.</param> /// <returns>Returns the timer object used for this operation.</returns> public static Timer PeriodicCall(TimeSpan delay, TimeSpan interval, int count, TimerCallback callback) { Timer t = new CallTimer(delay, interval, 0, callback); t.Priority = (count == 1) ? GetPriority(delay) : GetPriority(interval); t.Start(); return(t); }
public void TimeRangeValues() { List <TimeRangeValue> input = TimeRangeSamples(100000); // doesn't work for 1,000,000 using CallTimer callTimer = Call.Timer("Cloning"); serializer.Save(callTimer, input); var output = serializer.Load <List <TimeRangeValue> >(callTimer); }
private void M_Phone_OnCallReleaseEvent(string peerAddr, int callID, int causeCode, string reasonText, int accid) {// 释放电话 if (callID == m_CallID) { m_IsCallRelease = true; CallTimer.Stop(); this.Close(); } }
public void JsonTimeRangeValues() { List <TimeRangeValue> input = TimeRangeSamples(100000); using CallTimer callTimer = Call.Timer("Cloning"); var jsonSerializer = new SerializerMemoryJson(); jsonSerializer.Save(Call, input); var output = jsonSerializer.Load <List <TimeRangeValue> >(Call); }
public static async Task <int> DoTask(Call call, int id) { using CallTimer callTimer = call.Timer("Task", new Tag(id)); for (int i = 0; i < id && !callTimer.TaskInstance.CancelToken.IsCancellationRequested; i++) { callTimer.Log.Add("Sleeping"); await Task.Delay(1000, callTimer.TaskInstance.CancelToken); } return(id); }
private void M_Phone_OnCallConnectedEvent(string peerAddr, int callID, int accid) { if (m_CallID == callID) { lbState.Visible = false; lbTime.Visible = true; lbTime.Text = "00:00"; m_CallBegin = DateTime.Now; CallTimer.Start(); } }
public void DictionaryTest() { var items = new Dictionary <int, int>(); for (int i = 0; i < 100000; i++) { items[i] = i; } using CallTimer callTimer = Call.Timer("Get Count"); int count = items.Values.Count; }
public void Dispose() { CallTimer.Stop(); CallTimer.Dispose(); VoiceChannel.RemovePlayer(Caller.AccountEntity.Player); VoiceChannel.RemovePlayer(Receiving.AccountEntity.Player); Caller.CurrentSmartphone.IsTalking = false; Receiving.CurrentSmartphone.IsTalking = false; VoiceChannel.Remove(); }
public void Save(Call call, object obj, string name = null) { name ??= "<Default>"; using CallTimer callTimer = call.Timer(LogLevel.Debug, "Saving object: " + name, new Tag("Path", BasePath)); if (!Directory.Exists(BasePath)) { Directory.CreateDirectory(BasePath); } SaveInternal(callTimer, obj, name); }
public override T Load <T>(Call call = null) { call ??= new Call(); using CallTimer callTimer = call.Timer("Load"); Stream.Seek(0, SeekOrigin.Begin); using var reader = new BinaryReader(Stream); var serializer = Create(); serializer.TypeRepoString = _typeRepoString; serializer.Load(callTimer, reader); return((T)serializer.BaseObject(callTimer)); }
public CallTimer Timer(LogLevel logLevel, [CallerMemberName] string name = "", params Tag[] tags) { Log ??= new Log(); var call = new CallTimer() { Name = name, ParentCall = this, }; call.TaskInstance = TaskInstance?.AddSubTask(call); call.Log = Log.Call(logLevel, name, tags); return(call); }
public override void Save(Call call, object obj) { using CallTimer callTimer = call.Timer("Save"); using var writer = new BinaryWriter(Stream, Encoding.Default, true); var serializer = Create(); serializer.AddObject(callTimer, obj); serializer.Save(callTimer, writer); if (serializer.IdxTypeToRepo.TryGetValue(typeof(string), out TypeRepo typeRepo)) { _typeRepoString = (TypeRepoString)typeRepo; } }
public CallTimer Timer(int taskCount, [CallerMemberName] string name = "", params Tag[] tags) { TaskInstance ??= new TaskInstance(); if (TaskInstance.TaskCount == 0) { TaskInstance.TaskCount = 1; } var allTags = tags.ToList(); allTags.Add(new Tag("Count", taskCount)); CallTimer timer = Timer(name, allTags.ToArray()); timer.TaskInstance.TaskCount = taskCount; return(timer); }
private static async Task <T2> RunFuncAsync <T1, T2>(Call call, Func <Call, T1, Task <T2> > func, T1 item) { using CallTimer callTimer = call.Timer(item.ToString()); try { T2 result = await func(callTimer, item); if (result == null) { callTimer.Log.Add("No result"); } return(result); } catch (Exception e) { callTimer.Log.Add(e); return(default);
private byte[] GetResponse(string uri, string accept = null) { using CallTimer getCall = Call.Timer("Get Uri", new Tag("URI", uri)); for (int attempt = 1; ; attempt++) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); // requests can't be reused between attempts request.Method = "GET"; request.Accept = accept; try { WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); var memoryStream = new MemoryStream(); dataStream.CopyTo(memoryStream); byte[] data = memoryStream.ToArray(); dataStream.Close(); response.Close(); getCall.Log.Add("Downloaded HTTP File", new Tag("URI", request.RequestUri), new Tag("Size", memoryStream.Length)); return(data); } catch (WebException exception) { getCall.Log.AddError("URI request " + request.RequestUri + " failed: " + exception.Message); if (exception.Response != null) { string response = new StreamReader(exception.Response.GetResponseStream()).ReadToEnd(); Call.Log.AddError(response); } } if (attempt >= MaxAttempts) { break; } System.Threading.Thread.Sleep(SleepMilliseconds * attempt); } throw new Exception("HTTP request failed " + MaxAttempts + " times: " + uri); }
protected override object LoadInternal(Call call, bool lazy, TaskInstance taskInstance) { var serializer = new Serializer { TaskInstance = taskInstance }; MemoryStream memoryStream; using (CallTimer callReadAllBytes = call.Timer("Loading file: " + Name)) { memoryStream = new MemoryStream(File.ReadAllBytes(DataPath)); } var reader = new BinaryReader(memoryStream); serializer.Load(call, reader, lazy); object obj; using (CallTimer callLoadBaseObject = call.Timer("Loading base object")) { obj = serializer.BaseObject(callLoadBaseObject); } serializer.LogLoadedTypes(call); //logTimer.Add("Type Repos", new Tag("Repos", serializer.typeRepos)); // fields don't appear in columns if (taskInstance != null) { taskInstance.Percent = 100; } if (!lazy) { serializer.Dispose(); } return(obj); }
private void ParallelTaskProgress(Call call) { var downloads = new List <int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Parallel.ForEach(downloads, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, i => { using CallTimer sleepCall = call.Timer(i.ToString()); sleepCall.AddSubTask(); sleepCall.TaskInstance.ProgressMax = i; for (int j = 0; j < i; j++) { System.Threading.Thread.Sleep(1000); sleepCall.TaskInstance.Progress = j + 1; } }); }
private async Task SleepAsync(Call call) { using CallTimer callTimer = call.Timer("long op"); await Task.Delay(1000); }
private static void Main(string[] args) { var indexName = CloudConfigurationManager.GetSetting("Azure.Search.IndexName"); Log("Starting full sync to: {0}", indexName); var searchClient = GetSearchClient(); while (true) { using (var dbTimer = CallTimer.Start()) { // Get batch of restaurants. var restaurants = GetRestaurants(Skip, Take); dbTimer.Stop(); // No results, EOF. if (restaurants == null) { break; } // Flatten. var operations = new List <IndexOperation>(); foreach (var restaurant in restaurants) { var indexOperation = new IndexOperation(IndexOperationType.MergeOrUpload, "id", restaurant.Id.ToString()); indexOperation .WithProperty("internalName", restaurant.InternalName) .WithProperty("name", restaurant.Name) .WithProperty("postalCode", restaurant.PostalCode) .WithProperty("locality", restaurant.Locality) .WithProperty("street", restaurant.StreetAddress) .WithProperty("website", restaurant.Website) .WithProperty("budget", restaurant.Budget) .WithProperty("rating", restaurant.Rating) .WithProperty("fax", restaurant.Fax) .WithProperty("mobile", restaurant.Mobile) .WithProperty("phoneNumber", restaurant.PhoneNumber) .WithProperty("email", restaurant.Email) .WithProperty("hasImage", restaurant.HasImage) // Translated content. .WithProperty("region", restaurant.Region.TryGet("en")) .WithProperty("region_nl", restaurant.Region.TryGet("nl")) .WithProperty("region_fr", restaurant.Region.TryGet("fr")) .WithProperty("description", restaurant.TryGet(r => r.Description, "en")) .WithProperty("description_fr", restaurant.TryGet(r => r.Description, "fr")) .WithProperty("description_nl", restaurant.TryGet(r => r.Description, "nl")) .WithProperty("closing", restaurant.TryGet(r => r.Closing, "en")) .WithProperty("closing_fr", restaurant.TryGet(r => r.Closing, "fr")) .WithProperty("closing_nl", restaurant.TryGet(r => r.Closing, "nl")) .WithProperty("setting", restaurant.TryGet(r => r.Setting, "en")) .WithProperty("setting_fr", restaurant.TryGet(r => r.Setting, "fr")) .WithProperty("setting_nl", restaurant.TryGet(r => r.Setting, "nl")) // Translated tags. .WithProperty("accommodations", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("en")) .WithProperty("accommodations_fr", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("fr")) .WithProperty("accommodations_nl", restaurant.Accommodations.Select(a => a.Accommodation).TryGet <Accommodation, AccommodationTranslation>("nl")) .WithProperty("cuisine", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("en")) .WithProperty("cuisine_fr", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("fr")) .WithProperty("cuisine_nl", restaurant.Cuisines.Select(a => a.Cuisine).TryGet <Cuisine, CuisineTranslation>("nl")) .WithProperty("paymentFacilities", restaurant.PaymentFacilities.Select(a => a.PaymentFacility).TryGet <PaymentFacility, PaymentFacilityTranslation>("en")) .WithProperty("paymentFacilities_fr", restaurant.PaymentFacilities.Select(a => a.PaymentFacility).TryGet <PaymentFacility, PaymentFacilityTranslation>("fr")) .WithProperty("paymentFacilities_nl", restaurant.PaymentFacilities.Select(a => a.PaymentFacility).TryGet <PaymentFacility, PaymentFacilityTranslation>("nl")); // Add geocoordinates if available. if (restaurant.Longitude.HasValue && restaurant.Latitude.HasValue) { indexOperation.WithGeographyPoint("location", restaurant.Longitude.Value, restaurant.Latitude.Value); } // Add to batch. operations.Add(indexOperation); } using (var searchTimer = CallTimer.Start()) { var response = searchClient.PopulateAsync(indexName, operations.ToArray()).Result; // Error handling! if (!response.IsSuccess) { throw new Exception(response.StatusCode.ToString()); } else { var failed = response.Body.Where(r => !r.Status); foreach (var item in failed) { Log("Failed: {0} ({1})", item.Key, item.ErrorMessage); } } // Move forward. Skip += Take; Processed += restaurants.Count(); // Done! Log("Processed: {0} (Db: {1} s., Search: {2} s.)", Processed, dbTimer.TotalSeconds, searchTimer.TotalSeconds); } } } Log("Done!"); }