paint-brush
Async vs Sync Benchmark (.NET): Musiyano uripakati peAsynchronous uye Synchronous Methodsby@artemmikulich
1,892 kuverenga
1,892 kuverenga

Async vs Sync Benchmark (.NET): Musiyano uripakati peAsynchronous uye Synchronous Methods

by Artem Mikulich5m2024/09/21
Read on Terminal Reader

Kurebesa; Kuverenga

Ichi chinyorwa chinoratidza musiyano pakati peasynchronous uye synchronous nzira mukuita. Mhashu mbiri dzakazvimirira dziri kushanda pamichina miviri. Huwandu hwevashandisi hunokura zvakaenzana kusvika kunhamba yakatarwa (* Nhamba yeVashandisi*). Iko kumhanya kwekukura kunodzorwa ne * Spawn Rate * paramende (nhamba yevashandisi vakasiyana kujoina pasekondi)
featured image - Async vs Sync Benchmark (.NET): Musiyano uripakati peAsynchronous uye Synchronous Methods
Artem Mikulich HackerNoon profile picture

Imwe yemibvunzo yandinofarira yekubvunzurudza ndeye "Mazwi akadai seasync uye akamirira anokuudza chii?" nekuti zvinovhura mukana wekuita nhaurirano inofadza neanobvunzurudzwa… Kana kuti hazvidaro nekuti vanoyangarara pamusoro pechinyorwa ichi. Mune maonero angu, zvakakosha zvikuru kunzwisisa kuti nei tichishandisa nzira iyi.


Ini ndinonzwa sekunge vazhinji vanogadzira vanosarudza kuvimba ne "ndiyo yakanakisa tsika" chirevo uye kushandisa asynchronous nzira nehupofu.


Ichi chinyorwa chinoratidza musiyano pakati peasynchronous uye synchronous nzira mukuita.

Zvishandiso

  • .NET Web API application (test target)


  • 2 Azure SQL Databases


  • 2 Azure App Service paWindows (inobata application)


  • Azure App Insights (kuunganidza metrics)


  • mhashu chimiro (kutevedzera mutoro wemushandisi).

Configuration

Edza schema

Ini ndichamhanyisa bhenji nenzira inotevera. Mhashu mbiri dzakazvimirira dziri kushanda pamichina miviri. Mhashu zviitiko zvinotevedzera mushandisi anoita zvinotevera:

  • Mushandisi kubva kumutambi wemhashu 1 anorova iyo synchronous endpoint yeApp Service 1, anowana mhinduro, uye anogara asina chaanoita kwemasekonzi 0.5-1 (iyo chaiyo kunonoka kunonoka). Inodzokorora kusvika pakupera kwekuedza.


  • Mushandisi kubva kumumhashu 2 anoita zvakangofanana, aine musiyano mumwe chete - anorova asynchronous endpoint yeApp Service 2.


Pasi pehodhi, imwe neimwe App Service inobatanidza kune yayo yega dhatabhesi uye inoita SELECT query inotora masekonzi mashanu uye inodzosa mitsetse mishoma yedata. Ona kodhi yemutongi pazasi kuti uwane mareferensi. Ini ndichashandisa Dapper kufona kune database. Ndinoda kukwevera pfungwa dzako kune chokwadi chekuti iyo asynchronous endpoint inodaidza dhatabhesi asynchronously zvakare ( QueryAsync<T> ).


App Services kodhi


Zvakakodzera kuwedzera kuti ini ndinotumira iyo kodhi yakafanana kune ese ari maviri app masevhisi.


Munguva yekuedzwa, nhamba yevashandisi inokura zvakaenzana kusvika kunhamba inotarirwa ( Nhamba yeVashandisi ). Iko kumhanya kwekukura kunodzorwa neSpawn Rate parameter (nhamba yevashandisi vakasiyana kujoina pasekondi) - iyo yakakwira iyo nhamba, inokurumidza vashandisi vari kuwedzerwa. The spawn rate inotarwa kune gumi vashandisi/s kune zvese zviedzo.


Kuedza kwese kunogumira kumaminitsi gumi nemashanu.


Unogona kuwana ruzivo rwemuchina muchikamu cheTechnical Details chechinyorwa.

Metrics

  • zvikumbiro paminiti - inoratidza huwandu hwezvikumbiro izvo application yakanyatsogadziriswa uye yakadzosa kodhi yemamiriro.
  • thread count - inoratidza nhamba yeshinda iyo app service inoshandisa.
  • nguva yekupindura yepakati, ms


Mitsetse mitsvuku inoreva asynchronous, uye mitsara yebhuruu - kune iyo synchronous endpoint, zvichiteerana.


Ndizvo zviri pamusoro pedzidziso. Ngatitange.

Chiyedzo #1

  • nhamba yevashandisi : 75 (pasevhisi)


Isu tinogona kuona kuti ese ari maviri ekupedzisira anoita zvakafanana - kubata akatenderedza 750 zvikumbiro paminiti ine yepakati mhinduro nguva ye5200 ms.


