Ejemplo n.º 1
0
        public static void TestOperationsTimeElapsed(int maxItemsQuantity)
        {
            string searchTerm = CommandLine.RequestUserInput <string>("Search term: ");

            YouTubeAPIv3      api    = new YouTubeAPIv3();
            TimeElapsedResult result = api.TestOperationsTimeElapsed(maxItemsQuantity, searchTerm);

            CommandLine.WriteLine("\n");

            // Shows elapsed times for search list operation
            CommandLine.WriteLine("Search List Operation");
            CommandLine.WriteLine("----------------");
            Console.WriteLine(string.Format("Average: {0}", result.ElapsedSearch.Average()));
            Console.WriteLine(string.Format("Max: {0}", result.ElapsedSearch.Max()));
            CommandLine.WriteLine("\n");

            // Shows elapsed times for video list operation
            CommandLine.WriteLine("Video List Operation");
            CommandLine.WriteLine("----------------");
            Console.WriteLine(string.Format("Average: {0}", result.ElapsedVideo.Average()));
            Console.WriteLine(string.Format("Max: {0}", result.ElapsedVideo.Max()));
            CommandLine.WriteLine("\n");

            // Shows elapsed times for channel list operation
            CommandLine.WriteLine("Channel List Operation");
            CommandLine.WriteLine("----------------");
            Console.WriteLine(string.Format("Average: {0}", result.ElapsedChannel.Average()));
            Console.WriteLine(string.Format("Max: {0}", result.ElapsedChannel.Max()));
            CommandLine.WriteLine("\n");

            CommandLine.WriteLine(String.Format("Total Videos:{0}", result.Results.Count));

            CommandLine.PressAnyKeyToExit();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Test the time elapsed in different API operations, in order to evaluate performance.
        /// </summary>
        /// <param name="maxItemsQuantity"></param>
        /// <returns></returns>
        public TimeElapsedResult TestOperationsTimeElapsed(int maxItemsQuantity, string searchTerm)
        {
            // Counters
            TimeElapsedResult result = new TimeElapsedResult();

            var youtube = new YouTubeService(new BaseClientService.Initializer());

            SearchResource.ListRequest listRequest = youtube.Search.List("snippet");
            listRequest.Fields     = "items(id, snippet(title, description, publishedAt, thumbnails, channelId, channelTitle))";
            listRequest.Key        = API_KEY;
            listRequest.Type       = ResourceTypes.Video;
            listRequest.MaxResults = MAX_RESULTS_PER_PAGE;

            if (!string.IsNullOrEmpty(LOCATION))
            {
                listRequest.Location = LOCATION;
            }
            if (!string.IsNullOrEmpty(LOCATION_RADIUS))
            {
                listRequest.LocationRadius = LOCATION_RADIUS;
            }
            listRequest.Q     = searchTerm;
            listRequest.Order = SearchResource.ListRequest.OrderEnum.Date;

            var stopwatch = Stopwatch.StartNew();
            SearchListResponse searchResponse = listRequest.Execute();

            result.ElapsedSearch.Add(stopwatch.ElapsedMilliseconds);
            List <SearchResult> results    = new List <SearchResult>();
            List <string>       videosIds  = new List <string>();
            List <string>       channelIds = new List <string>();
            int currentCounter             = 0;

            while (searchResponse.Items.Count > 0 && currentCounter < maxItemsQuantity)
            {
                videosIds.AddRange(searchResponse.Items.Select(v => v.Id.VideoId));
                channelIds.AddRange(searchResponse.Items.Select(v => v.Snippet.ChannelId));
                results.AddRange(searchResponse.Items);
                // Gets oldest element
                var oldest = searchResponse.Items.OrderBy(i => i.Snippet.PublishedAt).FirstOrDefault();
                // Avoids getting the oldest again
                listRequest.PublishedBefore = oldest.Snippet.PublishedAt.Value.AddSeconds(-1);
                currentCounter += searchResponse.Items.Count;
                if (currentCounter < maxItemsQuantity)
                {
                    // Performs the search
                    stopwatch      = Stopwatch.StartNew();
                    searchResponse = listRequest.Execute();
                    result.ElapsedSearch.Add(stopwatch.ElapsedMilliseconds);
                }
            }

            // Retrieves videos recording details (location)
            List <string> videosToRetrieve = videosIds.Take(50).ToList();

            videosIds = videosIds.Skip(50).ToList();
            while (videosToRetrieve.Count > 0)
            {
                VideosResource.ListRequest videosRequest = youtube.Videos.List("recordingDetails");
                videosRequest.Key        = API_KEY;
                videosRequest.MaxResults = MAX_RESULTS_PER_PAGE;
                videosRequest.Id         = string.Join(",", videosToRetrieve.ToArray());

                stopwatch = Stopwatch.StartNew();
                VideoListResponse videosResponse = videosRequest.Execute();
                result.ElapsedVideo.Add(stopwatch.ElapsedMilliseconds);

                videosToRetrieve = videosIds.Take(50).ToList();
                videosIds        = videosIds.Skip(50).ToList();
            }

            // Retrieves channels
            List <string> channelsToRetrieve = channelIds.Take(50).ToList();

            channelIds = channelIds.Skip(50).ToList();
            while (channelsToRetrieve.Count > 0)
            {
                ChannelsResource.ListRequest channelRequest = youtube.Channels.List("snippet");
                channelRequest.Key        = API_KEY;
                channelRequest.MaxResults = MAX_RESULTS_PER_PAGE;
                channelRequest.Id         = string.Join(",", channelsToRetrieve.ToArray());

                stopwatch = Stopwatch.StartNew();
                ChannelListResponse channelsResponse = channelRequest.Execute();
                result.ElapsedChannel.Add(stopwatch.ElapsedMilliseconds);

                channelsToRetrieve = channelIds.Take(50).ToList();
                channelIds         = channelIds.Skip(50).ToList();
            }

            result.Results = results;

            return(result);
        }