Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ function CharacterDetailScreen(): React.JSX.Element {

const onShowEpisodes = async () => {
try {
const episodeNames = await Promise.all(episodeURLs.map((url) =>
RickMortyService.fetchRequest(url).then(json => json.name)
));
const episodeData = await RickMortyService.fetchEpisodesByIds(episodeURLs);
const episodeNames = episodeData.map(episode => episode.name);
setEpisodes(episodeNames);
} catch (_error) {
Alert.alert("Something went wrong. Please try again later.");
Expand Down Expand Up @@ -86,4 +85,4 @@ function CharacterDetailScreen(): React.JSX.Element {
);
};

export default CharacterDetailScreen;
export default CharacterDetailScreen;
8 changes: 3 additions & 5 deletions benchmarks/src/scenario/RUM/Auto/screens/episodeDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ function EpisodeDetailScreen(): React.JSX.Element {
const getCharacters = async () => {
try {
setIsLoading(true);
const charaterList = await Promise.all(characterURLS.map((url) =>
RickMortyService.fetchRequest(url).then(json => json as Character)
));
setCharacters(charaterList);
const characterList = await RickMortyService.fetchCharactersByIds(characterURLS);
setCharacters(characterList as Character[]);
} catch (_error) {
Alert.alert("Something went wrong. Please try again later.");
} finally {
Expand Down Expand Up @@ -103,4 +101,4 @@ function EpisodeDetailScreen(): React.JSX.Element {
);
};

export default EpisodeDetailScreen;
export default EpisodeDetailScreen;
8 changes: 3 additions & 5 deletions benchmarks/src/scenario/RUM/Auto/screens/locationDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ function LocationDetailScreen(): React.JSX.Element {
const getCharacters = async () => {
try {
setIsLoading(true);
const charaterList = await Promise.all(characterURLS.map((url) =>
RickMortyService.fetchRequest(url).then(json => json as Character)
));
setCharacters(charaterList);
const characterList = await RickMortyService.fetchCharactersByIds(characterURLS);
setCharacters(characterList as Character[]);
} catch (_error) {
Alert.alert("Something went wrong. Please try again later.");
} finally {
Expand Down Expand Up @@ -103,4 +101,4 @@ function LocationDetailScreen(): React.JSX.Element {
);
};

export default LocationDetailScreen;
export default LocationDetailScreen;
118 changes: 106 additions & 12 deletions benchmarks/src/scenario/RUM/Auto/service/rickMorty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,121 @@ const CHARACTERS_ENDPOINT = "character";
const LOCATIONS_ENDPOINT = "location";
const EPISODES_ENDPOINT = "episode";

const MAX_CONCURRENT_REQUESTS = 1;
const REQUEST_DELAY_MS = 600;
interface QueuedRequest {
url: string;
resolve: (value: any) => void;
reject: (error: any) => void;
}

class RickMortyService {
fetchRequest(url: string, page?: number) {
const fullURL = url + (page ? ("?page=" + page.toString()) : '');
return fetch(fullURL).then((data) => {
return data.json();
}).catch((_error) => {
return Promise.reject();
})
};
private requestQueue: QueuedRequest[] = [];
private activeRequests = 0;
private isProcessing = false;

private delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}

private async processQueue(): Promise<void> {
if (this.isProcessing) {
return;
}

this.isProcessing = true;

while (this.requestQueue.length > 0 && this.activeRequests < MAX_CONCURRENT_REQUESTS) {
const request = this.requestQueue.shift();
if (!request) break;

this.activeRequests++;

try {
await this.delay(REQUEST_DELAY_MS);

const response = await fetch(request.url);

if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}

const data = await response.json();
request.resolve(data);
} catch (error) {
request.reject(error);
} finally {
this.activeRequests--;
}
}

this.isProcessing = false;

if (this.requestQueue.length > 0) {
this.processQueue();
}
}

fetchRequest(url: string, page?: number): Promise<any> {
const fullURL = url + (page ? `?page=${page}` : '');

return new Promise((resolve, reject) => {
this.requestQueue.push({
url: fullURL,
resolve,
reject
});

this.processQueue();
});
}

private extractIdFromUrl(url: string): string | null {
const match = url.match(/\/(\d+)$/);
return match ? match[1] : null;
}

private async fetchByIds(endpoint: string, urls: string[], resourceType: string): Promise<any[]> {
const ids = urls.map(url => this.extractIdFromUrl(url)).filter(Boolean);
if (ids.length === 0) return [];

const batchUrl = `${BASE_URL}/${endpoint}/${ids.join(',')}`;

try {
const response = await fetch(batchUrl);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
return Array.isArray(data) ? data : [data];
} catch (error) {
throw error;
}
}

fetchCharacters(page?: number) {
return this.fetchRequest(BASE_URL + "/" + CHARACTERS_ENDPOINT, page);
};
}

fetchLocations(page?: number) {
return this.fetchRequest(BASE_URL + "/" + LOCATIONS_ENDPOINT, page);
};
}

fetchEpisodes(page?: number) {
return this.fetchRequest(BASE_URL + "/" + EPISODES_ENDPOINT, page);
};
}

fetchCharactersByIds(urls: string[]): Promise<any[]> {
return this.fetchByIds(CHARACTERS_ENDPOINT, urls, 'characters');
}

fetchEpisodesByIds(urls: string[]): Promise<any[]> {
return this.fetchByIds(EPISODES_ENDPOINT, urls, 'episodes');
}

fetchLocationsByIds(urls: string[]): Promise<any[]> {
return this.fetchByIds(LOCATIONS_ENDPOINT, urls, 'locations');
}
};

export default new RickMortyService();
export default new RickMortyService();
Loading