wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
342 stars 130 forks source link

Technical Issue | Apply AsyncFixer suggestions #53

Closed polushinmk closed 7 years ago

polushinmk commented 7 years ago

Hi! Some time ago I found on the Internet a link to the plug-in for Visual Studio Async Fixer https://visualstudiogallery.msdn.microsoft.com/03448836-db42-46b3-a5c7-5fc5d36a8308_blank. This plugin analyzes the use of async / await in the code and suggests correcting its incorrect use. At us, according to a plug-in, about 30 incorrect calls async await (AsyncFixer01) which are not necessary, but thus it is generated overhead code.

I decided to run tests using BenchmarkDotNet. The Categories.Query () method was called 25 times with parameters of 100 categories per page The results in the tables below: Using Async / await


BenchmarkDotNet=v0.10.9, OS=Windows 7 SP1 (6.1.7601)
Processor=Intel Xeon CPU X5675 3.07GHz, ProcessorCount=4
Frequency=14318180 Hz, Resolution=69.8413 ns, Timer=HPET
.NET Core SDK=2.0.0
  [Host]     : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT
  Job-CVVEAN : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT

LaunchCount=1  RunStrategy=Monitoring  TargetCount=25  
WarmupCount=5  
Method Mean Error StdDev Min Max
GetCategories 1.274 s 0.1699 s 0.2268 s 1.112 s 2.249 s

Without using async / await


BenchmarkDotNet=v0.10.9, OS=Windows 7 SP1 (6.1.7601)
Processor=Intel Xeon CPU X5675 3.07GHz, ProcessorCount=4
Frequency=14318180 Hz, Resolution=69.8413 ns, Timer=HPET
.NET Core SDK=2.0.0
  [Host]     : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT
  Job-QRTQAA : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT

LaunchCount=1  RunStrategy=Monitoring  TargetCount=25  
WarmupCount=5  
Method Mean Error StdDev Min Max
GetCategories 1.337 s 0.1493 s 0.1993 s 1.095 s 1.929 s

That is, there is a gain in performance, but it is very small. In a real application, the benefit is likely to be quite insensitive, but overall, it cleans up the code a little. What do you think, can be applied recommendations?

ThomasPe commented 7 years ago

I'll have to look into this on the weekend - I'm not quite sure where we actually could get rid off aync-Statements... If there are any that won't be needed, I'm not against removing those.

However, with these kind of libraries the Web-Requests will always be the bottleneck, so I don't think wie should overthink this.

ThomasPe commented 7 years ago

Could you give me an example of what should be changed and how?

polushinmk commented 7 years ago

Now:

public async Task<IEnumerable<Comment>> GetCommentsForPost(string PostID, bool embed = false, bool useAuth = false)
        {
            return await _httpHelper.GetRequest<IEnumerable<Comment>>($"{_defaultPath}{_methodPath}?post={PostID}", embed, useAuth);
        }

Then

public Task<IEnumerable<Comment>> GetCommentsForPost(string PostID, bool embed = false, bool useAuth = false)
        {
            return _httpHelper.GetRequest<IEnumerable<Comment>>($"{_defaultPath}{_methodPath}?post={PostID}", embed, useAuth);
        }

List of methods:

AsyncFixer01 The method 'GetCommentsForPost' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Comments.cs 39
AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Comments.cs 50
AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\CRUDOperation.cs 71
AsyncFixer01 The method 'GetByID' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\CRUDOperation.cs 104 AsyncFixer01 The method 'Query' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\CRUDOperation.cs 115 AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\CustomRequest.cs 44
AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Media.cs 58
AsyncFixer01 The method 'GetByID' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Media.cs 91
AsyncFixer01 The method 'Query' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Media.cs 102 AsyncFixer01 The method 'GetPagesByAuthor' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Pages.cs 38
AsyncFixer01 The method 'GetPagesBySearch' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Pages.cs 52
AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Pages.cs 65
AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\PostRevisions.cs 40
AsyncFixer01 The method 'GetAll' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\PostRevisions.cs 50
AsyncFixer01 The method 'GetByID' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\PostRevisions.cs 61
AsyncFixer01 The method 'GetStickyPosts' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Posts.cs 37
AsyncFixer01 The method 'GetPostsByCategory' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Posts.cs 51
AsyncFixer01 The method 'GetPostsByTag' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Posts.cs 65
AsyncFixer01 The method 'GetPostsByAuthor' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Posts.cs 79
AsyncFixer01 The method 'GetPostsBySearch' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Posts.cs 93
AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Posts.cs 106 AsyncFixer01 The method 'GetByID' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\PostStatuses.cs 54
AsyncFixer01 The method 'GetByID' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\PostTypes.cs 54
AsyncFixer01 The method 'GetByID' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Taxonomies.cs 54
AsyncFixer01 The method 'GetByID' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Users.cs 76
AsyncFixer01 The method 'Query' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Users.cs 87
AsyncFixer01 The method 'GetCurrentUser' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Users.cs 109 AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Users.cs 120 AsyncFixer01 The method 'Delete' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\Client\Users.cs 131 AsyncFixer01 The method 'GetSettings' do not need to use async/await. WordPressPCL C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCL\WordPressClient.cs 133 AsyncFixer01 The method 'CreateRandomUser' do not need to use async/await. WordPressPCLTests C:\Users\polushin_m_k\Source\Repos\WordPressPCL\WordPressPCLTests\User_Tests.cs 152

ThomasPe commented 7 years ago

Ah, so you would just remove the async & await parts, but this would not change the way the public methods would be called, right? Then we might as well just remove them. :-)

polushinmk commented 7 years ago

All right! :)