YoutubeExplode icon indicating copy to clipboard operation
YoutubeExplode copied to clipboard

Performance issues on mobile devices: GetVideoAsync and GetVideoMediaStreamInfosAsync

Open jungjanos opened this issue 7 years ago • 12 comments

Hi Alexey,

I have encountered very poor performance on mobile devices when awaiting the async methods GetVideoAsync and GetVideoMediaStreamInfosAsync. I have not tested any other async methods.

To be able to track performance I have created a very basic benchmark in Xamarin.Forms(Android) and have already provided benchmark results for phones of different performance levels.

The benchmark calls await GetVideoAsync() and in a different run GetVideoMediaStreamInfosAsync() on a collection of test Youtube-Id-s.

This collection is the same 10 piece collection, the YoutubeExplode.Test project uses.

Compiled APK, results and code can be found below: https://github.com/j4ni/YoutubeExplodeXamarinBenchmark/blob/master/README.md

Regards Janos

jungjanos avatar Aug 14 '19 19:08 jungjanos

This is indeed pretty abnormal. I assume it's caused by slow parser performance https://github.com/Tyrrrz/YoutubeExplode/pull/274#issuecomment-512970706, I guess the slowdown is amplified on mobile.

Tyrrrz avatar Aug 14 '19 19:08 Tyrrrz

Low end phone (Redmi 3S Prime with Qualcomm Snapdragon 430), 10x invocations: .GetVideoAsync() ~ 40sec .GetVideoMediaStreamInfosAsync() ~ 24sec

Just to clarify, those times are per invokation, right? Not aggregated from 10 invokations?

Tyrrrz avatar Aug 14 '19 20:08 Tyrrrz

Just to clarify, those times are per invokation, right? Not aggregated from 10 invokations?

No, that is the aggregate result of 10 invocations based on YoutubeExplde's 10 item test collection in the test project.

Testlist: https://github.com/j4ni/YoutubeExplodeXamarinBenchmark/blob/master/XamAppWithYtE/XamAppWithYtE/Testdata.cs

jungjanos avatar Aug 14 '19 20:08 jungjanos

So in other words 4 sec per GetVideoAsync and 2.4 sec per GetVideoMediaStreamInfosAsync?

Tyrrrz avatar Aug 14 '19 20:08 Tyrrrz

Yes,

4sec and respectivelly 2.4 sec on average. But you shouldn't think in terms of averages as there is great variance in execution time between different videoId-s. E.g.

GetVideoMediaStreamInfosAsync("5VGm0dczmHc") always takes ~4-6sec (instead of ~2.4) whereas the same call for some VideoId-s takes only 1.4sec.

GetVideoAsync exhibits a bit less variance between videoIds (based on the used test-collection with 10 video ids) but also there is some variance. ~5sec maximum and ~2,3sec minimum generally. But most of them close to 4 sec.

There is the APK I have uploaded to the repo I linked above, you can try it on your phone too... :)

jungjanos avatar Aug 14 '19 21:08 jungjanos

Okay so it's not as bad as I thought.

The reason for variance in GetVideoMediaStreamInfosAsync is because there are many different types of videos and they are handled differently by YoutubeExplode. There's not much we can do about that unfortunately.

Tyrrrz avatar Aug 14 '19 21:08 Tyrrrz

There's not much we can do about that unfortunately.

I am not familiar with YoutubeExplode structure or parsing Html in general, so it might not be feasible, but:

Did you think about constructing YoutubeClient with an optional settings object containing a flags enum parameters (for Video and Streams objects) declaring what information the library consumer wants YtE to parse from the Html. When called, YtE would then only parse the required information and would only partially populate the resulting model objects. (giving null or throwing when a property is accessed which was not contracted by the settings object)

As I said I am not familiar with web scraping or Html parsing in general, but I would think, that way large parts of the CPU intensive parsing could be foregone. Providing an optional constructor parameter would I suppose not be a breaking changes to the library.

jungjanos avatar Aug 14 '19 21:08 jungjanos

To shed some light on what I am using YoutubeExplode for: My current (hobby-learning) application includes the following use case:

