1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
//! Helpers and objects for building Goose load tests. //! //! Goose manages load tests with a series of objects: //! //! - [`GooseTaskSet`](./struct.GooseTaskSet.html) each client is assigned a task set, which is a collection of tasks. //! - [`GooseTask`](./struct.GooseTask.html) tasks define one or more web requests and are assigned to task sets. //! - [`GooseClient`](./struct.GooseClient.html) a client state responsible for repeatedly running all tasks in the assigned task set. //! - [`GooseRequest`](./struct.GooseRequest.html) optional statistics collected for each URL/method pair. //! //! ## Creating Task Sets //! //! A [`GooseTaskSet`](./struct.GooseTaskSet.html) is created by passing in a `&str` name to the `new` function, for example: //! //! ```rust //! use goose::goose::GooseTaskSet; //! use goose::taskset; //! //! let mut loadtest_tasks = taskset!("LoadtestTasks"); //! ``` //! //! ### Task Set Weight //! //! A weight can be assigned to a task set, controlling how often it is assigned to client //! threads. The larger the value of weight, the more it will be assigned to clients. In the //! following example, `FooTasks` will be assigned to clients twice as often as `Bar` tasks. //! We could have just added a weight of `2` to `FooTasks` and left the default weight of `1` //! assigned to `BarTasks` for the same weighting: //! //! ```rust //! use goose::goose::GooseTaskSet; //! use goose::taskset; //! //! let mut foo_tasks = taskset!("FooTasks").set_weight(10); //! let mut bar_tasks = taskset!("BarTasks").set_weight(5); //! ``` //! //! ### Task Set Host //! //! A default host can be assigned to a task set, which will be used only if the `--host` //! CLI option is not set at run-time. For example, this can configure your load test to //! run against your local development environment by default, allowing the `--host` option //! to override host when you want to load test production. You can also assign different //! hosts to different task sets if this is desirable: //! //! ```rust //! use goose::goose::GooseTaskSet; //! use goose::taskset; //! //! let mut foo_tasks = taskset!("FooTasks").set_host("http://www.local"); //! let mut bar_tasks = taskset!("BarTasks").set_host("http://www2.local"); //! ``` //! //! ### Task Set Wait Time //! //! Wait time is specified as a low-high integer range. Each time a task completes in //! the task set, the client will pause for a random number of seconds inclusively between //! the low and high wait times. In the following example, Clients loading `foo` tasks will //! sleep 0 to 3 seconds after each task completes, and Clients loading `bar` tasks will //! sleep 5 to 10 seconds after each task completes. //! //! ```rust //! use goose::goose::GooseTaskSet; //! use goose::taskset; //! //! let mut foo_tasks = taskset!("FooTasks").set_wait_time(0, 3); //! let mut bar_tasks = taskset!("BarTasks").set_wait_time(5, 10); //! ``` //! ## Creating Tasks //! //! A [`GooseTask`](./struct.GooseTask.html) must include a pointer to a function which //! will be executed each time the task is run. //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut a_task = task!(task_function); //! //! /// A very simple task that simply loads the front page. //! async fn task_function(client: &mut GooseClient) { //! let _response = client.get("/"); //! } //! ``` //! //! ### Task Name //! //! A name can be assigned to a task, and will be displayed in statistics about all requests //! made by the task. //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut a_task = task!(task_function).set_name("a"); //! //! /// A very simple task that simply loads the front page. //! async fn task_function(client: &mut GooseClient) { //! let _response = client.get("/"); //! } //! ``` //! //! ### Task Weight //! //! Individual tasks can be assigned a weight, controlling how often the task runs. The //! larger the value of weight, the more it will run. In the following example, `a_task` //! runs 3 times as often as `b_task`: //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut a_task = task!(a_task_function).set_weight(9); //! let mut b_task = task!(b_task_function).set_weight(3); //! //! /// A very simple task that simply loads the "a" page. //! async fn a_task_function(client: &mut GooseClient) { //! let _response = client.get("/a/"); //! } //! //! /// Another very simple task that simply loads the "b" page. //! async fn b_task_function(client: &mut GooseClient) { //! let _response = client.get("/b/"); //! } //! ``` //! //! ### Task Sequence //! //! Tasks can also be configured to run in a sequence. For example, a task with a sequence //! value of `1` will always run before a task with a sequence value of `2`. Weight can //! be applied to sequenced tasks, so for example a task with a weight of `2` and a sequence //! of `1` will run two times before a task with a sequence of `2`. Task sets can contain //! tasks with sequence values and without sequence values, and in this case all tasks with //! a sequence value will run before tasks without a sequence value. In the folllowing example, //! `a_task` runs before `b_task`, which runs before `c_task`: //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut a_task = task!(a_task_function).set_sequence(1); //! let mut b_task = task!(b_task_function).set_sequence(2); //! let mut c_task = task!(c_task_function); //! //! /// A very simple task that simply loads the "a" page. //! async fn a_task_function(client: &mut GooseClient) { //! let _response = client.get("/a/"); //! } //! //! /// Another very simple task that simply loads the "b" page. //! async fn b_task_function(client: &mut GooseClient) { //! let _response = client.get("/b/"); //! } //! //! /// Another very simple task that simply loads the "c" page. //! async fn c_task_function(client: &mut GooseClient) { //! let _response = client.get("/c/"); //! } //! ``` //! //! ### Task On Start //! //! Tasks can be flagged to only run when a client first starts. This can be useful if you'd //! like your load test to use a logged-in user. It is possible to assign sequences and weights //! to `on_start` functions if you want to have multiple tasks run in a specific order at start //! time, and/or the tasks to run multiple times. A task can be flagged to run both on start //! and on stop. //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut a_task = task!(a_task_function).set_sequence(1).set_on_start(); //! //! /// A very simple task that simply loads the "a" page. //! async fn a_task_function(client: &mut GooseClient) { //! let _response = client.get("/a/"); //! } //! ``` //! //! ### Task On Stop //! //! Tasks can be flagged to only run when a client stops. This can be useful if you'd like your //! load test to simluate a user logging out when it finishes. It is possible to assign sequences //! and weights to `on_stop` functions if you want to have multiple tasks run in a specific order //! at stop time, and/or the tasks to run multiple times. A task can be flagged to run both on //! start and on stop. //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut b_task = task!(b_task_function).set_sequence(2).set_on_stop(); //! //! /// Another very simple task that simply loads the "b" page. //! async fn b_task_function(client: &mut GooseClient) { //! let _response = client.get("/b/"); //! } //! ``` //! //! ## Controlling Clients //! //! When Goose starts, it creates one or more [`GooseClient`](./struct.GooseClient.html)s, //! assigning a single [`GooseTaskSet`](./struct.GooseTaskSet.html) to each. This client is //! then used to generate load. Behind the scenes, Goose is leveraging the //! [`reqwest::blocking::client`](https://docs.rs/reqwest/*/reqwest/blocking/struct.Client.html) //! to load web pages, and Goose can therefor do anything Reqwest can do. //! //! The most common request types are [`GET`](./struct.GooseClient.html#method.get) and //! [`POST`](./struct.GooseClient.html#method.post), but [`HEAD`](./struct.GooseClient.html#method.head), //! PUT, PATCH and [`DELETE`](./struct.GooseClient.html#method.delete) are also supported. //! //! ### GET //! //! A helper to make a `GET` request of a path and collect relevant statistics. //! Automatically prepends the correct host. //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut task = task!(get_function); //! //! /// A very simple task that makes a GET request. //! async fn get_function(client: &mut GooseClient) { //! let _response = client.get("/path/to/foo/"); //! } //! ``` //! //! The returned response is a [`reqwest::blocking::Response`](https://docs.rs/reqwest/*/reqwest/blocking/struct.Response.html) //! struct. You can use it as you would any Reqwest Response. //! //! //! ### POST //! //! A helper to make a `POST` request of a string value to the path and collect relevant //! statistics. Automatically prepends the correct host. The returned response is a //! [`reqwest::blocking::Response`](https://docs.rs/reqwest/*/reqwest/blocking/struct.Response.html) //! //! ```rust //! use goose::goose::{GooseTask, GooseClient}; //! use goose::task; //! //! // Needed to wrap and store async functions. //! use std::boxed::Box; //! //! let mut task = task!(post_function); //! //! /// A very simple task that makes a POST request. //! async fn post_function(client: &mut GooseClient) { //! let _response = client.post("/path/to/foo/", "string value to post".to_string()); //! } //! ``` //! //! ## License //! //! Copyright 2020 Jeremy Andrews //! //! Licensed under the Apache License, Version 2.0 (the "License"); //! you may not use this file except in compliance with the License. //! You may obtain a copy of the License at //! //! http://www.apache.org/licenses/LICENSE-2.0 //! //! Unless required by applicable law or agreed to in writing, software //! distributed under the License is distributed on an "AS IS" BASIS, //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //! See the License for the specific language governing permissions and //! limitations under the License. use http::method::Method; use http::StatusCode; use reqwest::Error; use reqwest::{Client, RequestBuilder, Response}; use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, HashMap}; use std::hash::{Hash, Hasher}; use std::{future::Future, pin::Pin, time::Instant}; use url::Url; use crate::GooseConfiguration; static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); /// Async tasks must be boxed and pinned for Goose to store and invoke them. #[macro_export] macro_rules! task { ($task_func:ident) => { GooseTask::new(move |s| Box::pin($task_func(s))) }; } /// For consistency, taskset!("foo") expands to GooseTaskSet::new("foo"). #[macro_export] macro_rules! taskset { ($taskset_func:tt) => { GooseTaskSet::new($taskset_func) }; } /// An individual task set. #[derive(Clone, Hash)] pub struct GooseTaskSet { /// The name of the task set. pub name: String, /// An integer reflecting where this task set lives in the internal `GooseTest.task_sets` vector. pub task_sets_index: usize, /// An integer value that controls the frequency that this task set will be assigned to a client. pub weight: usize, /// An integer value indicating the minimum number of seconds a client will sleep after running a task. pub min_wait: usize, /// An integer value indicating the maximum number of seconds a client will sleep after running a task. pub max_wait: usize, /// A vector containing one copy of each GooseTask that will run by clients running this task set. pub tasks: Vec<GooseTask>, /// A vector of vectors of integers, controlling the sequence and order GooseTasks are run. pub weighted_tasks: Vec<Vec<usize>>, /// A vector of vectors of integers, controlling the sequence and order on_start GooseTasks are run when the client first starts. pub weighted_on_start_tasks: Vec<Vec<usize>>, /// A vector of vectors of integers, controlling the sequence and order on_stop GooseTasks are run when a client stops. pub weighted_on_stop_tasks: Vec<Vec<usize>>, /// An optional default host to run this TaskSet against. pub host: Option<String>, } impl GooseTaskSet { /// Creates a new GooseTaskSet. Once created, GooseTasks must be assigned to it, and finally it must be /// registered with the GooseAttack object. The returned object must be stored in a mutable value. /// /// # Example /// ```rust /// use goose::goose::GooseTaskSet; /// use goose::taskset; /// /// let mut example_tasks = taskset!("ExampleTasks"); /// ``` pub fn new(name: &str) -> Self { trace!("new taskset: name: {}", &name); GooseTaskSet { name: name.to_string(), task_sets_index: usize::max_value(), weight: 1, min_wait: 0, max_wait: 0, tasks: Vec::new(), weighted_tasks: Vec::new(), weighted_on_start_tasks: Vec::new(), weighted_on_stop_tasks: Vec::new(), host: None, } } /// Registers a GooseTask with a GooseTaskSet, where it is stored in the GooseTaskSet.tasks vector. The /// function associated with the task will be run during the load test. /// /// # Example /// ```rust /// use goose::goose::{GooseTaskSet, GooseTask, GooseClient}; /// use goose::{task, taskset}; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut example_tasks = taskset!("ExampleTasks"); /// example_tasks.register_task(task!(a_task_function)); /// /// /// A very simple task that simply loads the "a" page. /// async fn a_task_function(client: &mut GooseClient) { /// let _response = client.get("/a/"); /// } /// ``` pub fn register_task(mut self, mut task: GooseTask) -> Self { trace!("{} register_task: {}", self.name, task.name); task.tasks_index = self.tasks.len(); self.tasks.push(task); self } /// Sets a weight on a task set. The larger the value of weight, the more often the task set will /// be assigned to clients. For example, if you have task set foo with a weight of 3, and task set /// bar with a weight of 1, and you spin up a load test with 8 clients, 6 of them will be running /// the foo task set, and 2 will be running the bar task set. /// /// # Example /// ```rust /// use goose::goose::GooseTaskSet; /// use goose::taskset; /// /// let mut example_tasks = taskset!("ExampleTasks").set_weight(3); /// ``` pub fn set_weight(mut self, weight: usize) -> Self { trace!("{} set_weight: {}", self.name, weight); if weight < 1 { error!("{} weight of {} not allowed", self.name, weight); std::process::exit(1); } else { self.weight = weight; } self } /// Set a default host for the task set. If no `--host` flag is set when running the load test, this /// host will be pre-pended on all requests. For example, this can configure your load test to run /// against your local development environment by default, and the `--host` option could be used to /// override host when running the load test against production. /// /// # Example /// ```rust /// use goose::goose::GooseTaskSet; /// use goose::taskset; /// /// let mut example_tasks = taskset!("ExampleTasks").set_host("http://10.1.1.42"); /// ``` pub fn set_host(mut self, host: &str) -> Self { trace!("{} set_host: {}", self.name, host); // Host validation happens in main() at startup. self.host = Some(host.to_string()); self } /// Configure a task_set to to pause after running each task. The length of the pause will be randomly /// selected from `min_weight` to `max_wait` inclusively. For example, if `min_wait` is `0` and /// `max_weight` is `2`, the client will randomly sleep for 0, 1 or 2 seconds after each task completes. /// /// # Example /// ```rust /// use goose::goose::GooseTaskSet; /// use goose::taskset; /// /// let mut example_tasks = taskset!("ExampleTasks").set_wait_time(0, 1); /// ``` pub fn set_wait_time(mut self, min_wait: usize, max_wait: usize) -> Self { trace!( "{} set_wait time: min: {} max: {}", self.name, min_wait, max_wait ); if min_wait > max_wait { error!( "min_wait({}) can't be larger than max_weight({})", min_wait, max_wait ); std::process::exit(1); } self.min_wait = min_wait; self.max_wait = max_wait; self } } /// Tracks the current run-mode of a client. #[derive(Debug, Clone, PartialEq)] pub enum GooseClientMode { /// Clients are briefly in the INIT mode when first allocated. INIT, /// Clients are briefly in the HATCHING mode when setting things up. HATCHING, /// Clients spend most of their time in the RUNNING mode, executing tasks. RUNNING, /// Clients are briefly in the EXITING mode when stopping. EXITING, } /// Commands sent between the parent and client threads, and between manager and /// worker processes. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum GooseClientCommand { /// Tell worker process to pause load test. WAIT, /// Tell worker process to start load test. RUN, /// Tell client thread to push statistics to parent. SYNC, /// Tell client thread to exit. EXIT, } /// Supported HTTP methods. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum GooseMethod { DELETE, GET, HEAD, PATCH, POST, PUT, } fn goose_method_from_method(method: Method) -> Option<GooseMethod> { match method { Method::DELETE => Some(GooseMethod::DELETE), Method::GET => Some(GooseMethod::GET), Method::HEAD => Some(GooseMethod::HEAD), Method::PATCH => Some(GooseMethod::PATCH), Method::POST => Some(GooseMethod::POST), Method::PUT => Some(GooseMethod::PUT), _ => None, } } /// Statistics collected about a path-method pair, (for example `/index`-`GET`). #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct GooseRequest { /// The path for which statistics are being collected. pub path: String, /// The method for which statistics are being collected. pub method: GooseMethod, /// Per-response-time counters, tracking how often pages are returned with this response time. pub response_times: BTreeMap<usize, usize>, /// The shortest response time seen so far. pub min_response_time: usize, /// The longest response time seen so far. pub max_response_time: usize, /// Total combined response times seen so far. pub total_response_time: usize, /// Total number of response times seen so far. pub response_time_counter: usize, /// Per-status-code counters, tracking how often each response code was returned for this request. pub status_code_counts: HashMap<u16, usize>, /// Total number of times this path-method request resulted in a successful (2xx) status code. pub success_count: usize, /// Total number of times this path-method request resulted in a non-successful (non-2xx) status code. pub fail_count: usize, /// Load test hash. pub load_test_hash: u64, } impl GooseRequest { /// Create a new GooseRequest object. pub fn new(path: &str, method: GooseMethod, load_test_hash: u64) -> Self { trace!("new request"); GooseRequest { path: path.to_string(), method, response_times: BTreeMap::new(), min_response_time: 0, max_response_time: 0, total_response_time: 0, response_time_counter: 0, status_code_counts: HashMap::new(), success_count: 0, fail_count: 0, load_test_hash, } } /// Track response time. fn set_response_time(&mut self, response_time: u128) { // Perform this conversin only once, then re-use throughout this funciton. let response_time_usize = response_time as usize; // Update minimum if this one is fastest yet. if self.min_response_time == 0 || response_time_usize < self.min_response_time { self.min_response_time = response_time_usize; } // Update maximum if this one is slowest yet. if response_time_usize > self.max_response_time { self.max_response_time = response_time_usize; } // Update total_response time, adding in this one. self.total_response_time += response_time_usize; // Each time we store a new response time, increment counter by one. self.response_time_counter += 1; // Round the response time so we can combine similar times together and // minimize required memory to store and push upstream to the parent. let rounded_response_time: usize; // No rounding for 1-100ms response times. if response_time < 100 { rounded_response_time = response_time_usize; } // Round to nearest 10 for 100-500ms response times. else if response_time < 500 { rounded_response_time = ((response_time as f64 / 10.0).round() * 10.0) as usize; } // Round to nearest 100 for 500-1000ms response times. else if response_time < 1000 { rounded_response_time = ((response_time as f64 / 100.0).round() * 100.0) as usize; } // Round to nearest 1000 for all larger response times. else { rounded_response_time = ((response_time as f64 / 1000.0).round() * 1000.0) as usize; } let counter = match self.response_times.get(&rounded_response_time) { // We've seen this response_time before, increment counter. Some(c) => { debug!("got {:?} counter: {}", rounded_response_time, c); *c + 1 } // First time we've seen this response time, initialize counter. None => { debug!("no match for counter: {}", rounded_response_time); 1 } }; self.response_times.insert(rounded_response_time, counter); debug!("incremented {} counter: {}", rounded_response_time, counter); } /// Increment counter for status code, creating new counter if first time seeing status code. fn set_status_code(&mut self, status_code: Option<StatusCode>) { let status_code_u16 = match status_code { Some(s) => s.as_u16(), _ => 0, }; let counter = match self.status_code_counts.get(&status_code_u16) { // We've seen this status code before, increment counter. Some(c) => { debug!("got {:?} counter: {}", status_code, c); *c + 1 } // First time we've seen this status code, initialize counter. None => { debug!("no match for counter: {}", status_code_u16); 1 } }; self.status_code_counts.insert(status_code_u16, counter); debug!("incremented {} counter: {}", status_code_u16, counter); } } /// An individual client state, repeatedly running all GooseTasks in a specific GooseTaskSet. #[derive(Debug, Clone)] pub struct GooseClient { /// An index into the internal `GooseTest.task_sets` vector, indicating which GooseTaskSet is running. pub task_sets_index: usize, /// A [`reqwest.blocking.client`](https://docs.rs/reqwest/*/reqwest/blocking/struct.Client.html) instance (@TODO: async). pub client: Client, /// Optional global host, can be overridden per-task-set or via the cli. pub default_host: Option<String>, /// Optional per-task-set .host. pub task_set_host: Option<String>, /// Minimum amount of time to sleep after running a task. pub min_wait: usize, /// Maximum amount of time to sleep after running a task. pub max_wait: usize, /// A local copy of the global GooseConfiguration. pub config: GooseConfiguration, /// An index into the internal `GooseTest.weighted_clients, indicating which weighted GooseTaskSet is running. pub weighted_clients_index: usize, /// The current run mode of this client, see `enum GooseClientMode`. pub mode: GooseClientMode, /// A weighted list of all tasks that run when the client first starts. pub weighted_on_start_tasks: Vec<Vec<usize>>, /// A weighted list of all tasks that this client runs once started. pub weighted_tasks: Vec<Vec<usize>>, /// A pointer into which sequenced bucket the client is currently running tasks from. pub weighted_bucket: usize, /// A pointer of which task within the current sequenced bucket is currently running. pub weighted_bucket_position: usize, /// A weighted list of all tasks that run when the client stops. pub weighted_on_stop_tasks: Vec<Vec<usize>>, /// Optional name of all requests made within the current task. pub task_request_name: Option<String>, /// Optional name of all requests made within the current task. pub request_name: Option<String>, /// Store the previous url. pub previous_path: Option<String>, /// Store the previous url. pub previous_method: Option<GooseMethod>, /// Store the optional request_name allowing tasks to toggle success/failure. pub previous_request_name: Option<String>, /// Store if the previous request was a success (false for failure). pub was_success: bool, /// Optional statistics collected about all requests made by this client. pub requests: HashMap<String, GooseRequest>, /// Load test hash. pub load_test_hash: u64, } impl GooseClient { /// Create a new client state. pub fn new( counter: usize, task_sets_index: usize, default_host: Option<String>, task_set_host: Option<String>, min_wait: usize, max_wait: usize, configuration: &GooseConfiguration, load_test_hash: u64, ) -> Self { trace!("new client"); let builder = Client::builder() .user_agent(APP_USER_AGENT) .cookie_store(true); let client = match builder.build() { Ok(c) => c, Err(e) => { error!( "failed to build client {} for task {}: {}", counter, task_sets_index, e ); std::process::exit(1); } }; GooseClient { task_sets_index, default_host, task_set_host, client, config: configuration.clone(), min_wait, max_wait, // A value of max_value() indicates this client isn't fully initialized yet. weighted_clients_index: usize::max_value(), mode: GooseClientMode::INIT, weighted_on_start_tasks: Vec::new(), weighted_tasks: Vec::new(), weighted_bucket: 0, weighted_bucket_position: 0, weighted_on_stop_tasks: Vec::new(), task_request_name: None, request_name: None, previous_path: None, previous_method: None, previous_request_name: None, was_success: false, requests: HashMap::new(), load_test_hash, } } /// Sets a name for the next request made. /// /// One example use case of this is to group together requests to different URLs in the /// statistics that don't need to be split out, perhaps because they're all the same type /// of page. /// /// # Examples /// /// In this example, the request will show up as "GET foo": /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(get_function); /// /// /// A very simple task that makes a GET request. /// async fn get_function(client: &mut GooseClient) { /// let _response = client.set_request_name("foo").get("/path/to/foo"); /// } /// ``` /// /// In this example, the first request will show up in the statistics as "GET foo", and the /// second request will show up as "GET /path/to/foo". /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(get_function); /// /// /// A very simple task that makes a GET request. /// async fn get_function(client: &mut GooseClient) { /// let _response = client.set_request_name("foo").get("/path/to/foo").await; /// let _response = client.get("/path/to/foo").await; /// } /// ``` pub fn set_request_name(&mut self, name: &str) -> &mut Self { if name != "" { self.request_name = Some(name.to_string()); } else { self.request_name = None; } self } /// Sets the current run mode of this client. pub fn set_mode(&mut self, mode: GooseClientMode) { self.mode = mode; } /// Checks if the current path-method pair has been requested before. fn get_request(&mut self, path: &str, method: &GooseMethod) -> GooseRequest { let key = format!("{:?} {}", method, path); trace!("get key: {}", &key); match self.requests.get(&key) { Some(r) => r.clone(), None => GooseRequest::new(path, method.clone(), self.load_test_hash), } } /// Stores request statistics about the current path-method pair. fn set_request(&mut self, path: &str, method: &GooseMethod, request: GooseRequest) { let key = format!("{:?} {}", method, path); trace!("set key: {}", &key); self.requests.insert(key, request); } /// A helper that pre-pends a hostname to the path. For example, if you pass in `/foo` /// and `--host` is set to `http://127.0.0.1` it will return `http://127.0.0.1/foo`. /// Respects per-`GooseTaskSet` `host` configuration, global `GooseAttack` `host` /// configuration, and `--host` CLI configuration option. /// /// If `path` is passed in with a hard-coded host, this will be used instead. /// /// Host is defined in the following order: /// - If `path` includes the host, use this /// - Otherwise, if `--host` is defined, use this /// - Otherwise, if `GooseTaskSet.host` is defined, use this /// - Otherwise, use global `GooseAttack.host`. pub fn build_url(&mut self, path: &str) -> String { // If URL includes a host, use it. if let Ok(parsed_path) = Url::parse(path) { if let Some(_uri) = parsed_path.host() { return path.to_string(); } } let base_url = if !self.config.host.is_empty() { // If the `--host` CLI option is set, use it to build the URL Url::parse(&self.config.host).unwrap() } else { match &self.task_set_host { // Otherwise, if `GooseTaskSet.host` is defined, usee this Some(host) => Url::parse(host).unwrap(), // Otherwise, use global `GooseAttack.host`. `unwrap` okay as host validation was done at startup. None => Url::parse(&self.default_host.clone().unwrap()).unwrap(), } }; match base_url.join(path) { Ok(url) => url.to_string(), Err(e) => { error!( "failed to build url from base {} and path {} for task {}: {}", &base_url, &path, self.task_sets_index, e ); std::process::exit(1); } } } /// A helper to make a `GET` request of a path and collect relevant statistics. /// Automatically prepends the correct host. /// /// (If you need to set headers, change timeouts, or otherwise make use of the /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object, you can instead call `goose_get` which returns a RequestBuilder, then /// call `goose_send` to invoke the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(get_function); /// /// /// A very simple task that makes a GET request. /// async fn get_function(client: &mut GooseClient) { /// let _response = client.get("/path/to/foo/"); /// } /// ``` pub async fn get(&mut self, path: &str) -> Result<Response, Error> { let request_builder = self.goose_get(path); self.goose_send(request_builder).await } /// A helper to make a `POST` request of a path and collect relevant statistics. /// Automatically prepends the correct host. /// /// (If you need to set headers, change timeouts, or otherwise make use of the /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object, you can instead call `goose_post` which returns a RequestBuilder, then /// call `goose_send` to invoke the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(post_function); /// /// /// A very simple task that makes a POST request. /// async fn post_function(client: &mut GooseClient) { /// let _response = client.post("/path/to/foo/", "BODY BEING POSTED".to_string()); /// } /// ``` pub async fn post(&mut self, path: &str, body: String) -> Result<Response, Error> { let request_builder = self.goose_post(path).body(body); self.goose_send(request_builder).await } /// A helper to make a `HEAD` request of a path and collect relevant statistics. /// Automatically prepends the correct host. /// /// (If you need to set headers, change timeouts, or otherwise make use of the /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object, you can instead call `goose_head` which returns a RequestBuilder, then /// call `goose_send` to invoke the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(head_function); /// /// /// A very simple task that makes a HEAD request. /// async fn head_function(client: &mut GooseClient) { /// let _response = client.head("/path/to/foo/"); /// } /// ``` pub async fn head(&mut self, path: &str) -> Result<Response, Error> { let request_builder = self.goose_head(path); self.goose_send(request_builder).await } /// A helper to make a `DELETE` request of a path and collect relevant statistics. /// Automatically prepends the correct host. /// /// (If you need to set headers, change timeouts, or otherwise make use of the /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object, you can instead call `goose_delete` which returns a RequestBuilder, /// then call `goose_send` to invoke the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(delete_function); /// /// /// A very simple task that makes a DELETE request. /// async fn delete_function(client: &mut GooseClient) { /// let _response = client.delete("/path/to/foo/"); /// } /// ``` pub async fn delete(&mut self, path: &str) -> Result<Response, Error> { let request_builder = self.goose_delete(path); self.goose_send(request_builder).await } /// Prepends the correct host on the path, then prepares a /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object for making a `GET` request. /// /// (You must then call `goose_send` on this object to actually execute the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(get_function); /// /// /// A simple task that makes a GET request, exposing the Reqwest /// /// request builder. /// async fn get_function(client: &mut GooseClient) { /// let request_builder = client.goose_get("/path/to/foo"); /// let response = client.goose_send(request_builder); /// } /// ``` pub fn goose_get(&mut self, path: &str) -> RequestBuilder { let url = self.build_url(path); self.client.get(&url) } /// Prepends the correct host on the path, then prepares a /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object for making a `POST` request. /// /// (You must then call `goose_send` on this object to actually execute the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(post_function); /// /// /// A simple task that makes a POST request, exposing the Reqwest /// /// request builder. /// async fn post_function(client: &mut GooseClient) { /// let request_builder = client.goose_post("/path/to/foo"); /// let response = client.goose_send(request_builder); /// } /// ``` pub fn goose_post(&mut self, path: &str) -> RequestBuilder { let url = self.build_url(path); self.client.post(&url) } /// Prepends the correct host on the path, then prepares a /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object for making a `HEAD` request. /// /// (You must then call `goose_send` on this object to actually execute the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(head_function); /// /// /// A simple task that makes a HEAD request, exposing the Reqwest /// /// request builder. /// async fn head_function(client: &mut GooseClient) { /// let request_builder = client.goose_head("/path/to/foo"); /// let response = client.goose_send(request_builder); /// } /// ``` pub fn goose_head(&mut self, path: &str) -> RequestBuilder { let url = self.build_url(path); self.client.head(&url) } /// Prepends the correct host on the path, then prepares a /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object for making a `PUT` request. /// /// (You must then call `goose_send` on this object to actually execute the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(put_function); /// /// /// A simple task that makes a PUT request, exposing the Reqwest /// /// request builder. /// async fn put_function(client: &mut GooseClient) { /// let request_builder = client.goose_put("/path/to/foo"); /// let response = client.goose_send(request_builder); /// } /// ``` pub fn goose_put(&mut self, path: &str) -> RequestBuilder { let url = self.build_url(path); self.client.put(&url) } /// Prepends the correct host on the path, then prepares a /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object for making a `PATCH` request. /// /// (You must then call `goose_send` on this object to actually execute the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(patch_function); /// /// /// A simple task that makes a PUT request, exposing the Reqwest /// /// request builder. /// async fn patch_function(client: &mut GooseClient) { /// let request_builder = client.goose_patch("/path/to/foo"); /// let response = client.goose_send(request_builder); /// } /// ``` pub fn goose_patch(&mut self, path: &str) -> RequestBuilder { let url = self.build_url(path); self.client.patch(&url) } /// Prepends the correct host on the path, then prepares a /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object for making a `DELETE` request. /// /// (You must then call `goose_send` on this object to actually execute the request.) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(delete_function); /// /// /// A simple task that makes a DELETE request, exposing the Reqwest /// /// request builder. /// async fn delete_function(client: &mut GooseClient) { /// let request_builder = client.goose_delete("/path/to/foo"); /// let response = client.goose_send(request_builder); /// } /// ``` pub fn goose_delete(&mut self, path: &str) -> RequestBuilder { let url = self.build_url(path); self.client.delete(&url) } /// Builds the provided /// [`reqwest::RequestBuilder`](reqwest/*/reqwest/struct.RequestBuilder.html) /// object and then executes the response. If statistics are being displayed, it /// also captures request statistics. /// /// It is possible to build and execute a `RequestBuilder` object directly with /// Reqwest without using this helper function, but then Goose is unable to capture /// statistics. /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(get_function); /// /// /// A simple task that makes a GET request, exposing the Reqwest /// /// request builder. /// async fn get_function(client: &mut GooseClient) { /// let request_builder = client.goose_get("/path/to/foo"); /// let response = client.goose_send(request_builder); /// } /// ``` pub async fn goose_send(&mut self, request_builder: RequestBuilder) -> Result<Response, Error> { let started = Instant::now(); let request = match request_builder.build() { Ok(r) => r, Err(e) => { error!("goose_send failed to build request: {}", e); std::process::exit(1); } }; // Allows introspection, and toggling success/failure. self.previous_method = goose_method_from_method(request.method().clone()); self.previous_path = match Url::parse(&request.url().to_string()) { Ok(u) => Some(u.path().to_string()), Err(e) => { error!("failed to parse url: {}", e); None } }; self.previous_request_name = self.request_name.clone(); // Make the actual request. let response = self.client.execute(request).await; let elapsed = started.elapsed(); if !self.config.no_stats { let path = self .previous_path .clone() .expect("failed to unwrap previous_path"); let method = self .previous_method .clone() .expect("failed to unwrap previous_method"); // Requests are named per-request, per-task, or based on the path loaded. let request_name = match &self.request_name { Some(rn) => rn.to_string(), None => match &self.task_request_name { Some(trn) => trn.to_string(), None => path.to_string(), }, }; let mut goose_request = self.get_request(&request_name, &method); goose_request.set_response_time(elapsed.as_millis()); match &response { Ok(r) => { let status_code = r.status(); // Only increment status_code_counts if we're displaying the results if self.config.status_codes { goose_request.set_status_code(Some(status_code)); } debug!("{:?}: status_code {}", &path, status_code); // @TODO: match/handle all is_foo() https://docs.rs/http/0.2.1/http/status/struct.StatusCode.html if status_code.is_success() { goose_request.success_count += 1; self.was_success = true; } // @TODO: properly track redirects and other code ranges else { // @TODO: handle this correctly debug!("{:?}: non-success status_code: {:?}", &path, status_code); goose_request.fail_count += 1; self.was_success = false; } } Err(e) => { // @TODO: what can we learn from a reqwest error? warn!("{:?}: {}", &path, e); goose_request.fail_count += 1; self.was_success = false; if self.config.status_codes { goose_request.set_status_code(None); } } }; self.set_request(&request_name, &method, goose_request); } // Consume self.request_name if it was set. if self.request_name.is_some() { self.request_name = None } response } /// Helper to determine which request_name was used in the previous request. fn get_previous_request_name(&mut self) -> String { match &self.previous_request_name { Some(prn) => prn.to_string(), None => match &self.task_request_name { Some(trn) => trn.to_string(), None => self .previous_path .clone() .expect("failed to unwrap previous_path"), }, } } /// Manually mark a request as a success. /// /// By default, Goose will consider any response with a 2xx status code as a success. It may be /// valid in your test for a non-2xx HTTP status code to be returned. /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(get_function); /// /// /// A simple task that makes a GET request. /// async fn get_function(client: &mut GooseClient) { /// let response = client.get("/404").await; /// match &response { /// Ok(r) => { /// // We expect a 404 here. /// if r.status() == 404 { /// client.set_success(); /// } /// }, /// Err(_) => (), /// } /// } /// ```` pub fn set_success(&mut self) { // If the last request was a success, we don't need to change anything. if !self.was_success { let request_name = self.get_previous_request_name(); let previous_method = self .previous_method .clone() .expect("failed to unwrap previous_method"); let mut goose_request = self.get_request(&request_name.to_string(), &previous_method.clone()); goose_request.success_count += 1; goose_request.fail_count -= 1; self.set_request(&request_name, &previous_method, goose_request); } } /// Manually mark a request as a failure. /// /// By default, Goose will consider any response with a 2xx status code as a success. You may require /// more advanced logic, in which a 2xx status code is actually a failure. /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let mut task = task!(loadtest_index_page); /// /// async fn loadtest_index_page(client: &mut GooseClient) { /// let response = client.set_request_name("index").get("/").await; /// // Extract the response Result. /// match response { /// Ok(r) => { /// // We only need to check pages that returned a success status code. /// if r.status().is_success() { /// match r.text().await { /// Ok(text) => { /// // If the expected string doesn't exist, this page load /// // was a failure. /// if !text.contains("this string must exist") { /// client.set_failure(); /// } /// } /// // Empty page, this is a failure. /// Err(_) => client.set_failure(), /// } /// } /// }, /// // Invalid response, this is already a failure. /// Err(_) => (), /// } /// } /// ```` pub fn set_failure(&mut self) { // If the last request was a failure, we don't need to change anything. if self.was_success { let request_name = self.get_previous_request_name(); let previous_method = self .previous_method .clone() .expect("failed to unwrap previous_method"); let mut goose_request = self.get_request(&request_name.to_string(), &previous_method.clone()); goose_request.success_count -= 1; goose_request.fail_count += 1; self.set_request(&request_name, &previous_method, goose_request); } } } /// An individual task within a `GooseTaskSet`. #[derive(Clone)] pub struct GooseTask { /// An index into GooseTaskSet.task, indicating which task this is. pub tasks_index: usize, /// An optional name for the task, used when displaying statistics about requests made. pub name: String, /// An integer value that controls the frequency that this task will be run. pub weight: usize, /// An integer value that controls when this task runs compared to other tasks in the same GooseTaskSet. pub sequence: usize, /// A flag indicating that this task runs when the client starts. pub on_start: bool, /// A flag indicating that this task runs when the client stops. pub on_stop: bool, /// A required function that is executed each time this task runs. pub function: for<'r> fn(&'r mut GooseClient) -> Pin<Box<dyn Future<Output = ()> + Send + 'r>>, } impl GooseTask { pub fn new( function: for<'r> fn(&'r mut GooseClient) -> Pin<Box<dyn Future<Output = ()> + Send + 'r>>, ) -> Self { trace!("new task"); GooseTask { tasks_index: usize::max_value(), name: "".to_string(), weight: 1, sequence: 0, on_start: false, on_stop: false, function, } } /// Set an optional name for the task, used when displaying statistics about /// requests made by the task. /// /// Individual requests can also be named withing your load test. See the /// documentation for `GooseClient`.[`set_request_name()`](./struct.GooseClient.html#method.set_request_name) /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// task!(my_task_function).set_name("foo"); /// /// async fn my_task_function(client: &mut GooseClient) { /// let _response = client.get("/"); /// } /// ``` pub fn set_name(mut self, name: &str) -> Self { trace!("[{}] set_name: {}", self.tasks_index, self.name); self.name = name.to_string(); self } /// Set an optional flag indicating that this task should be run when /// a client first starts. This could be used to log the user in, and /// so all subsequent tasks are done as a logged in user. A task with /// this flag set will only run at start time (and optionally at stop /// time as well, if that flag is also set). /// /// On-start tasks can be sequenced and weighted. Sequences allow /// multiple on-start tasks to run in a controlled order. Weights allow /// on-start tasks to run multiple times when a client starts. /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// task!(my_on_start_function).set_on_start(); /// /// async fn my_on_start_function(client: &mut GooseClient) { /// let _response = client.get("/"); /// } /// ``` pub fn set_on_start(mut self) -> Self { trace!("{} [{}] set_on_start task", self.name, self.tasks_index); self.on_start = true; self } /// Set an optional flag indicating that this task should be run when /// a client stops. This could be used to log a user out when the client /// finishes its load test. A task with this flag set will only run at /// stop time (and optionally at start time as well, if that flag is /// also set). /// /// On-stop tasks can be sequenced and weighted. Sequences allow /// multiple on-stop tasks to run in a controlled order. Weights allow /// on-stop tasks to run multiple times when a client stops. /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// task!(my_on_stop_function).set_on_stop(); /// /// async fn my_on_stop_function(client: &mut GooseClient) { /// let _response = client.get("/"); /// } /// ``` pub fn set_on_stop(mut self) -> Self { trace!("{} [{}] set_on_stop task", self.name, self.tasks_index); self.on_stop = true; self } /// Sets a weight on an individual task. The larger the value of weight, the more often it will be run /// in the TaskSet. For example, if one task has a weight of 3 and another task has a weight of 1, the /// first task will run 3 times as often. /// /// # Example /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// task!(task_function).set_weight(3); /// /// async fn task_function(client: &mut GooseClient) { /// let _response = client.get("/"); /// } /// ``` pub fn set_weight(mut self, weight: usize) -> Self { trace!( "{} [{}] set_weight: {}", self.name, self.tasks_index, weight ); if weight < 1 { error!("{} weight of {} not allowed", self.name, weight); std::process::exit(1); } else { self.weight = weight; } self } /// Defines the sequence value of an individual tasks. Tasks are run in order of their sequence value, /// so a task with a sequence value of 1 will run before a task with a sequence value of 2. Tasks with /// no sequence value (or a sequence value of 0) will run last, after all tasks with positive sequence /// values. /// /// All tasks with the same sequence value will run in a random order. Tasks can be assigned both /// squence values and weights. /// /// # Examples /// In this first example, the variable names indicate the order the tasks will be run in: /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let runs_first = task!(first_task_function).set_sequence(3); /// let runs_second = task!(second_task_function).set_sequence(5835); /// let runs_last = task!(third_task_function); /// /// async fn first_task_function(client: &mut GooseClient) { /// let _response = client.get("/1"); /// } /// /// async fn second_task_function(client: &mut GooseClient) { /// let _response = client.get("/2"); /// } /// /// async fn third_task_function(client: &mut GooseClient) { /// let _response = client.get("/3"); /// } /// ``` /// /// In the following example, the `runs_first` task runs two times, then one instance of `runs_second` /// and two instances of `also_runs_second` are all three run. The client will do this over and over /// the entire time it runs, with `runs_first` always running first, then the other tasks being /// run in a random and weighted order: /// ```rust /// use goose::goose::{GooseTask, GooseClient}; /// use goose::task; /// /// // Needed to wrap and store async functions. /// use std::boxed::Box; /// /// let runs_first = task!(first_task_function).set_sequence(1).set_weight(2); /// let runs_second = task!(second_task_function_a).set_sequence(2); /// let also_runs_second = task!(second_task_function_b).set_sequence(2).set_weight(2); /// /// async fn first_task_function(client: &mut GooseClient) { /// let _response = client.get("/1"); /// } /// /// async fn second_task_function_a(client: &mut GooseClient) { /// let _response = client.get("/2a"); /// } /// /// async fn second_task_function_b(client: &mut GooseClient) { /// let _response = client.get("/2b"); /// } /// ``` pub fn set_sequence(mut self, sequence: usize) -> Self { trace!( "{} [{}] set_sequence: {}", self.name, self.tasks_index, sequence ); if sequence < 1 { info!( "setting sequence to 0 for task {} is unnecessary, sequence disabled", self.name ); } self.sequence = sequence; self } } impl Hash for GooseTask { fn hash<H: Hasher>(&self, state: &mut H) { self.tasks_index.hash(state); self.name.hash(state); self.weight.hash(state); self.sequence.hash(state); self.on_start.hash(state); self.on_stop.hash(state); } } #[cfg(test)] mod tests { use super::*; #[test] fn goose_task_set() { // Simplistic test task functions. async fn test_function_a(client: &mut GooseClient) -> () { let _response = client.get("/a/").await; } async fn test_function_b(client: &mut GooseClient) -> () { let _response = client.get("/b/").await; } let mut task_set = taskset!("foo"); assert_eq!(task_set.name, "foo"); assert_eq!(task_set.task_sets_index, usize::max_value()); assert_eq!(task_set.weight, 1); assert_eq!(task_set.min_wait, 0); assert_eq!(task_set.max_wait, 0); assert_eq!(task_set.host, None); assert_eq!(task_set.tasks.len(), 0); assert_eq!(task_set.weighted_tasks.len(), 0); assert_eq!(task_set.weighted_on_start_tasks.len(), 0); assert_eq!(task_set.weighted_on_stop_tasks.len(), 0); // Registering a task adds it to tasks, but doesn't update weighted_tasks. task_set = task_set.register_task(task!(test_function_a)); assert_eq!(task_set.tasks.len(), 1); assert_eq!(task_set.weighted_tasks.len(), 0); assert_eq!(task_set.task_sets_index, usize::max_value()); assert_eq!(task_set.weight, 1); assert_eq!(task_set.min_wait, 0); assert_eq!(task_set.max_wait, 0); assert_eq!(task_set.host, None); // Different task can be registered. task_set = task_set.register_task(task!(test_function_b)); assert_eq!(task_set.tasks.len(), 2); assert_eq!(task_set.weighted_tasks.len(), 0); assert_eq!(task_set.task_sets_index, usize::max_value()); assert_eq!(task_set.weight, 1); assert_eq!(task_set.min_wait, 0); assert_eq!(task_set.max_wait, 0); assert_eq!(task_set.host, None); // Same task can be registered again. task_set = task_set.register_task(task!(test_function_a)); assert_eq!(task_set.tasks.len(), 3); assert_eq!(task_set.weighted_tasks.len(), 0); assert_eq!(task_set.task_sets_index, usize::max_value()); assert_eq!(task_set.weight, 1); assert_eq!(task_set.min_wait, 0); assert_eq!(task_set.max_wait, 0); assert_eq!(task_set.host, None); // Setting weight only affects weight field. task_set = task_set.set_weight(50); assert_eq!(task_set.weight, 50); assert_eq!(task_set.tasks.len(), 3); assert_eq!(task_set.weighted_tasks.len(), 0); assert_eq!(task_set.task_sets_index, usize::max_value()); assert_eq!(task_set.min_wait, 0); assert_eq!(task_set.max_wait, 0); assert_eq!(task_set.host, None); // Weight can be changed. task_set = task_set.set_weight(5); assert_eq!(task_set.weight, 5); // Setting host only affects host field. task_set = task_set.set_host("http://foo.example.com/"); assert_eq!(task_set.host, Some("http://foo.example.com/".to_string())); assert_eq!(task_set.weight, 5); assert_eq!(task_set.tasks.len(), 3); assert_eq!(task_set.weighted_tasks.len(), 0); assert_eq!(task_set.task_sets_index, usize::max_value()); assert_eq!(task_set.min_wait, 0); assert_eq!(task_set.max_wait, 0); // Host field can be changed. task_set = task_set.set_host("https://bar.example.com/"); assert_eq!(task_set.host, Some("https://bar.example.com/".to_string())); // Wait time only affects wait time fields. task_set = task_set.set_wait_time(1, 10); assert_eq!(task_set.min_wait, 1); assert_eq!(task_set.max_wait, 10); assert_eq!(task_set.host, Some("https://bar.example.com/".to_string())); assert_eq!(task_set.weight, 5); assert_eq!(task_set.tasks.len(), 3); assert_eq!(task_set.weighted_tasks.len(), 0); assert_eq!(task_set.task_sets_index, usize::max_value()); // Wait time can be changed. task_set = task_set.set_wait_time(3, 9); assert_eq!(task_set.min_wait, 3); assert_eq!(task_set.max_wait, 9); } #[test] fn goose_task() { // Simplistic test task functions. async fn test_function_a(client: &mut GooseClient) -> () { let _response = client.get("/a/"); } // Initialize task set. let mut task = task!(test_function_a); assert_eq!(task.tasks_index, usize::max_value()); assert_eq!(task.name, "".to_string()); assert_eq!(task.weight, 1); assert_eq!(task.sequence, 0); assert_eq!(task.on_start, false); assert_eq!(task.on_stop, false); // Name can be set, without affecting other fields. task = task.set_name("foo"); assert_eq!(task.name, "foo".to_string()); assert_eq!(task.weight, 1); assert_eq!(task.sequence, 0); assert_eq!(task.on_start, false); assert_eq!(task.on_stop, false); // Name can be set multiple times. task = task.set_name("bar"); assert_eq!(task.name, "bar".to_string()); // On start flag can be set, without affecting other fields. task = task.set_on_start(); assert_eq!(task.on_start, true); assert_eq!(task.name, "bar".to_string()); assert_eq!(task.weight, 1); assert_eq!(task.sequence, 0); assert_eq!(task.on_stop, false); // Setting on start flag twice doesn't change anything. task = task.set_on_start(); assert_eq!(task.on_start, true); // On stop flag can be set, without affecting other fields. // It's possible to set both on_start and on_stop for same task. task = task.set_on_stop(); assert_eq!(task.on_stop, true); assert_eq!(task.on_start, true); assert_eq!(task.name, "bar".to_string()); assert_eq!(task.weight, 1); assert_eq!(task.sequence, 0); // Setting on stop flag twice doesn't change anything. task = task.set_on_stop(); assert_eq!(task.on_stop, true); // Setting weight doesn't change anything else. task = task.set_weight(2); assert_eq!(task.weight, 2); assert_eq!(task.on_stop, true); assert_eq!(task.on_start, true); assert_eq!(task.name, "bar".to_string()); assert_eq!(task.sequence, 0); // Weight field can be changed multiple times. task = task.set_weight(3); assert_eq!(task.weight, 3); // Setting sequence doesn't change anything else. task = task.set_sequence(4); assert_eq!(task.sequence, 4); assert_eq!(task.weight, 3); assert_eq!(task.on_stop, true); assert_eq!(task.on_start, true); assert_eq!(task.name, "bar".to_string()); // Sequence field can be changed multiple times. task = task.set_sequence(8); assert_eq!(task.sequence, 8); } #[test] fn goose_request() { let mut request = GooseRequest::new("/", GooseMethod::GET, 0); assert_eq!(request.path, "/".to_string()); assert_eq!(request.method, GooseMethod::GET); assert_eq!(request.response_times.len(), 0); assert_eq!(request.min_response_time, 0); assert_eq!(request.max_response_time, 0); assert_eq!(request.total_response_time, 0); assert_eq!(request.response_time_counter, 0); assert_eq!(request.status_code_counts.len(), 0); assert_eq!(request.success_count, 0); assert_eq!(request.fail_count, 0); // Tracking a response time updates several fields. request.set_response_time(1); // We've seen only one response time so far. assert_eq!(request.response_times.len(), 1); // We've seen one response time of length 1. assert_eq!(request.response_times[&1], 1); // The minimum response time seen so far is 1. assert_eq!(request.min_response_time, 1); // The maximum response time seen so far is 1. assert_eq!(request.max_response_time, 1); // We've seen a total of 1 ms of response time so far. assert_eq!(request.total_response_time, 1); // We've seen a total of 2 response times so far. assert_eq!(request.response_time_counter, 1); // Nothing else changes. assert_eq!(request.path, "/".to_string()); assert_eq!(request.method, GooseMethod::GET); assert_eq!(request.status_code_counts.len(), 0); assert_eq!(request.success_count, 0); assert_eq!(request.fail_count, 0); // Tracking another response time updates all related fields. request.set_response_time(10); // We've added a new unique response time. assert_eq!(request.response_times.len(), 2); // We've seen the 10 ms response time 1 time. assert_eq!(request.response_times[&10], 1); // Minimum doesn't change. assert_eq!(request.min_response_time, 1); // Maximum is new response time. assert_eq!(request.max_response_time, 10); // Total combined response times is now 11 ms. assert_eq!(request.total_response_time, 11); // We've seen two response times so far. assert_eq!(request.response_time_counter, 2); // Nothing else changes. assert_eq!(request.path, "/".to_string()); assert_eq!(request.method, GooseMethod::GET); assert_eq!(request.status_code_counts.len(), 0); assert_eq!(request.success_count, 0); assert_eq!(request.fail_count, 0); // Tracking another response time updates all related fields. request.set_response_time(10); // We've incremented the counter of an existing response time. assert_eq!(request.response_times.len(), 2); // We've seen the 10 ms response time 2 times. assert_eq!(request.response_times[&10], 2); // Minimum doesn't change. assert_eq!(request.min_response_time, 1); // Maximum doesn't change. assert_eq!(request.max_response_time, 10); // Total combined response times is now 21 ms. assert_eq!(request.total_response_time, 21); // We've seen three response times so far. assert_eq!(request.response_time_counter, 3); // Tracking another response time updates all related fields. request.set_response_time(101); // We've added a new response time for the first time. assert_eq!(request.response_times.len(), 3); // The response time was internally rounded to 100, which we've seen once. assert_eq!(request.response_times[&100], 1); // Minimum doesn't change. assert_eq!(request.min_response_time, 1); // Maximum increases to actual maximum, not rounded maximum. assert_eq!(request.max_response_time, 101); // Total combined response times is now 122 ms. assert_eq!(request.total_response_time, 122); // We've seen four response times so far. assert_eq!(request.response_time_counter, 4); // Tracking another response time updates all related fields. request.set_response_time(102); // Due to rounding, this increments the existing 100 ms response time. assert_eq!(request.response_times.len(), 3); // The response time was internally rounded to 100, which we've now seen twice. assert_eq!(request.response_times[&100], 2); // Minimum doesn't change. assert_eq!(request.min_response_time, 1); // Maximum increases to actual maximum, not rounded maximum. assert_eq!(request.max_response_time, 102); // Add 102 to the total response time so far. assert_eq!(request.total_response_time, 224); // We've seen five response times so far. assert_eq!(request.response_time_counter, 5); // Tracking another response time updates all related fields. request.set_response_time(155); // Adds a new response time. assert_eq!(request.response_times.len(), 4); // The response time was internally rounded to 160, seen for the first time. assert_eq!(request.response_times[&160], 1); // Minimum doesn't change. assert_eq!(request.min_response_time, 1); // Maximum increases to actual maximum, not rounded maximum. assert_eq!(request.max_response_time, 155); // Add 155 to the total response time so far. assert_eq!(request.total_response_time, 379); // We've seen six response times so far. assert_eq!(request.response_time_counter, 6); // Tracking another response time updates all related fields. request.set_response_time(2345); // Adds a new response time. assert_eq!(request.response_times.len(), 5); // The response time was internally rounded to 2000, seen for the first time. assert_eq!(request.response_times[&2000], 1); // Minimum doesn't change. assert_eq!(request.min_response_time, 1); // Maximum increases to actual maximum, not rounded maximum. assert_eq!(request.max_response_time, 2345); // Add 2345 to the total response time so far. assert_eq!(request.total_response_time, 2724); // We've seen seven response times so far. assert_eq!(request.response_time_counter, 7); // Tracking another response time updates all related fields. request.set_response_time(987654321); // Adds a new response time. assert_eq!(request.response_times.len(), 6); // The response time was internally rounded to 987654000, seen for the first time. assert_eq!(request.response_times[&987654000], 1); // Minimum doesn't change. assert_eq!(request.min_response_time, 1); // Maximum increases to actual maximum, not rounded maximum. assert_eq!(request.max_response_time, 987654321); // Add 987654321 to the total response time so far. assert_eq!(request.total_response_time, 987657045); // We've seen eight response times so far. assert_eq!(request.response_time_counter, 8); // Tracking status code updates all related fields. request.set_status_code(Some(StatusCode::OK)); // We've seen only one status code. assert_eq!(request.status_code_counts.len(), 1); // First time seeing this status code. assert_eq!(request.status_code_counts[&200], 1); // As status code tracking is optional, we don't track success/fail here. assert_eq!(request.success_count, 0); assert_eq!(request.fail_count, 0); // Nothing else changes. assert_eq!(request.response_times.len(), 6); assert_eq!(request.min_response_time, 1); assert_eq!(request.max_response_time, 987654321); assert_eq!(request.total_response_time, 987657045); assert_eq!(request.response_time_counter, 8); // Tracking status code updates all related fields. request.set_status_code(Some(StatusCode::OK)); // We've seen only one unique status code. assert_eq!(request.status_code_counts.len(), 1); // Second time seeing this status code. assert_eq!(request.status_code_counts[&200], 2); // Tracking status code updates all related fields. request.set_status_code(None); // We've seen two unique status codes. assert_eq!(request.status_code_counts.len(), 2); // First time seeing a client-side error. assert_eq!(request.status_code_counts[&0], 1); // Tracking status code updates all related fields. request.set_status_code(Some(StatusCode::INTERNAL_SERVER_ERROR)); // We've seen three unique status codes. assert_eq!(request.status_code_counts.len(), 3); // First time seeing an internal server error. assert_eq!(request.status_code_counts[&500], 1); // Tracking status code updates all related fields. request.set_status_code(Some(StatusCode::PERMANENT_REDIRECT)); // We've seen four unique status codes. assert_eq!(request.status_code_counts.len(), 4); // First time seeing an internal server error. assert_eq!(request.status_code_counts[&308], 1); // Tracking status code updates all related fields. request.set_status_code(Some(StatusCode::OK)); // We've seen four unique status codes. assert_eq!(request.status_code_counts.len(), 4); // Third time seeing this status code. assert_eq!(request.status_code_counts[&200], 3); // Nothing else changes. assert_eq!(request.success_count, 0); assert_eq!(request.fail_count, 0); assert_eq!(request.response_times.len(), 6); assert_eq!(request.min_response_time, 1); assert_eq!(request.max_response_time, 987654321); assert_eq!(request.total_response_time, 987657045); assert_eq!(request.response_time_counter, 8); } #[test] fn goose_client() { let configuration = GooseConfiguration::default(); let mut client = GooseClient::new( 0, 0, Some("http://example.com/".to_string()), None, 0, 0, &configuration, 0, ); assert_eq!(client.task_sets_index, 0); assert_eq!(client.default_host, Some("http://example.com/".to_string())); assert_eq!(client.task_set_host, None); assert_eq!(client.min_wait, 0); assert_eq!(client.max_wait, 0); assert_eq!(client.weighted_clients_index, usize::max_value()); assert_eq!(client.mode, GooseClientMode::INIT); assert_eq!(client.weighted_on_start_tasks.len(), 0); assert_eq!(client.weighted_tasks.len(), 0); assert_eq!(client.weighted_bucket, 0); assert_eq!(client.weighted_bucket_position, 0); assert_eq!(client.weighted_on_stop_tasks.len(), 0); assert_eq!(client.task_request_name, None); assert_eq!(client.request_name, None); assert_eq!(client.previous_path, None); assert_eq!(client.previous_method, None); assert_eq!(client.previous_request_name, None); assert_eq!(client.was_success, false); assert_eq!(client.requests.len(), 0); // Setting request name doesn't affect anything else. client.set_request_name("foo"); assert_eq!(client.request_name, Some("foo".to_string())); assert_eq!(client.task_sets_index, 0); assert_eq!(client.default_host, Some("http://example.com/".to_string())); assert_eq!(client.task_set_host, None); assert_eq!(client.min_wait, 0); assert_eq!(client.max_wait, 0); assert_eq!(client.weighted_clients_index, usize::max_value()); assert_eq!(client.mode, GooseClientMode::INIT); assert_eq!(client.weighted_on_start_tasks.len(), 0); assert_eq!(client.weighted_tasks.len(), 0); assert_eq!(client.weighted_bucket, 0); assert_eq!(client.weighted_bucket_position, 0); assert_eq!(client.weighted_on_stop_tasks.len(), 0); assert_eq!(client.task_request_name, None); assert_eq!(client.previous_path, None); assert_eq!(client.previous_method, None); assert_eq!(client.previous_request_name, None); assert_eq!(client.was_success, false); assert_eq!(client.requests.len(), 0); // Can set request name multiple times. client.set_request_name("bar"); assert_eq!(client.request_name, Some("bar".to_string())); // Can change mode. client.set_mode(GooseClientMode::HATCHING); assert_eq!(client.mode, GooseClientMode::HATCHING); // Returns new GooseRequest if never set before. let request = client.get_request("/foo", &GooseMethod::GET); assert_eq!(request, GooseRequest::new("/foo", GooseMethod::GET, 0)); // Store a GooseRequest objet and confirm we can them retreive it. let mut request = GooseRequest::new("/", GooseMethod::GET, 0); request.set_response_time(55); request.set_status_code(Some(StatusCode::OK)); client.set_request("/", &GooseMethod::GET, request.clone()); let restored_request = client.get_request("/", &GooseMethod::GET); // This is not an empty request object. assert_ne!( restored_request, GooseRequest::new("/", GooseMethod::GET, 0) ); // This is the request we stored. assert_eq!(&request, &restored_request); // Make another change to the request and re-store. request.set_response_time(951); request.set_status_code(Some(StatusCode::OK)); client.set_request("/", &GooseMethod::GET, request.clone()); let restored_request_again = client.get_request("/", &GooseMethod::GET); // This is not an empty request object. assert_ne!( restored_request, GooseRequest::new("/", GooseMethod::GET, 0) ); // This is not first request we stored. assert_ne!(&request, &restored_request); // This is the new request we stored. assert_eq!(&request, &restored_request_again); // Confirm the URLs are correctly built using the default_host. let url = client.build_url("/foo"); assert_eq!(url, "http://example.com/foo"); let url = client.build_url("bar/"); assert_eq!(url, "http://example.com/bar/"); let url = client.build_url("/foo/bar"); assert_eq!(url, "http://example.com/foo/bar"); // Confirm the URLs are built with their own specified host. let url = client.build_url("https://example.com/foo"); assert_eq!(url, "https://example.com/foo"); let url = client.build_url("https://www.example.com/path/to/resource"); assert_eq!(url, "https://www.example.com/path/to/resource"); // Create a second client, this time setting a task_set_host. let mut client2 = GooseClient::new( 0, 0, Some("http://www.example.com/".to_string()), Some("http://www2.example.com/".to_string()), 1, 3, &configuration, 0, ); assert_eq!( client2.default_host, Some("http://www.example.com/".to_string()) ); assert_eq!( client2.task_set_host, Some("http://www2.example.com/".to_string()) ); assert_eq!(client2.min_wait, 1); assert_eq!(client2.max_wait, 3); // Confirm the URLs are correctly built using the task_set_host. let url = client2.build_url("/foo"); assert_eq!(url, "http://www2.example.com/foo"); // Confirm URLs are still built with their own specified host. let url = client.build_url("https://example.com/foo"); assert_eq!(url, "https://example.com/foo"); // Create a GET request. let mut goose_request = client.goose_get("/foo"); let mut built_request = goose_request.build().unwrap(); assert_eq!(built_request.method(), &Method::GET); assert_eq!(built_request.url().as_str(), "http://example.com/foo"); assert_eq!(built_request.timeout(), None); // Create a POST request. goose_request = client.goose_post("/path/to/post"); built_request = goose_request.build().unwrap(); assert_eq!(built_request.method(), &Method::POST); assert_eq!( built_request.url().as_str(), "http://example.com/path/to/post" ); assert_eq!(built_request.timeout(), None); // Create a PUT request. goose_request = client.goose_put("/path/to/put"); built_request = goose_request.build().unwrap(); assert_eq!(built_request.method(), &Method::PUT); assert_eq!( built_request.url().as_str(), "http://example.com/path/to/put" ); assert_eq!(built_request.timeout(), None); // Create a PATCH request. goose_request = client.goose_patch("/path/to/patch"); built_request = goose_request.build().unwrap(); assert_eq!(built_request.method(), &Method::PATCH); assert_eq!( built_request.url().as_str(), "http://example.com/path/to/patch" ); assert_eq!(built_request.timeout(), None); // Create a DELETE request. goose_request = client.goose_delete("/path/to/delete"); built_request = goose_request.build().unwrap(); assert_eq!(built_request.method(), &Method::DELETE); assert_eq!( built_request.url().as_str(), "http://example.com/path/to/delete" ); assert_eq!(built_request.timeout(), None); // Create a HEAD request. goose_request = client.goose_head("/path/to/head"); built_request = goose_request.build().unwrap(); assert_eq!(built_request.method(), &Method::HEAD); assert_eq!( built_request.url().as_str(), "http://example.com/path/to/head" ); assert_eq!(built_request.timeout(), None); } }