Skip to content

Commit c0bcaa7

Browse files
committed
cached latest
1 parent a9bf511 commit c0bcaa7

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

perf_lab_test/main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const axios = require('axios');
22

33
const ENDPOINTS = {
4-
A: 'https://prod-gw-2vzgiib6.ue.gateway.dev/v1/technologies',
5-
B: 'https://dev-gw-2vzgiib6.uc.gateway.dev/v1/technologies'
4+
A: 'https://prod-gw-2vzgiib6.ue.gateway.dev/v1/cwv?technology=WordPress,Shopify,Wix,Joomla,Drupal,Squarespace,PrestaShop,Webflow,1C-Bitrix,Tilda&geo=United%20States%20of%20America&rank=Top%20100k&start=latest',
5+
B: 'https://reports-dev-2vzgiib6.uc.gateway.dev/v1/cwv?technology=WordPress,Shopify,Wix,Joomla,Drupal,Squarespace,PrestaShop,Webflow,1C-Bitrix,Tilda&geo=United%20States%20of%20America&rank=Top%20100k&start=latest'
66
};
77

8-
const NUM_REQUESTS = 200;
8+
const NUM_REQUESTS = 100;
99
const CONCURRENCY = 10;
1010
const MAX_JITTER_MS = 100;
1111

src/utils/controllerHelpers.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,42 @@ const sendValidationError = (res, errors) => {
3838
res.end(JSON.stringify(createErrorResponse(errors)));
3939
};
4040

41+
// Cache for latest dates to avoid repeated queries
42+
const latestDateCache = new Map();
43+
const CACHE_TTL = 60 * 60 * 1000; // 1 hour in milliseconds
44+
4145
/**
42-
* Get the latest date from a collection
46+
* Get the latest date from a collection with caching
4347
* @param {Object} firestore - Firestore instance
4448
* @param {string} collection - Collection name
4549
* @returns {string|null} - Latest date or null
4650
*/
4751
const getLatestDate = async (firestore, collection) => {
52+
const now = Date.now();
53+
const cacheKey = collection;
54+
const cached = latestDateCache.get(cacheKey);
55+
56+
// Check if we have a valid cached result
57+
if (cached && (now - cached.timestamp) < CACHE_TTL) {
58+
return cached.date;
59+
}
60+
61+
// Query for latest date
4862
const query = firestore.collection(collection).orderBy('date', 'desc').limit(1);
4963
const snapshot = await query.get();
64+
65+
let latestDate = null;
5066
if (!snapshot.empty) {
51-
return snapshot.docs[0].data().date;
67+
latestDate = snapshot.docs[0].data().date;
5268
}
53-
return null;
69+
70+
// Cache the result
71+
latestDateCache.set(cacheKey, {
72+
date: latestDate,
73+
timestamp: now
74+
});
75+
76+
return latestDate;
5477
};
5578

5679
/**

test-api.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ test_endpoint "/v1/geos" ""
104104
test_endpoint "/v1/adoption" "?technology=WordPress&geo=ALL&rank=ALL&start=latest"
105105

106106
# Test cwv endpoint
107-
test_endpoint "/v1/cwv" "?technology=WordPress&geo=ALL&rank=ALL&start=latest"
107+
test_endpoint "/v1/cwv" "?technology=WordPress,Drupal&geo=ALL&rank=ALL&start=latest"
108108

109109
# Test lighthouse endpoint
110110
test_endpoint "/v1/lighthouse" "?technology=WordPress&geo=ALL&rank=ALL&start=latest"

0 commit comments

Comments
 (0)