Skip to content

Commit c028b3c

Browse files
author
Nikhil Thorat
authored
Move util.now() implementation into platform. (tensorflow#1834)
FEATURE
1 parent 877681f commit c028b3c

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

src/platforms/platform.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ export interface Platform {
2929
*/
3030
fetch(path: string, requestInits?: RequestInit): Promise<Response>;
3131

32+
/**
33+
* Returns the current high-resolution time in milliseconds relative to an
34+
* arbitrary time in the past. It works across different platforms (node.js,
35+
* browsers).
36+
*/
37+
now(): number;
38+
3239
/**
3340
* Encode the provided string into an array of bytes using the provided
3441
* encoding.

src/platforms/platform_browser.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export class PlatformBrowser implements Platform {
2626
this.textEncoder = new TextEncoder();
2727
}
2828

29+
fetch(path: string, init?: RequestInit): Promise<Response> {
30+
return fetch(path, init);
31+
}
32+
33+
now(): number {
34+
return performance.now();
35+
}
36+
2937
encode(text: string, encoding: string): Uint8Array {
3038
if (encoding !== 'utf-8' && encoding !== 'utf8') {
3139
throw new Error(
@@ -36,9 +44,6 @@ export class PlatformBrowser implements Platform {
3644
decode(bytes: Uint8Array, encoding: string): string {
3745
return new TextDecoder(encoding).decode(bytes);
3846
}
39-
fetch(path: string, init?: RequestInit): Promise<Response> {
40-
return fetch(path, init);
41-
}
4247
}
4348

4449
if (ENV.get('IS_BROWSER')) {

src/platforms/platform_browser_test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ describeWithFlags('PlatformBrowser', BROWSER_ENVS, async () => {
3030
expect(self.fetch).toHaveBeenCalledWith('test/url', {method: 'GET'});
3131
});
3232

33+
it('now should use performance.now', async () => {
34+
const platform = new PlatformBrowser();
35+
36+
const ms = 1234567;
37+
spyOn(performance, 'now').and.returnValue(ms);
38+
expect(platform.now()).toEqual(ms);
39+
});
40+
3341
it('encodeUTF8 single string', () => {
3442
const platform = new PlatformBrowser();
3543
const bytes = platform.encode('hello', 'utf-8');

src/platforms/platform_node.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ export class PlatformNode implements Platform {
3939
this.textEncoder = new this.util.TextEncoder();
4040
}
4141

42+
fetch(path: string, requestInits?: RequestInit): Promise<Response> {
43+
if (ENV.global.fetch != null) {
44+
return ENV.global.fetch(path, requestInits);
45+
}
46+
47+
if (systemFetch == null) {
48+
systemFetch = getNodeFetch.importFetch();
49+
}
50+
return systemFetch(path, requestInits);
51+
}
52+
53+
now(): number {
54+
const time = process.hrtime();
55+
return time[0] * 1000 + time[1] / 1000000;
56+
}
57+
4258
encode(text: string, encoding: string): Uint8Array {
4359
if (encoding !== 'utf-8' && encoding !== 'utf8') {
4460
throw new Error(
@@ -52,16 +68,6 @@ export class PlatformNode implements Platform {
5268
}
5369
return new this.util.TextDecoder(encoding).decode(bytes);
5470
}
55-
fetch(path: string, requestInits?: RequestInit): Promise<Response> {
56-
if (ENV.global.fetch != null) {
57-
return ENV.global.fetch(path, requestInits);
58-
}
59-
60-
if (systemFetch == null) {
61-
systemFetch = getNodeFetch.importFetch();
62-
}
63-
return systemFetch(path, requestInits);
64-
}
6571
}
6672

6773
if (ENV.get('IS_NODE')) {

src/platforms/platform_node_test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ describeWithFlags('PlatformNode', NODE_ENVS, () => {
6868
ENV.global.fetch = globalFetch;
6969
});
7070

71+
it('now should use process.hrtime', async () => {
72+
const time = [100, 200];
73+
spyOn(process, 'hrtime').and.returnValue(time);
74+
expect(ENV.platform.now()).toEqual(time[0] * 1000 + time[1] / 1000000);
75+
});
76+
7177
it('encodeUTF8 single string', () => {
7278
const platform = new PlatformNode();
7379
const bytes = platform.encode('hello', 'utf-8');

src/util.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -653,16 +653,7 @@ export function makeZerosTypedArray<D extends DataType>(
653653
*/
654654
/** @doc {heading: 'Util', namespace: 'util'} */
655655
export function now(): number {
656-
if (typeof performance !== 'undefined') {
657-
return performance.now();
658-
} else if (typeof process !== 'undefined') {
659-
const time = process.hrtime();
660-
return time[0] * 1000 + time[1] / 1000000;
661-
} else {
662-
throw new Error(
663-
'Cannot measure time in this environment. You should run tf.js ' +
664-
'in the browser or in Node.js');
665-
}
656+
return ENV.platform.now();
666657
}
667658

668659
export function assertNonNegativeIntegerDimensions(shape: number[]) {

0 commit comments

Comments
 (0)