Skip to content

Commit e04636c

Browse files
authored
add response time profiling code, add response time profiling to /projects (#222)
* add response time profiling code, add profile to /projects * add profiling api endpoint * remove report name assignment from startTimeRecord method * remove explicit return from startRimeRecord method * bump version -> v1.0.17
1 parent a627f41 commit e04636c

File tree

12 files changed

+4265
-5680
lines changed

12 files changed

+4265
-5680
lines changed

.env.next

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
GA_TRACKING_ID="UA-123456789-0"
22
GATSBY_API_ENDPOINT="https://pub-api-test.azurewebsites.net/api"
3+
GATSBY_PROFILING_API_ENDPOINT="https://pub-profiler-collector-test.azurewebsites.net"
34
BUILD_PATH_PREFIX="/"
45
BUILD_SITE_URL="https://projectunicorn-next.surge.sh"

.env.release

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
GATSBY_API_ENDPOINT="https://pub-api.azurewebsites.net/api"
2+
GATSBY_PROFILING_API_ENDPOINT="https://pub-profiler-collector.azurewebsites.net"
23
BUILD_PATH_PREFIX="/"
34
BUILD_SITE_URL="https://projectunicorn.net"

package-lock.json

Lines changed: 4149 additions & 5675 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pub",
3-
"version": "1.0.16",
3+
"version": "1.0.17",
44
"private": true,
55
"scripts": {
66
"build": "gatsby build --prefix-paths",
@@ -44,7 +44,7 @@
4444
"gatsby-transformer-remark": "2.7.1",
4545
"gatsby-transformer-sharp": "2.4.4",
4646
"gatsby-transformer-yaml": "2.3.1",
47-
"lodash": "^4.17.15",
47+
"lodash": "^4.17.19",
4848
"prismjs": "1.20.0",
4949
"prop-types": "15.7.2",
5050
"react": "16.13.1",

src/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './types';
22
export * from './api-service';
33
export * from './auth-service';
4+
export * from './profiling-service';
45
export * from './http-client';
56
export * from './service-resolver';
67
export * from './stack-exchange-service';

src/api/profiling-service.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { HttpClient } from './http-client';
2+
3+
export interface ProfilingReport {
4+
name: string;
5+
value: number;
6+
additionalInfo: string;
7+
}
8+
9+
export class ProfilingService {
10+
private headers = {
11+
Accept: 'application/json',
12+
'Content-Type': 'application/json; charset=utf-8',
13+
};
14+
15+
private apiEndpoint: string;
16+
17+
public constructor() {
18+
this.apiEndpoint = process.env.GATSBY_PROFILING_API_ENDPOINT || '';
19+
}
20+
21+
public async sendReport(report: ProfilingReport) {
22+
return await HttpClient.post(
23+
`${this.apiEndpoint}/report`,
24+
this.headers,
25+
report,
26+
);
27+
}
28+
}

src/api/service-resolver.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import { ApiService } from './api-service';
2-
import { AuthService } from './auth-service';
3-
import { StackExchangeService } from './stack-exchange-service';
1+
import {
2+
ApiService,
3+
AuthService,
4+
ProfilingService,
5+
StackExchangeService,
6+
} from './';
47
import {
58
MockApiService,
69
MockAuthService,
10+
MockProfilingService,
711
MockStackExchangeService,
812
} from '@mocks';
913

1014
export class ServiceResolver {
1115
private static apiServiceInstance?: MockApiService | ApiService;
1216
private static authServiceInstance?: MockAuthService | AuthService;
17+
private static profilingServiceInstance?:
18+
| MockProfilingService
19+
| ProfilingService;
1320
private static stackExchangeServiceInstance?:
1421
| MockStackExchangeService
1522
| StackExchangeService;
@@ -22,6 +29,10 @@ export class ServiceResolver {
2229
return this.getAuthServiceInstance();
2330
}
2431

32+
public static profilingResolver() {
33+
return this.getProfilingServiceInstancxe();
34+
}
35+
2536
public static stackExchangeResolver() {
2637
return this.getStackExchangeServiceInstance();
2738
}
@@ -44,6 +55,15 @@ export class ServiceResolver {
4455
return this.authServiceInstance;
4556
}
4657

58+
private static getProfilingServiceInstancxe() {
59+
if (this.profilingServiceInstance === undefined) {
60+
this.profilingServiceInstance = this.useMock()
61+
? new MockProfilingService()
62+
: new ProfilingService();
63+
}
64+
return this.profilingServiceInstance;
65+
}
66+
4767
private static getStackExchangeServiceInstance() {
4868
if (this.stackExchangeServiceInstance === undefined) {
4969
this.stackExchangeServiceInstance = this.useMock()

src/components/projects/project-gallery/project-gallery.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ApiResponse, ErrorResponse, Project, ServiceResolver } from '@api';
66
import { FeedbackForm } from '@components/shared/form';
77
import { CloseButton, Ribbon } from '@components/shared/ribbons';
88
import { Loader, Seo, Wrapper } from '@components/shared';
9+
import { ProfilingUtils } from '@utils';
910

1011
type OwnProps = {};
1112
type ProjectGalleryProps = OwnProps & RouteComponentProps;
@@ -19,6 +20,11 @@ const ProjectGallery: FC<ProjectGalleryProps> = () => {
1920

2021
useEffect(() => {
2122
const api = ServiceResolver.apiResolver();
23+
const profiler = new ProfilingUtils(
24+
'pub-ui-projects-response-time',
25+
'measure the response time of /projects from client-side.',
26+
);
27+
profiler.startTimeRecord();
2228

2329
async function fetchContent() {
2430
try {
@@ -29,6 +35,7 @@ const ProjectGallery: FC<ProjectGalleryProps> = () => {
2935
if (response.ok) {
3036
const projects = response.data as Project[];
3137
setProjects(projects);
38+
profiler.endTimeRecord();
3239
} else {
3340
// TODO: remove, currently this will never execute.
3441
setError((response.data as ErrorResponse).message);

src/mocks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './responses';
22
export * from './mock-api-service';
33
export * from './mock-auth-service';
4+
export * from './mock-profiling-service';
45
export * from './mock-stack-exchange-service';
56
export * from './mock-theme-provider';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ProfilingReport } from '@api/profiling-service';
2+
3+
export class MockProfilingService {
4+
public sendReport(report: ProfilingReport) {
5+
return report;
6+
}
7+
}

0 commit comments

Comments
 (0)