Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-26115] TiAPI: Add support for console.timeLog #10156

Merged
merged 4 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 23 additions & 8 deletions android/runtime/common/src/js/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ function join(args) {
}).join(' ');
}

function logTime(label, logData) {
label = `${label}`;
const startTime = times.get(label);
if (!startTime) {
exports.warn(`Label "${label}" does not exist`);
return true;
}
const duration = Date.now() - startTime;
if (logData) {
exports.log(`${label}: ${duration}ms`, ...logData);
} else {
exports.log(`${label}: ${duration}ms`);
}
return false;
}

exports.log = function () {
Titanium.API.info(join(arguments));
};
Expand Down Expand Up @@ -56,13 +72,12 @@ exports.time = function (label = 'default') {
};

exports.timeEnd = function (label = 'default') {
label = `${label}`;
const startTime = times.get(label);
if (!startTime) {
exports.warn(`Label "${label}" does not exist`);
return;
const warned = logTime(label);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the use of warned here is a little confusing. What you're really determining is if the label existed.

if (!warned) {
times.delete(label);
}
const duration = Date.now() - startTime;
exports.log(`${label}: ${duration}ms`);
times.delete(label);
};

exports.timeLog = function (label = 'default', ...logData) {
logTime(label, logData);
};
20 changes: 20 additions & 0 deletions apidoc/Global/console/console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,23 @@ methods:
optional: true
default: "default"
since: 7.3.0

- name: timeLog
summary: Log duration taken so far for an operation.
description: |
Output the time since a timer was started by calling
[console.time](Global.console.time) to the console, as well as any
other `data` arguments provided. To log extra data a label must
be provided. If not timer exists a warning will be logged to the
console.
parameters:
- name: label
summary: The label to track the timer by
type: String
optional: true
default: "default"
- name: ...data
summary: Extra log data to be provided when logging
type: Object
optional: true
since: 7.4.0
20 changes: 19 additions & 1 deletion iphone/Classes/TiConsole.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,30 @@ - (void)timeEnd:(id)label
[self logMessage:[logMessage componentsSeparatedByString:@" "] severity:@"warn"];
return;
}
double duration = [startTime timeIntervalSinceNow] * -1000;
NSTimeInterval duration = [startTime timeIntervalSinceNow] * -1000;
NSString *logMessage = [NSString stringWithFormat:@"%@: %0.fms", label, duration];
[self logMessage:[logMessage componentsSeparatedByString:@" "] severity:@"info"];
[_times removeObjectForKey:label];
}

- (void)timeLog:(id)args
{
NSString *label = [args objectAtIndex:0] ?: @"default";
NSArray *logData = [args count] > 1 ? [args subarrayWithRange:NSMakeRange(1, [args count] - 1)] : nil;
NSDate *startTime = _times[label];
if (startTime == nil) {
NSString *logMessage = [NSString stringWithFormat:@"Label \"%@\" does not exist", label];
[self logMessage:[logMessage componentsSeparatedByString:@" "] severity:@"warn"];
return;
}
NSTimeInterval duration = [startTime timeIntervalSinceNow] * -1000;
NSString *logMessage = [NSString stringWithFormat:@"%@: %0.fms ", label, duration];
if ([logData count] > 0) {
logMessage = [logMessage stringByAppendingString:[logData componentsJoinedByString:@" "]];
}
[self logMessage:[logMessage componentsSeparatedByString:@" "] severity:@"info"];
}

- (void)dealloc
{
RELEASE_TO_NIL(_times);
Expand Down
65 changes: 65 additions & 0 deletions tests/Resources/console.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2018-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions'); // eslint-disable-line no-unused-vars

describe('console', function () {
describe('#timeLog', function () {
it('is a function', function () {
should(console.timeLog).be.a.Function;
});

// FIXME Due to native impl on iOS that doesn't defer to the JS object, we cannot intercept the log calls
it.iosBroken('prefixes logs with label: and ends with millisecond timing', function () {
var orig = console.log,
logs = [];
try {
console.log = function (string) {
logs.push(string);
};
console.time('mytimer'); // Start timer
console.timeLog('mytimer'); // Log time taken so far
console.timeLog('mytimer', 'with', 'some', 'extra', 'info'); // Log time taken with extra logging
console.timeLog('mytimer', [ 'a', 'b', 'c' ], { objects: true }); // Should handle Arrays and Objects
console.timeEnd('mytimer');
logs.length.should.eql(4);
// We don't worry about the actual optional data sent along, just the format of the string
logs.forEach(function (log) {
log.should.match(/mytimer: \d+ms/);
});
} finally {
console.log = orig;
}
});

// FIXME Due to native impl on iOS that doesn't defer to the JS object, we cannot intercept the log calls
it.iosBroken('warns and does not log if label doesn\'t exist', function () {
var origLogFunction = console.log,
origWarnFunction = console.warn,
logs = [],
warnings = [];
try {
console.log = function (string) {
logs.push(string);
};
console.warn = function (string) {
warnings.push(string);
};
console.timeLog('mytimer'); // should spit out a warning that label doesn't exist, but not log the timer
warnings.length.should.eql(1);
warnings[0].should.eql('Label "mytimer" does not exist');
logs.length.should.eql(0);
} finally {
console.log = origLogFunction;
console.warn = origWarnFunction;
}
});
});
});