Exemplo n.º 1
0
            public void Run(string resourceKey, string cloudEndPoint = "")
            {
                var builder = new DeviceDetectionPipelineBuilder()
                              // Tell it that we want to use cloud and pass our resource key.
                              .UseCloud(resourceKey);

                // If a cloud endpoint has been provided then set the
                // cloud pipeline endpoint.
                if (string.IsNullOrWhiteSpace(cloudEndPoint) == false)
                {
                    builder.SetEndPoint(cloudEndPoint);
                }

                // Create the pipeline
                using (var pipeline = builder.Build())
                {
                    // Output details for a mobile User-Agent.
                    AnalyseUserAgent(mobileUserAgent, pipeline);
                }
            }
        /// <summary>
        /// This test will create a device detection pipeline based on the
        /// supplied parameters, process 1000 user agents.
        /// The various parameters allow the test to be run for many
        /// different configurations.
        /// </summary>
        /// <param name="datafileName">
        /// The filename of the data file to use for device detection.
        /// </param>
        /// <param name="performanceProfile">
        /// The performance profile to use.
        /// </param>
        /// <param name="useLazyLoading">
        /// Whether or not to use the lazy loading feature.
        /// </param>
        /// <param name="multiThreaded">
        /// Whether to use a single thread or multiple threads when
        /// passing user agents to the pipeline for processing.
        /// </param>
        public void TestOnPremise_AllConfigurations_100_UserAgents(
            string datafileName,
            PerformanceProfiles performanceProfile,
            bool useLazyLoading,
            bool multiThreaded)
        {
            var datafile      = TestHelpers.Utils.GetFilePath(datafileName);
            var updateService = new Mock <IDataUpdateService>();

            // Configure the pipeline builder based on the
            // parameters passed to this method.
            var builder = new DeviceDetectionPipelineBuilder(
                new NullLoggerFactory(), null, updateService.Object)
                          .UseOnPremise(datafile.FullName, null, false)
                          .SetPerformanceProfile(performanceProfile)
                          .SetShareUsage(false)
                          .SetDataFileSystemWatcher(false);

            if (useLazyLoading)
            {
                builder.UseLazyLoading();
            }

            CancellationTokenSource cancellationSource = new CancellationTokenSource();

            using (var pipeline = builder.Build())
            {
                var options = new ParallelOptions()
                {
                    CancellationToken = cancellationSource.Token,
                    // The max parallelism is limited to 8 when the
                    // multiThreaded flag is enabled.
                    // This is because, if it is not limited, the lazy
                    // loading tests will start all requests almost
                    // immediately and then some will take so long to
                    // complete that they exceed the configured timeout.
                    MaxDegreeOfParallelism = (multiThreaded ? 8 : 1)
                };


                // Create a parallel loop to actually process the user agents.
                Parallel.ForEach(USER_AGENTS.GetRandomUserAgents(100), options,
                                 (useragent) =>
                {
                    // Create the flow data instance
                    using (var flowData = pipeline.CreateFlowData())
                    {
                        // Add the user agent to the flow data
                        // and process it
                        flowData.AddEvidence(
                            Pipeline.Core.Constants.EVIDENCE_HTTPHEADER_PREFIX +
                            Pipeline.Core.Constants.EVIDENCE_SEPERATOR + "User-Agent", useragent)
                        .Process();

                        // Make sure no errors occurred. If any did then
                        // cancel the parallel process.
                        if (flowData.Errors != null)
                        {
                            Assert.AreEqual(0, flowData.Errors.Count,
                                            $"Expected no errors but got {flowData.Errors.Count}" +
                                            $":{Environment.NewLine}{ReportErrors(flowData.Errors)}");
                            cancellationSource.Cancel();
                        }

                        // Get the device data instance and access the
                        // IsMobile property to ensure we can get
                        // data out.
                        var deviceData = flowData.Get <IDeviceData>();
                        var result     = deviceData.IsMobile;
                    }
                });
            }
        }