The app is a mobile movie-info app providing different kinds of information about a selected movie to the user. These are among others: Movie title, actors, genre, rating, movie poster images etc. The user can toggle the image-view to switch from movie poster/backdrop images to video thumbnail images (moviedb's WebAPI provides YT video Ids with some other basic info like video title ). These videos are the movie trailers on Youtube associated with the movie the user is interested in. On the App UI beside the thumbnail there are some best effort information (e.g. title like "Official Avatar 2 trailer") to give some context. The user then swipes between the youtube-thumbnails and taps on one of them to watch the Youtube hosted movie trailer.

The bottom line is, that its not needed to recreate a full functionality youtube client with every possible piece of information scraped and displayed. Some very basic information is enough as add free YT videos are only a small (but sweet and flashy) convenience feature to the user. For me its really enough to scrape those infos the user actually needs instead of doing a through parsing. Speed is much more important than completeness of pop culture data.

jungjanos avatar Aug 14 '19 22:08 jungjanos

What is the time and dependency structure of the parsing GetVideoAsync and GetVideoMediaStreamInfosAsync are doing on the Html (I am not really familiar with web scraping)?

  • Is the parsing of the different information pieces largely independent each taking a significant portion of the total compute of a full parsing?

  • Or is the parsing of every piece of information related to other pieces so a full parsing of the html can not be avoided?

  • Is there some large initial work to be done that can not be avoided and which takes so much time, that even if small information pieces are required, a partial parsing would take nearly as much time as a full parsing?

  • If parsing multiple sections of the document can not be avoided, can those overlap in time? Even those puny mobile SoCs have many cores...

Thanks for the kind answers!

jungjanos avatar Aug 14 '19 22:08 jungjanos

Implemented new testset (cinema movie trailers), added new results: https://github.com/j4ni/YoutubeExplodeXamarinBenchmark/blob/master/README.md

In short:

  • GetVideoAsync is 20% slower
  • GetVideoMediaStreamInfosAsyn is 50% faster

when compared to the original testset.

jungjanos avatar Aug 15 '19 11:08 jungjanos

Did you think about constructing YoutubeClient with an optional settings object containing a flags enum parameters (for Video and Streams objects) declaring what information the library consumer wants YtE to parse from the Html. When called, YtE would then only parse the required information and would only partially populate the resulting model objects. (giving null or throwing when a property is accessed which was not contracted by the settings object)

As I said I am not familiar with web scraping or Html parsing in general, but I would think, that way large parts of the CPU intensive parsing could be foregone. Providing an optional constructor parameter would I suppose not be a breaking changes to the library.

That would be hell to test and maintain, but that doesn't really help anyway. GetVideoAsync and GetMediaStreamInfosAsync are pretty granular. In fact, in the past YoutubeExplode used to only have GetVideoAsync which would pull all of the data, until it was split into 4 methods in version 3.x or 4.x, I think.

There's one CPU-bound bottleneck that I'm planning to deal with, which is related to parsing. It will make things faster, but those methods are still also I/O-bound by HTTP requests that they make, including a 1mb request for the player source which is required on some videos, and which as you've noticed causes variance in your benchmarks. For example, on PC, GetVideoMediaStreamInfosAsync completes in about 900ms on average, with around 200ms of that taken up by CPU-bound operations, rest is I/O.

Is the parsing of the different information pieces largely independent each taking a significant portion of the total compute of a full parsing?

There's no parsing of individual pieces per se, they are extracted from the document which is parsed as a whole.

Or is the parsing of every piece of information related to other pieces so a full parsing of the html can not be avoided?

Full parsing of HTML cannot be avoided because HTML has a complex tree structure.

Is there some large initial work to be done that can not be avoided and which takes so much time, that even if small information pieces are required, a partial parsing would take nearly as much time as a full parsing?

The HTTP request itself, i.e. the I/O-bound operation.

If parsing multiple sections of the document can not be avoided, can those overlap in time? Even those puny mobile SoCs have many cores...

Like I said, there's no "individual item parsing" so it wouldn't make sense here.

Also, please note that the main goal of YoutubeExplode is to let users download YouTube videos. In general, being offset by 1mb HTTP requests is not ideal, but seeing as it leads up to downloading a 500mb video stream, that might be fine in grand scheme of things.

Tyrrrz avatar Aug 15 '19 15:08 Tyrrrz

Hi Alexey, thanks for the fast replay!

There's one CPU-bound bottleneck that I'm planning to deal with, which is related to parsing. It will make things faster, but those methods are still also I/O-bound by HTTP requests that they make .. completes in about 900ms on average, with around 200ms of that taken up by CPU-bound operations, rest is I/O.

Whether you are more IO or CPU bound also depends on the computing platform. having a 1/4 as powerful CPU (lower end phone) will increase the time of CPU bound parts fourfold. Time consumption of the downloading of some fixed size of data does not really scale with what platform you are connected from.

When you look at the numbers I posted (I also have run the test on a different set of YouTube Id-s, see results in the shared rep.) You will see that GetVideoAsync() can consume 5 seconds of time for a single call (depending on video). That 5 seconds is surely 80% percent of the time CPU bound, as I was connected from WiFi. Even the Pocophone took 2-3 seconds (I havent yet received the results for the newest run on the Pocophone with the new testset).

being offset by 1mb HTTP requests is not ideal, but seeing as it leads up to downloading a 500mb video stream, that might be fine in grand scheme of things

Yes, if one wants to download a 500MB video, than a few seconds for the parsing is fine.

Out of curiosity so that I can better understand the constraints:

  1. GetVideoAsync awaits two Http-requests in two steps: these are:
            // Get video info dictionary
            var videoInfoDic = await GetVideoInfoDicAsync(videoId).ConfigureAwait(false);

            // Get video watch page HTML
            HtmlDocument videoWatchPageHtml = await GetVideoWatchPageHtmlAsync(videoId).ConfigureAwait(false);

in between you have synchronous methods which only depend on the results of the former async call not the later.

Would it not be possible to fetch the two Youtube HttpResponses overlapping in time and awaiting before the parsing has to be done? Does Youtube allow that?

Also half of the data GetVideoAsync returns comes from GetVideoInfoDicAsync and the other half comes from GetVideoWatchPageHtmlAsync which are then composed into a single Model object.

What is the relationship of the amount of work your two async submethods are doing? They both have IO and at least I suppose the first one is CPU bound with the subsequent parser call.

Is GetVideoWatchPageHtmlAsync's work neglegibe compared to GetVideoInfoDicAsync ?

Best Regards Janos

jungjanos avatar Aug 15 '19 16:08 jungjanos