Exemplo n.º 1
0
        void GenerateBigFile(IConfiguration config)
        {
            var random = new Random();

            using (var system = new FarmSystem(config))
            {
                Cow[] cows = new Cow[_totalCows];
                for (int i = 0; i < _totalCows; ++i)
                {
                    cows[i] = new Cow {
                        Code = "Cow " + i
                    };
                    system.Track(cows[i]);
                }

                int days = 0;
                foreach (DateTime day in (DateTime.Now - TimeSpan.FromDays(_totalDays)).DaysUntil(DateTime.Now))
                {
                    Console.Write("{0} ", ++days);
                    foreach (Cow cow in cows)
                    {
                        system.Milked(cow, day, (float)(2 * random.NextDouble()));
                        system.Milked(cow, day.AddHours(8), (float)(2 * random.NextDouble()));
                        system.Milked(cow, day.AddHours(8), (float)(2 * random.NextDouble()));
                        system.CustomEvent(cow, day, EventIds[random.Next(EventIds.Length)]);
                    }
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 2
0
        private void RunBenchmarks(IConfiguration config)
        {
            using (var system = new FarmSystem(config))
            {
                var cow1 = RandomCow(system);
                var cow2 = RandomCow(system);


                var code = RandomCowCode();
                Benchmark("select a single cow",
                          () => from c in system.Cows where c.Code == code select new { c.Code },
                          () => from c in new[] { system.OptimizedSingleCow(code) } select new { c.Code });

                // “find all cows whose average milk during august was less than 0.75 of the herd’s average.”
                var august    = new DateTime(2007, 8, 1);
                var september = august.AddMonths(1);

                Benchmark("find all cows whose average milk during august was less than 0.75 of the herd’s average.", () =>
                {
                    var herdAverage = system.HerdAverage(august, september);
                    var threshold   = .90 * herdAverage;
                    return(from c in system.Cows
                           let CowAverage = system.CowAverage(c, august, september)
                                            where CowAverage < threshold
                                            select new { c.Code, CowAverage });
                }, () =>
                {
                    var herdAverage = system.OptimizedHerdAverage(august, september);
                    var threshold   = .90 * herdAverage;
                    return(from c in system.Cows
                           let CowAverage = system.OptimizedCowAverage(c, august, september)
                                            where CowAverage < threshold
                                            select new { c.Code, CowAverage });
                });

                // The example queries:
                // A. For each cow, calculate total milk between date1 and date2
                Benchmark("For each cow, calculate total milk between date1 and date2",
                          () => from c in system.Cows
                          let CowTotal = system.CowTotal(c, august, september)
                                         select new { c.Code, CowTotal },
                          () => from c in system.Cows
                          let CowTotal = system.OptimizedCowTotal(c, august, september)
                                         select new { c.Code, CowTotal });


                // B. Find cows whose total(date1,date2) < X

                Benchmark("Find cows whose total(date1,date2) < X",
                          () => from c in system.Cows
                          let CowTotal = system.CowTotal(c, august, september)
                                         where CowTotal < 2
                                         select new { c.Code, CowTotal },
                          () => from c in system.Cows
                          let CowTotal = system.OptimizedCowTotal(c, august, september)
                                         where CowTotal < 2
                                         select new { c.Code, CowTotal });


                // C. Find cows whose total(date1,date2) < 0.75 * average total(date1,date2)

                // D. Days from last give_birth event for cows who are in active milking
                // [these are cows that have a "give_birth" event and no "drying" event
                // after it]

                Benchmark("Days in active milking",
                          () => from c in system.Cows
                          let LastGaveBirth = system.DateOfLastEvent(c, "give_birth")
                                              let LastDrying = system.DateOfLastEvent(c, "drying")
                                                               where LastDrying == null || LastGaveBirth > LastDrying
                                                               select new { c.Code, (DateTime.Now - LastGaveBirth.Value).TotalDays },
                          () => from c in system.OptimizedCowsThatEverGaveBirth()
                          let LastGaveBirth = system.OptimizedDateOfLastEvent(c, "give_birth")
                                              let LastDrying = system.OptimizedDateOfLastEvent(c, "drying")
                                                               where LastDrying == null || LastGaveBirth > LastDrying
                                                               select new { c.Code, (DateTime.Now - LastGaveBirth.Value).TotalDays });


                // E. How many cows gave birth in each month during the last year
                var lastYear    = new DateTime(2007, 1, 1);
                var lastYearEnd = lastYear.AddYears(1);

                Benchmark("How many cows gave birth in each month",
                          () => from e in system.Events
                          where e.Id == "give_birth" && e.Date >= lastYear && e.Date <= lastYearEnd
                          group e.Cow by e.Date.Month into g
                          select new
                {
                    Month = g.Key,
                    Count = g.Distinct().Count()
                },
                          () => from CustomCowEvent e in system.OptimizedEventRange("give_birth", lastYear, lastYearEnd)
                          group e.Cow by e.Date.Month into g
                          select new
                {
                    Month = g.Key,
                    Count = g.Distinct().Count()
                });
            }
        }
Exemplo n.º 3
0
        private Cow RandomCow(FarmSystem system)
        {
            var code = RandomCowCode();

            return((from c in system.Cows where c.Code == code select c).First());
        }