Skip to content

Commit 171d29b

Browse files
committed
feat: working on chapter 11
1 parent b5af693 commit 171d29b

File tree

9 files changed

+761
-0
lines changed

9 files changed

+761
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sales/
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 04-batching-and-caching
2+
3+
This sample shocases how asynchronous batching and caching work.
4+
5+
## Dependencies
6+
7+
This example requires you to install some third-party dependencies from npm.
8+
9+
If you have `pnpm` installed, you can do that with:
10+
11+
```bash
12+
pnpm install
13+
```
14+
15+
Alternatively, if you prefer to use another package manager, make sure to delete
16+
the `pnpm-lock.yaml` file before using it.
17+
18+
If you want to use `npm`, you can run:
19+
20+
```bash
21+
npm install
22+
```
23+
24+
If you want to use `yarn`, you can run:
25+
26+
```bash
27+
yarn install
28+
```
29+
30+
## Run
31+
32+
First, populate the database with some sample data:
33+
34+
```
35+
node populateDb.js
36+
```
37+
38+
The command above will create 100000 random sales transactions in the format:
39+
40+
```
41+
{amount, product}
42+
```
43+
44+
Next, to start the server, run:
45+
46+
```
47+
node server.js
48+
```
49+
50+
To test the server with multiple concurrent request, simply run:
51+
52+
```
53+
autocannon 'http://localhost:8000/?product=book'
54+
```
55+
56+
In the file `server.js`, try to swap between the various implementations of the
57+
`totalSales()` API to compare their performances. Please note that using the
58+
vanilla implementation (`totalSales`).
59+
60+
```
61+
import { totalSales } from './totalSales.js'
62+
// import {totalSales} from './totalSalesBatch.js'
63+
// import {totalSales} from './totalSalesCache.js'
64+
```
65+
66+
NOTE: remember to restart the server after changing the implementation before
67+
running the benchmark again.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "04-batching-and-caching",
3+
"version": "1.0.0",
4+
"description": "This sample showcases how asynchronous batching and caching work",
5+
"type": "module",
6+
"scripts": {},
7+
"engines": {
8+
"node": ">=23"
9+
},
10+
"engineStrict": true,
11+
"keywords": [],
12+
"author": "Luciano Mammino and Mario Casciaro",
13+
"license": "MIT",
14+
"dependencies": {
15+
"level": "^10.0.0",
16+
"nanoid": "^5.1.5"
17+
},
18+
"devDependencies": {
19+
"autocannon": "^8.0.0"
20+
}
21+
}

0 commit comments

Comments
 (0)