public void ExecuteSleepLock(object argument) { using (IndentManager.IncreaseIndent()) { bool log = !warmup && (int)argument == 0; int passCount = PassCount; Thread thread = Thread.CurrentThread; using (new Measurement("Sleep lock ", log ? MeasurementOptions.Log : 0, passCount)) for (int j = 0; j < passCount; j++) { Wait: while (LastAccessor == thread) { if (Stop) { return; } } lock (ObjectLock) { if (LastAccessor == thread) { goto Wait; } LastAccessor = thread; } Thread.Sleep(0); } } }
public void ArrayTest <T>(int length, int count, double equalityProbability) { Random r = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); IInstanceGenerator <T> g = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>(); IEnumerator <T> sequence = InstanceGenerationUtils <T> .GetInstances(g, r, equalityProbability).GetEnumerator(); sequence.MoveNext(); T[] array = new T[length]; for (int i = 0; i < length; i++, sequence.MoveNext()) { array[i] = sequence.Current; } AdvancedComparerStruct <T> c1 = AdvancedComparer <T> .System; AdvancedComparerStruct <T> c2 = AdvancedComparer <T> .Default; if (!TestInfo.IsProfileTestRunning) { ArrayComparisonLoop(c1, array, 1); } ArrayComparisonLoop(c2, array, 1); TestLog.Info("Array comparison (equality probability = {0}):", (int)(equalityProbability * 100)); TestLog.Info(" Type: {0}, array length: {1}", typeof(T).GetShortName(), length); using (IndentManager.IncreaseIndent()) { TestHelper.CollectGarbage(); if (!TestInfo.IsProfileTestRunning) { using (new Measurement("Default comparer", MeasurementOptions.Log, (length - 1) * count)) ArrayComparisonLoop(c1, array, count); TestHelper.CollectGarbage(); } using (new Measurement("Xtensive comparer", MeasurementOptions.Log, (length - 1) * count)) ArrayComparisonLoop(c2, array, count); TestHelper.CollectGarbage(); } }
private void GMethod2CallTest <T, T1, T2>(double speedFactor) { Random r = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); T o = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>().GetInstance(r); Container <T> c = new Container <T>(o); Action a = c.Method2 <T1, T2>; // Warmup int iterations = 100; CallClassGMethod2 <T, T1, T2>(c, iterations); a.Invoke(); CastClassGMethod2 <T, T1, T2>(c, iterations); // Real test iterations = (int)(IterationCount * speedFactor); TestLog.Info("Regular call test (2 generic arguments: {0}, {1}):", typeof(T1).GetShortName(), typeof(T2).GetShortName()); TestLog.Info(" Type: {0}", typeof(T).GetShortName()); FastCache <T> .Value = null; using (IndentManager.IncreaseIndent()) { Cleanup(); using (new Measurement("Method 2 ", MeasurementOptions.Log, iterations)) CallClassGMethod2 <T, T1, T2>(c, iterations); // Cleanup(); using (new Measurement("Method 2 (by delegate)", MeasurementOptions.Log, iterations)) CallAction(a, iterations); // Cleanup(); using (new Measurement("Method 2 cast ", MeasurementOptions.Log, iterations)) CastClassGMethod2 <T, T1, T2>(c, iterations); Cleanup(); } }
public string GenerateCodeSnippet(SnippetModel snippetModel) { var indentManager = new IndentManager(); var snippetBuilder = new StringBuilder( "//THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY" + Environment.NewLine + $"var {clientVarName} = new {clientVarType}({httpCoreVarName});{Environment.NewLine}{Environment.NewLine}"); var(requestPayload, payloadVarName) = GetRequestPayloadAndVariableName(snippetModel, indentManager); snippetBuilder.Append(requestPayload); var responseAssignment = "var result = "; // have a return type if we have a response schema that is not an error if (snippetModel.ResponseSchema == null || (snippetModel.ResponseSchema.Properties.Count == 1 && snippetModel.ResponseSchema.Properties.First().Key.Equals("error", StringComparison.OrdinalIgnoreCase))) { responseAssignment = string.Empty; } var requestConfigurationPayload = GetRequestConfiguration(snippetModel, indentManager); var parametersList = GetActionParametersList(payloadVarName, requestConfigurationPayload); var methodName = snippetModel.Method.ToString().ToLower().ToFirstCharacterUpperCase() + "Async"; snippetBuilder.AppendLine($"{responseAssignment}await {clientVarName}.{GetFluentApiPath(snippetModel.PathNodes)}.{methodName}({parametersList});"); return(snippetBuilder.ToString()); }
private static string GetRequestQueryParameters(SnippetCodeGraph model, IndentManager indentManager) { var payloadSb = new StringBuilder(); if (!model.HasParameters()) { return(default);
public void ExecuteWriteLock(object argument) { using (IndentManager.IncreaseIndent()) { bool log = !warmup && (int)argument == 0; int lockCount = 0; int switchCount = 0; int passCount = PassCount; Thread thread = Thread.CurrentThread; using (new Measurement("Write lock ", log ? MeasurementOptions.Log : 0, passCount)) for (int j = 0; j < passCount; j++) { SlimLock.EnterWriteLock(); try { lockCount++; if (LastAccessor != thread) { LastAccessor = thread; switchCount++; } } finally { SlimLock.ExitWriteLock(); } } if (log) { TestLog.Info(" Switch rate: {0} ({1:F3}%)", switchCount, switchCount * 1.0 / lockCount); } } }
public void InnerTest <T>(double speedFactor) { TestLog.Info("Type {0}:", typeof(T).GetShortName()); using (IndentManager.IncreaseIndent()) { TwoValuesTest <T>((int)(TwoValueTestIterations * speedFactor)); ArrayTest <T>((int)(ArrayTestShortLength * speedFactor), ArrayTestShortIterations, 0); ArrayTest <T>((int)(ArrayTestShortLength * speedFactor), ArrayTestShortIterations, 0.5); ArrayTest <T>((int)(ArrayTestShortLength * speedFactor), ArrayTestShortIterations, 1); ArrayTest <T>((int)(ArrayTestLongLength * speedFactor), ArrayTestLongIterations, 0); ArrayTest <T>((int)(ArrayTestLongLength * speedFactor), ArrayTestLongIterations, 0.5); ArrayTest <T>((int)(ArrayTestLongLength * speedFactor), ArrayTestLongIterations, 1); } }
private static string GetRequestConfiguration(SnippetModel snippetModel, IndentManager indentManager) { var payloadSB = new StringBuilder(); var queryParamsPayload = GetRequestQueryParameters(snippetModel, indentManager); var requestHeadersPayload = GetRequestHeaders(snippetModel, indentManager); if (!string.IsNullOrEmpty(queryParamsPayload) || !string.IsNullOrEmpty(requestHeadersPayload)) { payloadSB.AppendLine($"({requestConfigurationVarName}) =>"); payloadSB.AppendLine($"{indentManager.GetIndent()}{{"); payloadSB.Append(queryParamsPayload); payloadSB.Append(requestHeadersPayload); payloadSB.Append($"{indentManager.GetIndent()}}}"); } return(payloadSB.Length > 0 ? payloadSB.ToString() : default);
public string GenerateCodeSnippet(SnippetModel snippetModel) { var indentManager = new IndentManager(); var codeGraph = new SnippetCodeGraph(snippetModel); var payloadSb = new StringBuilder( "<?php" + Environment.NewLine + Environment.NewLine + "// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY" + Environment.NewLine + $"{ClientVarName} = new {ClientVarType}({HttpCoreVarName});{Environment.NewLine}{Environment.NewLine}"); if (codeGraph.HasBody()) { WriteObjectProperty(RequestBodyVarName, payloadSb, codeGraph.Body, indentManager); } WriteRequestExecutionPath(codeGraph, payloadSb, indentManager); return(payloadSb.ToString()); }
private void VMethodCallTest <T>(double speedFactor) { Random r = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); T o = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>().GetInstance(r); ContainerBase <T> c = new Container <T>(o); IContainer <T> ic = (IContainer <T>)c; // Warmup int iterations = 100; FastCache <T> .Value = null; CallClassVMethod(c, iterations); CallClassVMethod_WithBoxing(c, iterations); CallInterfaceVMethod(ic, iterations); CallInterfaceVMethod_WithoutCaching(ic, iterations); CallInterfaceVMethod_WithCast(c, iterations); // Real test iterations = (int)(IterationCount * speedFactor); TestLog.Info("Virtual call test:"); TestLog.Info(" Type: {0}", typeof(T).GetShortName()); FastCache <T> .Value = null; using (IndentManager.IncreaseIndent()) { Cleanup(); using (new Measurement("Virtual method (typed) ", MeasurementOptions.Log, iterations)) CallClassVMethod(c, iterations); // Cleanup(); using (new Measurement("Virtual method (with boxing) ", MeasurementOptions.Log, iterations)) CallClassVMethod_WithBoxing(c, iterations); Cleanup(); using (new Measurement("Virtual method cast ", MeasurementOptions.Log, iterations)) CastClassVMethod(c, iterations); Cleanup(); using (new Measurement("Interface method ", MeasurementOptions.Log, iterations)) CallInterfaceVMethod(ic, iterations); Cleanup(); using (new Measurement("Interface method (worst case) ", MeasurementOptions.Log, iterations)) CallInterfaceVMethod_WithoutCaching(ic, iterations); Cleanup(); using (new Measurement("Interface method (with cast) ", MeasurementOptions.Log, iterations)) CallInterfaceVMethod_WithCast(c, iterations); Cleanup(); using (new Measurement("Interface method cast ", MeasurementOptions.Log, iterations)) CastInterfaceVMethod(ic, iterations); Cleanup(); } }
public void ExecuteInvokeAsync(object argument) { Action d = delegate { return; }; using (IndentManager.IncreaseIndent()) { bool log = !warmup && (int)argument == 0; int passCount = PassCount; Thread thread = Thread.CurrentThread; using (new Measurement("Execute", log ? MeasurementOptions.Log : 0, passCount)) for (int j = 0; j < passCount; j++) { IAsyncResult r = d.BeginInvoke(null, null); d.EndInvoke(r); } } }
private void GVMethod2CallTest <T, T1, T2>(double speedFactor) { Random r = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); T o = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>().GetInstance(r); ContainerBase <T> c = new Container <T>(o); IContainer <T> ic = (IContainer <T>)c; Action a1 = c.GenericMethod2 <T1, T2>; Action a2 = ic.GenericMethod2 <T1, T2>; // Warmup int iterations = 100; CallClassGVMethod2 <T, T1, T2>(c, iterations); CallInterfaceGVMethod2 <T, T1, T2>(ic, iterations); a1.Invoke(); a2.Invoke(); CastClassGVMethod2 <T, T1, T2>(c, iterations); CastInterfaceGVMethod2 <T, T1, T2>(ic, iterations); // Real test iterations = (int)(IterationCount * speedFactor); TestLog.Info("Virtual generic call test (2 generic arguments: {0}, {1}):", typeof(T1).GetShortName(), typeof(T2).GetShortName()); TestLog.Info(" Type: {0}", typeof(T).GetShortName()); FastCache <T> .Value = null; using (IndentManager.IncreaseIndent()) { Cleanup(); using (new Measurement("Generic method 2 (class) ", MeasurementOptions.Log, iterations)) CallClassGVMethod2 <T, T1, T2>(c, iterations); // Cleanup(); using (new Measurement("Generic method 2 (class, by delegate) ", MeasurementOptions.Log, iterations)) CallAction(a1, iterations); // Cleanup(); using (new Measurement("Generic method 2 cast (class) ", MeasurementOptions.Log, iterations)) CastClassGVMethod2 <T, T1, T2>(c, iterations); Cleanup(); using (new Measurement("Generic method 2 (interface) ", MeasurementOptions.Log, iterations)) CallInterfaceGVMethod2 <T, T1, T2>(ic, iterations); // Cleanup(); using (new Measurement("Generic method 2 (interface, by delegate)", MeasurementOptions.Log, iterations)) CallAction(a2, iterations); // Cleanup(); using (new Measurement("Generic method 2 cast (interface) ", MeasurementOptions.Log, iterations)) CastInterfaceGVMethod2 <T, T1, T2>(ic, iterations); Cleanup(); } }
public void TestSequence <T>(int size, double expectedShare, double shareTolerance) { TestLog.Info("{0} random sequence, {1} items:", typeof(T).GetShortName(), size); using (IndentManager.IncreaseIndent()) { TestLog.Info("Expected probability: {0}, tolerance: {1}.", expectedShare, shareTolerance); // Testing the same sequence Random r1 = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); Random r2 = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); double equalityProbability = GetEqualityProbability <T>(r1, r2, size); Assert.AreEqual(1, equalityProbability); // Testing different sequences r1 = RandomManager.CreateRandom(1, SeedVariatorType.CallingMethod); r2 = RandomManager.CreateRandom(2, SeedVariatorType.CallingMethod); using (new Measurement("Generation and comparison", size * 2)) equalityProbability = GetEqualityProbability <T>(r1, r2, size); TestLog.Info("Actual probability: {0}.", equalityProbability); Assert.AreEqual(expectedShare, equalityProbability, shareTolerance); } }
public void TwoValuesTest <T>(int count) { Random r = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); IInstanceGenerator <T> g = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>(); IEnumerator <T> sequence = InstanceGenerationUtils <T> .GetInstances(g, r, 1).GetEnumerator(); sequence.MoveNext(); T o1 = sequence.Current; sequence.MoveNext(); T o1c = sequence.Current; T o2 = g.GetInstance(r); AdvancedComparerStruct <T> c1 = AdvancedComparer <T> .System; AdvancedComparerStruct <T> c2 = AdvancedComparer <T> .Default; if (!TestInfo.IsProfileTestRunning) { SimpleComparisonLoop(c1, o1, o2, 1000); } SimpleComparisonLoop(c2, o1, o2, 1000); TestLog.Info("Values comparison:"); TestLog.Info(" Type: {0}, instances: {1} x 2, {2}", typeof(T).GetShortName(), o1, o2); using (IndentManager.IncreaseIndent()) { TestHelper.CollectGarbage(); if (!TestInfo.IsProfileTestRunning) { using (new Measurement("Default comparer (equal) ", MeasurementOptions.Log, count)) SimpleComparisonLoop(c1, o1, o1c, count); TestHelper.CollectGarbage(); using (new Measurement("Default comparer (different)", MeasurementOptions.Log, count)) SimpleComparisonLoop(c1, o1, o2, count); TestHelper.CollectGarbage(); } using (new Measurement("Xtensive comparer (equal) ", MeasurementOptions.Log, count)) SimpleComparisonLoop(c2, o1, o1c, count); using (new Measurement("Xtensive comparer (different)", MeasurementOptions.Log, count)) SimpleComparisonLoop(c2, o1, o2, count); TestHelper.CollectGarbage(); } }
private void RunNullCheckTest(double speedFactor) { int count = (int)(IterationCount * speedFactor); // Warmup Holder[] holders = new Holder[10]; FillHolders(holders); NoCheckLoop(holders); NullCheckLoop(holders); // Real test holders = new Holder[count]; FillHolders(holders); TestLog.Info("Null check test:"); using (IndentManager.IncreaseIndent()) { TestHelper.CollectGarbage(); using (new Measurement("No check ", MeasurementOptions.Log, count)) NoCheckLoop(holders); TestHelper.CollectGarbage(); using (new Measurement("Null check ", MeasurementOptions.Log, count)) NullCheckLoop(holders); TestHelper.CollectGarbage(); } }
private void TestTypes(Type[] types) { TupleDescriptor descriptor = TupleDescriptor.Create(types); TestLog.Info("Testing sequence {0}:", descriptor); using (IndentManager.IncreaseIndent()) { // Logic test LogicTest(types, this, GetType(), "SequenceAStep"); LogicTest(types, null, GetType(), "SequenceAStaticStep"); // Performance tests ExecutionData data = ExecutionData.Create(); ExecutionSequenceHandler <ExecutionData>[] delegates; int count = passCount / 1000; delegates = DelegateHelper.CreateDelegates <ExecutionSequenceHandler <ExecutionData> >( this, GetType(), "SequenceBStep", types); TestHelper.CollectGarbage(); using (new Measurement("Creating delegates", count)) for (int i = 0; i < count; i++) { delegates = DelegateHelper.CreateDelegates <ExecutionSequenceHandler <ExecutionData> >( this, GetType(), "SequenceBStep", types); } count = passCount; DelegateHelper.ExecuteDelegates(delegates, ref data, Direction.Positive); data = ExecutionData.Create(); TestHelper.CollectGarbage(); using (new Measurement("Executing delegates", count)) for (int i = 0; i < count; i++) { DelegateHelper.ExecuteDelegates(delegates, ref data, Direction.Positive); } Assert.AreEqual(count * types.Length, data.CallCount); } }
private void RunTryCatchTest(double speedFactor) { int count = (int)(IterationCount * speedFactor); // Warmup Holder h = new Holder(); NoTryNoExceptionLoop(h, 10); TryNoExceptionLoop(h, 10); TryCatchExceptionLoop(h, 10); // Real test TestLog.Info("Try-catch test:"); using (IndentManager.IncreaseIndent()) { TestHelper.CollectGarbage(); using (new Measurement("NoTryNoException ", MeasurementOptions.Log, count)) NoTryNoExceptionLoop(h, count); TestHelper.CollectGarbage(); using (new Measurement("TryNoException ", MeasurementOptions.Log, count)) TryNoExceptionLoop(h, count); TestHelper.CollectGarbage(); using (new Measurement("TryCatchException", MeasurementOptions.Log, count)) TryCatchExceptionLoop(h, count); TestHelper.CollectGarbage(); } }
private void CloneTest <T>(double speedFactor) { Random r = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); T o = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>().GetInstance(r); Cloneable <T> c = new Cloneable <T>(o); // Warmup Cloneable <T>[] a = new Cloneable <T> [100]; CloneByMCLoop(c, a); CloneByCLoop(c, a); // Real test a = new Cloneable <T> [(int)(CloneTestArrayLength * speedFactor / 10 * 10)]; TestLog.Info("Cloning test:"); TestLog.Info(" Type: {0}, length: {1}", c.GetType().GetShortName(), a.Length); using (IndentManager.IncreaseIndent()) { Cleanup(); using (new Measurement("MemberwiseClone ", MeasurementOptions.Log, a.Length)) CloneByMCLoop(c, a); Cleanup(); using (new Measurement("CopyingConstructor", MeasurementOptions.Log, a.Length)) CloneByCLoop(c, a); Cleanup(); } }
public void rent(object sender, EventArgs e) { if (Session["user_id"] != null) { string st = Session["datebegin"].ToString(); string ed = Session["dateend"].ToString(); if (st == "" || ed == "") { Response.Write("<script>alert('请输入开始结束日期')</script>"); } else { Button btn = (Button)sender; Label gprice = btn.Parent.FindControl("gprice") as Label; string str = gprice.Attributes["data-value"].ToString(); indent ind = new indent(); ind.user_id = (int)Session["user_id"]; DateTime start = Convert.ToDateTime(Session["datebegin"].ToString()); ind.start_time = start; DateTime end = Convert.ToDateTime(Session["dateend"].ToString()); ind.end_time = end; ind.indent_date = DateTime.Now; ind.indent_state = "已租用"; ind.indent_price = float.Parse(str); int i = IndentManager.InsertIndent(ind); if (i == 1) { Response.Write("<script>alert('租车成功,可到个人中心页面查看')</script>"); } } } else { Response.Write("<script>alert('请先登录网站')</script>"); } }
private void AccessFieldTest <T>(double speedFactor) { int count = (int)(IterationCount * speedFactor); Random r = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); T o = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>().GetInstance(r); Container <T> c = new Container <T>(o); if (!warmup) { TestLog.Info("Type: {0}, length: {1}", o.GetType().GetShortName(), count); } using (IndentManager.IncreaseIndent()) { if (!warmup) { TestLog.Info("Direct field access test:"); } using (IndentManager.IncreaseIndent()) { TestHelper.CollectGarbage(); using (new Measurement("Read ", warmup ? 0 : MeasurementOptions.Log, count)) for (int i = 0; i < count; i++) { o = c.Value; } TestHelper.CollectGarbage(); using (new Measurement("Write ", warmup ? 0 : MeasurementOptions.Log, count)) for (int i = 0; i < count; i++) { c.Value = o; } TestHelper.CollectGarbage(); } if (!warmup) { TestLog.Info("TypedReference field access test:"); } using (IndentManager.IncreaseIndent()) { var fi = c.GetType().GetField("Value"); TypedReference tr = __makeref(c); TestHelper.CollectGarbage(); using (new Measurement("GetRef", warmup ? 0 : MeasurementOptions.Log, count)) for (int i = 0; i < count; i++) { tr = __makeref(c); } TestHelper.CollectGarbage(); using (new Measurement("Read ", warmup ? 0 : MeasurementOptions.Log, count)) for (int i = 0; i < count; i++) { o = (T)fi.GetValueDirect(tr); } TestHelper.CollectGarbage(); using (new Measurement("Write ", warmup ? 0 : MeasurementOptions.Log, count)) for (int i = 0; i < count; i++) { fi.SetValueDirect(tr, o); } TestHelper.CollectGarbage(); } } }
private static void WriteRequestExecutionPath(SnippetCodeGraph codeGraph, StringBuilder payloadSb, IndentManager indentManager) { var method = codeGraph.HttpMethod.Method.ToLower(); var configParameter = codeGraph.HasHeaders() || codeGraph.HasParameters() ? $"{RequestConfigurationVarName}" : string.Empty; var bodyParameter = codeGraph.HasBody() ? $"{RequestBodyVarName}" : string.Empty; var optionsParameter = codeGraph.HasOptions() ? "options" : string.Empty; var returnVar = codeGraph.HasReturnedBody() ? "$requestResult = " : string.Empty; var parameterList = GetActionParametersList(bodyParameter, configParameter, optionsParameter); payloadSb.AppendLine(GetRequestConfiguration(codeGraph, indentManager)); payloadSb.AppendLine($"{returnVar}{ClientVarName}->{GetFluentApiPath(codeGraph.Nodes)}->{method}({parameterList});"); }