Skip to content

Commit 76c1d5a

Browse files
Merge pull request #7 from julianrubisch/log-timing
Log timing
2 parents 12039b2 + 4211862 commit 76c1d5a

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,12 @@ const middleWare = (module.exports = function(options) {
7272
res.locals.cacheKey || res.locals.fetchUrl
7373
);
7474

75+
const startTime = process.hrtime();
76+
7577
try {
7678
if (fs.existsSync(assetCachePath)) {
7779
const firstFile = fs.readdirSync(assetCachePath)[0];
7880

79-
if (options.logger)
80-
options.logger.debug(
81-
`Reading buffer from path ${assetCachePath}/${firstFile}`
82-
);
83-
8481
const [contentType, contentLength] = middleWare.decodeAssetCacheName(
8582
firstFile
8683
);
@@ -89,6 +86,14 @@ const middleWare = (module.exports = function(options) {
8986
res.locals.contentType = contentType;
9087

9188
res.locals.buffer = fs.readFileSync(`${assetCachePath}/${firstFile}`);
89+
90+
const [seconds, nanoSeconds] = process.hrtime(startTime);
91+
if (options.logger)
92+
options.logger.info(
93+
`Read buffer from path ${assetCachePath}/${firstFile} in ${seconds *
94+
1000 +
95+
nanoSeconds / 1e6} ms`
96+
);
9297
} else {
9398
// node 10 supports recursive: true, but who knows?
9499
middleWare.makeDirIfNotExists(options.cacheDir);
@@ -101,17 +106,21 @@ const middleWare = (module.exports = function(options) {
101106
const blob = await (await fetch(res.locals.fetchUrl)).blob();
102107

103108
const fileName = middleWare.encodeAssetCacheName(blob.type, blob.size);
104-
if (options.logger)
105-
options.logger.debug(
106-
`Writing buffer to path ${assetCachePath}/${fileName}`
107-
);
108109

109110
res.locals.buffer = Buffer.from(await blob.arrayBuffer(), "binary");
110111

111112
res.locals.contentType = blob.type;
112113
res.locals.contentLength = blob.size;
113114

114115
fs.writeFileSync(`${assetCachePath}/${fileName}`, res.locals.buffer);
116+
117+
const [seconds, nanoSeconds] = process.hrtime(startTime);
118+
if (options.logger)
119+
options.logger.info(
120+
`Wrote buffer to path ${assetCachePath}/${fileName} in ${seconds *
121+
1000 +
122+
nanoSeconds / 1e6} ms`
123+
);
115124
}
116125

117126
next();

0 commit comments

Comments
 (0)