Пример #1
0
        static void Parser2()
        {
            var emea = GeoRegion.Regions.First(area => area.Id == "amer");

            var geodata = GeoData.FromResource();

            var country = emea.Selector(geodata);


            var freqs = Enumerable.Range(0, 10000).Select(i => country()).GroupBy(c => c.Country.Name)
                        .ToDictionary(g => g.Key, g => g.Count());

            File.WriteAllLines(@"C:\Temp\Countries.txt", new[] { "Country\tCount" }.Concat(freqs.OrderByDescending(kv => kv.Value).Select(kv => kv.Key + "\t" + kv.Value)));

            return;



            Randomness.Seed(1337);


            var json = File.ReadAllText(@"C:\Temp\Xerox2.js");

            var def    = JObject.Parse(json);
            var parser = new XGenParser("http://sc80rev150427/api/xgen/");



            //var vars = new[]{""}


            var sw = Stopwatch.StartNew();

            var visits    = 0;
            var id        = 0;
            var contactId = 0;

            using (var f = File.CreateText(@"C:\Temp\XeroxOut2.txt"))
            {
                f.Write("Contact\tId\tVisitIndex\tStart\tEnd\tHour\tDuration\tPageViews\tCampaign\tCountry\tTimeZone\tContinent\tReferrer\tLandingPage\tSite");
                f.WriteLine();

                var threads = Enumerable.Range(0, 25).Select(i =>
                {
                    var t = new Thread(() =>
                    {
                        Randomness.Seed(1337 + i);
                        var segments = parser.ParseSegments(def["Segments"], JobType.Visits);
                        var sim      = new SegmentBasedSimulator(segments);

                        foreach (var v in sim.NextVisitors(80))
                        {
                            var myId = Interlocked.Increment(ref contactId);
                            System.Console.Out.WriteLine("Visitor at {0}", v.Start);
                            try
                            {
                                var visitIndex = 0;
                                foreach (var visit in v.Segment.Behavior().Commit(v))
                                {
                                    Interlocked.Increment(ref visits);
                                    System.Console.Out.WriteLine(" - Visit at {0}", visit.Start);
                                    //foreach (var req in visit.Requests)
                                    //{
                                    //    System.Console.Out.WriteLine("   " + req.Url);
                                    //}

                                    //var visit = v.AddVisit();
                                    //visit.AddRequest("");
                                    lock (f)
                                    {
                                        f.Write("Contact" + myId);
                                        f.Write("\t");
                                        f.Write("Visit" + Interlocked.Increment(ref id));
                                        f.Write("\t");
                                        f.Write(++visitIndex);
                                        f.Write("\t");
                                        f.Write(v.Start.Date.ToString("yyyy-MM-dd"));
                                        f.Write("\t");
                                        f.Write(v.End.Date.ToString("yyyy-MM-dd"));
                                        f.Write("\t");
                                        f.Write(v.Start.Hour);
                                        f.Write("\t");
                                        f.Write((v.End - v.Start).TotalSeconds);
                                        f.Write("\t");
                                        f.Write(visit.GetVariable <double>("PageViews"));
                                        f.Write("\t");
                                        f.Write(visit.GetVariable("Campaign", ""));
                                        f.Write("\t");
                                        f.Write(visit.GetVariable("Country", ""));
                                        f.Write("\t");
                                        f.Write(visit.GetVariable("TimeZone", ""));
                                        f.Write("\t");
                                        f.Write(visit.GetVariable("Continent", ""));
                                        f.Write("\t");
                                        f.Write(visit.GetVariable("Referrer", ""));
                                        f.Write("\t");
                                        f.Write(visit.GetVariable("LandingPage", ""));
                                        f.Write("\t");
                                        f.Write(visit.GetVariable("Site", ""));

                                        f.WriteLine();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                System.Console.Out.WriteLine("Error: " + ex);
                            }
                            System.Console.Out.WriteLine("{0:N0} visits ({1:N2} visits per second)", visits, visits / sw.Elapsed.TotalSeconds);
                        }
                    });
                    t.Start();
                    return(t);
                }).ToArray();

                foreach (var t in threads)
                {
                    t.Join();
                }

                System.Console.Out.WriteLine("{0:N0} visits in {1:N2} seconds", visits, sw.Elapsed.TotalSeconds);
            }
        }
Пример #2
0
        static XGenParser()
        {
            GeoData = GeoData.FromResource();

            Factories = new Dictionary <string, VariableFactory>();

            Factories.Add("PageViews", VariableFactory.Lambda((segment, token, parser) =>
                                                              segment.VisitVariables.AddOrReplace(Variables.Random("PageViews",
                                                                                                                   new PoissonGenerator(token.Value <double>()).Truncate(1, 20)))));

            Factories.Add("VisitCount", VariableFactory.Lambda((segment, token, parser) =>
                                                               segment.VisitorVariables.AddOrReplace(Variables.Random("VisitCount",
                                                                                                                      new PoissonGenerator(token.Value <double>()).Truncate(1, 20)))));

            Factories.Add("BounceRate", VariableFactory.Lambda((segment, token, parser) =>
                                                               segment.VisitVariables.AddOrReplace(Variables.Boolean("Bounce", token.Value <double>()))));

            Factories.Add("Duration", VariableFactory.Lambda((segment, token, parser) =>
            {
                var mean = token.Value <double>();
                segment.RequestVariables.AddOrReplace(Variables.Duration(new SkewNormalGenerator(mean, mean, 3), 1));
            }));


            Factories.Add("StartDate", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.Start = token.Value <DateTime>()));
            Factories.Add("EndDate", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.End     = token.Value <DateTime>()));


            Factories.Add("DayOfWeek", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.DayOfWeek(t =>
                                                                                                                          t.Clear().Weighted(builder =>
            {
                foreach (var kv in (JObject)token)
                {
                    DayOfWeek day;
                    builder.Add(
                        Enum.TryParse(kv.Key, out day) ? (int)day :
                        int.Parse(kv.Key), kv.Value.Value <double>());
                }
            })
                                                                                                                          )));

            Factories.Add("YearlyTrend", VariableFactory.Lambda((segment, token, parser) =>
            {
                if (token.Value <double>() != 1)
                {
                    segment.DateGenerator.Year(
                        t => t.Clear().MoveAbsolutePercentage(0).LineAbsolutePercentage(1, token.Value <double>()));
                }
                //segment.DateGenerator.YearWeight = 1;
            }));

            Factories.Add("Month", new MonthFactory());

            Factories.Add("Identified",
                          VariableFactory.Lambda((segment, token, parser) =>
                                                 segment.VisitorVariables.AddOrReplace(
                                                     new ContactDataVariable(token.Value <double>()))));


            Factories.Add("Campaign", VariableFactory.Lambda((segment, token, parser) =>
            {
                var campaignPct = token.Value <double?>("Percentage") ?? 1;
                var campaigns   = parser.ParseWeightedSet <string>(token["Weights"]);
                segment.VisitVariables.AddOrReplace(Variables.Random("Campaign",
                                                                     () => Randomness.Random.NextDouble() < campaignPct ? campaigns() : null, true));
            }));


            Factories.Add("Channel", VariableFactory.Lambda((segment, token, parser) =>
            {
                var channelPct = token.Value <double?>("Percentage") ?? 1;
                var channels   = parser.ParseWeightedSet <string>(token["Weights"]);
                segment.VisitVariables.AddOrReplace(Variables.Random("Channel",
                                                                     () => Randomness.Random.NextDouble() < channelPct ? channels() : null, true));
            }));


            Factories.Add("Referrer", VariableFactory.Lambda((segment, token, parser) =>
                                                             segment.VisitVariables.AddOrReplace(Variables.Random("Referrer",
                                                                                                                  parser.ParseWeightedSet <string>(token), true))));

            var subregions = GeoRegion.Regions.SelectMany(x => x.SubRegions);
            var areas      = subregions.ToDictionary(ga => ga.Id, ga => ga.Selector(GeoData));


            Factories.Add("Geo", VariableFactory.Lambda((segment, token, parser) =>
            {
                var regionId = parser.ParseWeightedSet <string>(token["Region"]);
                segment.VisitorVariables.AddOrReplace(new GeoVariables(() => areas[regionId()](), true));
            }));


            var devices = new DeviceRepository().GetAll().ToDictionary(ga => ga.Id, ga => ga);

            Factories.Add("Devices", VariableFactory.Lambda((segment, token, parser) =>
            {
                Func <string> userAgent;
                if (!token.HasValues)
                {
                    userAgent = FileHelpers.ReadLinesFromResource <GeoData>("ExperienceGenerator.Data.useragents.txt")
                                .Exponential(.8, 8);
                }
                else
                {
                    var id    = parser.ParseWeightedSet <string>(token);
                    userAgent = () => devices[id()].UserAgent;
                }
                segment.VisitorVariables.AddOrReplace(Variables.Random("UserAgent", userAgent));
            }));

            Factories.Add("Outcomes", VariableFactory.Lambda((segment, token, parser) =>
            {
                var value = new NormalGenerator(10, 5).Truncate(1);
                segment.VisitVariables.AddOrReplace(new OutcomeVariable(parser.ParseSet <string>(token), value.Next));
            }));

            Factories.Add("InternalSearch", VariableFactory.Lambda((segment, token, parser) =>
            {
                var searchPct = token.Value <double?>("Percentage") ?? 0.2;
                var keywords  = parser.ParseWeightedSet <string>(token["Keywords"]);
                segment.VisitVariables.AddOrReplace(Variables.Random("InternalSearch",
                                                                     () => Randomness.Random.NextDouble() < searchPct ? keywords() : null, true));
            }));

            var searchEngines = SearchEngine.SearchEngines.ToDictionary(s => s.Id);

            Factories.Add("ExternalSearch", VariableFactory.Lambda((segment, token, parser) =>
            {
                var searchPct = token.Value <double?>("Percentage") ?? 0.2;
                var keywords  = parser.ParseWeightedSet <string>(token["Keywords"]);

                var engineId = parser.ParseWeightedSet <string>(token["Engine"]);

                segment.VisitVariables.AddOrReplace(new ExternalSearchVariable(
                                                        () =>
                                                        Randomness.Random.NextDouble() >= searchPct
              ? null
              : searchEngines[engineId()],
                                                        () => new[]
                {
                    keywords()
                }));
            }));


            Factories.Add("Language", VariableFactory.Lambda((segment, token, parser) =>
            {
                var languages = parser.ParseWeightedSet <string>(token);
                segment.VisitVariables.AddOrReplace(Variables.Random("Language", languages));
            }));

            Factories.Add("LandingPage", new LandingPageFactory());
        }