Chiyedzo #1. Zvikumbiro paminiti


Iyo inonyanya kunakidza girafu muchiyedzo ichi ndeye thread maitiro. Iwe unogona kuona yakakwira nhamba dzeiyo synchronous endpoint (girafu rebhuruu) - anopfuura zana shinda!


Chiyedzo #1. Thread count


Izvo zvinotarisirwa, zvakadaro, uye zvinoenderana nedzidziso - kana chikumbiro chapinda, uye chikumbiro chikaita runhare kune dhatabhesi, tambo yakavharwa nekuti inofanirwa kumirira kutenderera kupedzisa. Naizvozvo, kana chimwe chikumbiro chapinda, chikumbiro chinofanira kuburitsa shinda nyowani yekuchibata.


Girafu dzvuku - iyo asynchronous endpoint thread count- inoratidza maitiro akasiyana. Kana chikumbiro chikauya uye chikumbiro chikaita kufona kune dhatabhesi, iyo tambo inodzokera kune tambo dziva pane kuvharwa. Naizvozvo, kana chimwe chikumbiro chapinda, iyi tambo yemahara inoshandiswa zvakare. Kunyangwe zvikumbiro zvinouya zvichikura, chishandiso hachidi chero tambo nyowani, saka kuverenga kwavo kunoramba kwakafanana.


Zvakakodzera kutaura iyo 3rd metric — median mhinduro nguva . Mapeji ese ari maviri airatidza mhedzisiro yakafanana - 5200 ms. Saka, hapana musiyano maererano nekuita.


Chiyedzo #1. Summary


Zvino, yave nguva yekudhonza matanda.

Chiyedzo #2

  • nhamba yevashandisi : 150


Takawedzera mutoro. Mugumo usingaenzaniswi unobata basa iri zvinobudirira— chikumbiro chayo paminiti imwe neimwe chinoyangarara chakapoteredza 1500. Hama inowirirana yakazosvika nhamba inoenzana ne1410. Asi kana ukatarisa girafu riri pasi apa, uchaona kuti zvakatora maminitsi gumi!


Chikonzero ndechekuti iyo synchronous endpoint inopindirana nekusvika kwemushandisi mutsva nekugadzira imwe tambo, asi vashandisi vari kuwedzerwa kune sisitimu (kungokuyeuchidzai kuti Spawn Rate ndeye gumi vashandisi / s) nekukurumidza kupfuura iyo webhu server inogona kuchinjika. Ndosaka yakaronga zvikumbiro zvakawanda pakutanga.


Chiyedzo #2. Zvikumbiro paminiti


Zvisingashamise, iyo tambo yekuverenga metric ichiri kutenderera makumi matatu nemana kune iyo asynchronous endpoint, nepo yakawedzera kubva pa102 kuenda ku155 kune inowirirana. Nguva yekupindura yepakati yakaderedzwa zvakafanana nekukumbira paminiti mwero - iyo synchronous yekupindura nguva yaive yakakwira zvakanyanya pakutanga kwekuyedza. Dai ndakachengeta bvunzo kwemaawa makumi maviri nemana, nhamba dzepakati dzaizoenzana.


Chiyedzo #2. Summary


Chiyedzo #3

  • nhamba yevashandisi : 200


Kuedza kwechitatu kunoitirwa kuratidza mafambiro akaratidzwa panguva yechipiri-tinogona kuona kumwe kuderedzwa kweiyo synchronous endpoint.


Chiyedzo #3. Summary


Mhedziso

Kushandisa asynchronous pachinzvimbo che synchronous mashandiro haagadzirise zvakananga kuita kana ruzivo rwemushandisi. Kutanga, inowedzera kugadzikana uye kufanotaura pasi pekumanikidzwa. Mune mamwe mazwi, inosimudza chikumbaridzo chemutoro kuitira kuti sisitimu igadzirise zvakanyanya isati yadzikisira.

Appendikisi #1. Technical Details

  • Azure App Service: B1, 100 ACU , 1,75 Gb Memory, A-Series compute yakaenzana.
  • Azure SQL Database: Standard S4: 200 DTUs, 500 Mb kuchengetedza.
  • SQL Connection marongero: Max Pool Size=200.

Appendikisi #2. Notes

Kuti ndiwane yakachena bvunzo mhedzisiro, ndinofanira kunge ndakamhanyisa bvunzo kubva ku2 VMs iri mune imwecheteyo network panogara iyo inotarirwa App Services.


Nekudaro, ini ndakafungidzira kuti network lag yaizokanganisa ese maapplication nenzira yakawedzera kana kushoma yakafanana. Naizvozvo, haigone kukanganisa chinangwa chikuru - kuenzanisa maitiro asynchronous uye synchronous maitiro.

Appendikisi #3. Bonus Kuedza

Chii chandakabira kumanikidza iyo inopindirana magumo kuti iite senge asynchronous uye kuronga girafu pazasi (mamiriro ekuyedza akafanana neyechitatu - vashandisi mazana maviri)?


Bonus Kuedza. Zvikumbiro paminiti