Best Python code snippet using locust
dispatch.py
Source:dispatch.py  
1"""2This file contains a benchmark to validate the performance of Locust itself.3More precisely, the performance of the `UsersDispatcher` class which is responsible4for calculating the distribution of users on each workers. This benchmark is to be used5by people working on Locust's development.6"""7import itertools8import statistics9import time10from prettytable import PrettyTable11from locust import User12from locust.dispatch import UsersDispatcher13from locust.runners import WorkerNode14class User1(User):15    weight = 516class User2(User):17    weight = 5518class User3(User):19    weight = 3720class User4(User):21    weight = 222class User5(User):23    weight = 9724class User6(User):25    weight = 4126class User7(User):27    weight = 3328class User8(User):29    weight = 1930class User9(User):31    weight = 1932class User10(User):33    weight = 3434class User11(User):35    weight = 7836class User12(User):37    weight = 7638class User13(User):39    weight = 2840class User14(User):41    weight = 6242class User15(User):43    weight = 6944class User16(User):45    weight = 546class User17(User):47    weight = 5548class User18(User):49    weight = 3750class User19(User):51    weight = 252class User20(User):53    weight = 9754class User21(User):55    weight = 4156class User22(User):57    weight = 3358class User23(User):59    weight = 1960class User24(User):61    weight = 1962class User25(User):63    weight = 3464class User26(User):65    weight = 7866class User27(User):67    weight = 7668class User28(User):69    weight = 2870class User29(User):71    weight = 6272class User30(User):73    weight = 6974class User31(User):75    weight = 4176class User32(User):77    weight = 3378class User33(User):79    weight = 1980class User34(User):81    weight = 1982class User35(User):83    weight = 3484class User36(User):85    weight = 7886class User37(User):87    weight = 7688class User38(User):89    weight = 2890class User39(User):91    weight = 6292class User40(User):93    weight = 6994class User41(User):95    weight = 4196class User42(User):97    weight = 3398class User43(User):99    weight = 19100class User44(User):101    weight = 19102class User45(User):103    weight = 34104class User46(User):105    weight = 78106class User47(User):107    weight = 76108class User48(User):109    weight = 28110class User49(User):111    weight = 62112class User50(User):113    weight = 69114class User51(User):115    weight = 5116class User52(User):117    weight = 55118class User53(User):119    weight = 37120class User54(User):121    weight = 2122class User55(User):123    weight = 97124class User56(User):125    weight = 41126class User57(User):127    weight = 33128class User58(User):129    weight = 19130class User59(User):131    weight = 19132class User60(User):133    weight = 34134class User61(User):135    weight = 78136class User62(User):137    weight = 76138class User63(User):139    weight = 28140class User64(User):141    weight = 62142class User65(User):143    weight = 69144class User66(User):145    weight = 5146class User67(User):147    weight = 55148class User68(User):149    weight = 37150class User69(User):151    weight = 2152class User70(User):153    weight = 97154class User71(User):155    weight = 41156class User72(User):157    weight = 33158class User73(User):159    weight = 19160class User74(User):161    weight = 19162class User75(User):163    weight = 34164class User76(User):165    weight = 78166class User77(User):167    weight = 76168class User78(User):169    weight = 28170class User79(User):171    weight = 62172class User80(User):173    weight = 69174class User81(User):175    weight = 41176class User82(User):177    weight = 33178class User83(User):179    weight = 19180class User84(User):181    weight = 19182class User85(User):183    weight = 34184class User86(User):185    weight = 78186class User87(User):187    weight = 76188class User88(User):189    weight = 28190class User89(User):191    weight = 62192class User90(User):193    weight = 69194class User91(User):195    weight = 41196class User92(User):197    weight = 33198class User93(User):199    weight = 19200class User94(User):201    weight = 19202class User95(User):203    weight = 34204class User96(User):205    weight = 78206class User97(User):207    weight = 76208class User98(User):209    weight = 28210class User99(User):211    weight = 62212class User100(User):213    weight = 69214USER_CLASSES = [215    User1,216    User2,217    User3,218    User4,219    User5,220    User6,221    User7,222    User8,223    User9,224    User10,225    User11,226    User12,227    User13,228    User14,229    User15,230    User16,231    User17,232    User18,233    User19,234    User20,235    User21,236    User22,237    User23,238    User24,239    User25,240    User26,241    User27,242    User28,243    User29,244    User30,245    User31,246    User32,247    User33,248    User34,249    User35,250    User36,251    User37,252    User38,253    User39,254    User40,255    User41,256    User42,257    User43,258    User44,259    User45,260    User46,261    User47,262    User48,263    User49,264    User50,265    User51,266    User52,267    User53,268    User54,269    User55,270    User56,271    User57,272    User58,273    User59,274    User60,275    User61,276    User62,277    User63,278    User64,279    User65,280    User66,281    User67,282    User68,283    User69,284    User70,285    User71,286    User72,287    User73,288    User74,289    User75,290    User76,291    User77,292    User78,293    User79,294    User80,295    User81,296    User82,297    User83,298    User84,299    User85,300    User86,301    User87,302    User88,303    User89,304    User90,305    User91,306    User92,307    User93,308    User94,309    User95,310    User96,311    User97,312    User98,313    User99,314    User100,315]316if __name__ == "__main__":317    now = time.time()318    worker_count_cases = [10, 100, 500, 1000, 5000, 10_000, 15_000, 20_000]319    user_count_cases = [10, 100, 1000, 10_000, 50_000, 100_000, 500_000]320    number_of_user_classes_cases = [1, 10, 40, 60, 80, 100]321    spawn_rate_cases = [1, 10, 100, 500, 1000, 2500, 5000, 10_000, 20_000, 25_000]322    case_count = (323        len(worker_count_cases) * len(user_count_cases) * len(number_of_user_classes_cases) * len(spawn_rate_cases)324    )325    results = {}326    try:327        for case_index, (worker_count, user_count, number_of_user_classes, spawn_rate) in enumerate(328            itertools.product(worker_count_cases, user_count_cases, number_of_user_classes_cases, spawn_rate_cases)329        ):330            if user_count / spawn_rate > 1000:331                print(f"Skipping user_count = {user_count:,} - spawn_rate = {spawn_rate:,}")332                continue333            workers = [WorkerNode(str(i + 1)) for i in range(worker_count)]334            ts = time.perf_counter()335            users_dispatcher = UsersDispatcher(worker_nodes=workers, user_classes=USER_CLASSES[:number_of_user_classes])336            instantiate_duration = time.perf_counter() - ts337            # Ramp-up338            ts = time.perf_counter()339            users_dispatcher.new_dispatch(target_user_count=user_count, spawn_rate=spawn_rate)340            new_dispatch_ramp_up_duration = time.perf_counter() - ts341            assert len(users_dispatcher.dispatch_iteration_durations) == 0342            users_dispatcher._wait_between_dispatch = 0343            all_dispatched_users_ramp_up = list(users_dispatcher)344            dispatch_iteration_durations_ramp_up = users_dispatcher.dispatch_iteration_durations[:]345            # Ramp-down346            ts = time.perf_counter()347            users_dispatcher.new_dispatch(target_user_count=0, spawn_rate=spawn_rate)348            new_dispatch_ramp_down_duration = time.perf_counter() - ts349            assert len(users_dispatcher.dispatch_iteration_durations) == 0350            users_dispatcher._wait_between_dispatch = 0351            all_dispatched_users_ramp_down = list(users_dispatcher)352            dispatch_iteration_durations_ramp_down = users_dispatcher.dispatch_iteration_durations[:]353            cpu_ramp_up = "{:3.3f}/{:3.3f}/{:3.3f}".format(354                1000 * statistics.mean(dispatch_iteration_durations_ramp_up),355                1000 * min(dispatch_iteration_durations_ramp_up),356                1000 * max(dispatch_iteration_durations_ramp_up),357            )358            cpu_ramp_down = "{:3.3f}/{:3.3f}/{:3.3f}".format(359                1000 * statistics.mean(dispatch_iteration_durations_ramp_down),360                1000 * min(dispatch_iteration_durations_ramp_down),361                1000 * max(dispatch_iteration_durations_ramp_down),362            )363            print(364                "{:04.0f}/{:04.0f} - {:,} workers - {:,} users - {} user classes - {:,} users/s - instantiate: {:.3f}ms - new_dispatch (ramp-up/ramp-down): {:.3f}ms/{:.3f}ms - cpu_ramp_up: {}ms - cpu_ramp_down: {}ms".format(365                    case_index + 1,366                    case_count,367                    worker_count,368                    user_count,369                    number_of_user_classes,370                    spawn_rate,371                    instantiate_duration * 1000,372                    new_dispatch_ramp_up_duration * 1000,373                    new_dispatch_ramp_down_duration * 1000,374                    cpu_ramp_up,375                    cpu_ramp_down,376                )377            )378            results[(worker_count, user_count, number_of_user_classes, spawn_rate)] = (cpu_ramp_up, cpu_ramp_down)379    finally:380        table = PrettyTable()381        table.field_names = [382            "Workers",383            "Users",384            "User Classes",385            "Spawn Rate",386            "Ramp-Up (avg/min/max) (ms)",387            "Ramp-Down (avg/min/max) (ms)",388        ]389        table.align["Workers"] = "l"390        table.align["Users"] = "l"391        table.align["User Classes"] = "l"392        table.align["Spawn Rate"] = "l"393        table.align["Ramp-Up (avg/min/max) (ms)"] = "c"394        table.align["Ramp-Down (avg/min/max) (ms)"] = "c"395        table.add_rows(396            [397                [398                    f"{worker_count:,}",399                    f"{user_count:,}",400                    number_of_user_classes,401                    f"{spawn_rate:,}",402                    cpu_ramp_up,403                    cpu_ramp_down,404                ]405                for (worker_count, user_count, number_of_user_classes, spawn_rate), (406                    cpu_ramp_up,407                    cpu_ramp_down,408                ) in results.items()409            ]410        )411        print()412        print()413        print()414        print(table)415        with open(f"results-dispatch-benchmarks-{int(now)}.txt", "wt") as file:416            file.write(table.get_string())417        with open(f"results-dispatch-benchmarks-{int(now)}.json", "wt") as file:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
