Skip to content

Commit eb8ca2d

Browse files
authored
chore: add method to encode profile (#14)
* chore: add method to encode profile * address comments
1 parent 6fb69a1 commit eb8ca2d

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

ts/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import {gzipSync} from 'zlib';
1919
import {perftools} from '../../proto/profile';
2020

2121
import * as heapProfiler from './heap-profiler';
22+
import {encodeSync} from './profile-encoder';
2223
import * as timeProfiler from './time-profiler';
2324

25+
export {encode, encodeSync} from './profile-encoder';
2426
export {SourceMapper} from './sourcemapper/sourcemapper';
2527

2628
export const time = {
@@ -41,8 +43,7 @@ if (module.parent && module.parent.id === 'internal/preload') {
4143
// The process is going to terminate imminently. All work here needs to
4244
// be synchronous.
4345
const profile = stop();
44-
const buffer = perftools.profiles.Profile.encode(profile).finish();
45-
const compressed = gzipSync(buffer);
46-
writeFileSync(`pprof-profile-${process.pid}.pb.gz`, compressed);
46+
const buffer = encodeSync(profile);
47+
writeFileSync(`pprof-profile-${process.pid}.pb.gz`, buffer);
4748
});
4849
}

ts/src/profile-encoder.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2019 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as pify from 'pify';
18+
import {gzip, gzipSync} from 'zlib';
19+
20+
import {perftools} from '../../proto/profile';
21+
22+
const gzipPromise = pify(gzip);
23+
24+
export async function encode(profile: perftools.profiles.IProfile):
25+
Promise<Buffer> {
26+
const buffer = perftools.profiles.Profile.encode(profile).finish();
27+
return gzipPromise(buffer);
28+
}
29+
30+
export function encodeSync(profile: perftools.profiles.IProfile): Buffer {
31+
const buffer = perftools.profiles.Profile.encode(profile).finish();
32+
return gzipSync(buffer);
33+
}

ts/test/test-profile-encoder.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2019 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as pify from 'pify';
18+
import {gunzip as gunzipPromise, gunzipSync} from 'zlib';
19+
20+
import {perftools} from '../../proto/profile';
21+
import {encode, encodeSync} from '../src/profile-encoder';
22+
23+
import {decodedTimeProfile, timeProfile} from './profiles-for-tests';
24+
25+
const assert = require('assert');
26+
const gunzip = pify(gunzipPromise);
27+
28+
describe('profile-encoded', () => {
29+
describe('encode', () => {
30+
it('should encode profile such that the encoded profile can be decoded',
31+
async () => {
32+
const encoded = await encode(timeProfile);
33+
const unzipped = await gunzip(encoded);
34+
const decoded = perftools.profiles.Profile.decode(unzipped);
35+
assert.deepEqual(decoded, decodedTimeProfile);
36+
});
37+
});
38+
describe('encodeSync', () => {
39+
it('should encode profile such that the encoded profile can be decoded',
40+
() => {
41+
const encoded = encodeSync(timeProfile);
42+
const unzipped = gunzipSync(encoded);
43+
const decoded = perftools.profiles.Profile.decode(unzipped);
44+
assert.deepEqual(decoded, decodedTimeProfile);
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)