我在Google App Script上测试Gmail服务,我想知道接收电子邮件的速度有多快。所以我开始执行这段代码:
// just a tester
function getInboxMails() {
var execStart = new Date();
var threads = GmailApp.getInboxThreads();
var threadTime = new Date();
logTime(execStart, threadTime, 'Get Thread Collection Time: ');
var arrayThreads = [];
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var emails = getThreadEmails(thread);
arrayThreads.push(emails);
}
var execEnd = new Date();
logTime(execStart, execEnd, "Get Inbox Time: ");
return arrayThreads;
}
function getThreadEmails(thread) {
var execStart = new Date();
var arrEmails = [];
var count = thread.getMessageCount();
var messages = thread.getMessages();
var threadTime = new Date();
logTime(execStart, threadTime, 'ta ' + thread.getId() + ': ');
for (var i = 0; i < count; i++) {
var message = messages[i];
var mail = getEmailData(message);
//arrEmails.push(mail);
}
var execEnd = new Date();
//logTime(execStart, execEnd, 'Thread + data access: ');
//Logger.log('n');
return arrEmails;
}
function getEmailData(message) {
var execStart = new Date();
var mail = {
body : message.getBody(),
to : message.getTo(),
cc : message.getCc(),
bcc : message.getBcc(),
from : message.getFrom(),
date : message.getDate(),
plainBody : message.getPlainBody(),
rawContent : message.getRawContent(),
replyTo : message.getReplyTo(),
subject : message.getSubject(),
mailId : message.getId()
}
var execEnd = new Date();
//Mail Access
logTime(execStart, execEnd, 'ma ' + mail.mailId + ': ');
return mail;
}
function logTime(startDate, endDate, message) {
var timeDiff = endDate - startDate;
var ms = timeDiff % 1000;
timeDiff = Math.floor(timeDiff/1000);
var sec = timeDiff % 60;
Logger.log(message + sec + '.' + ms);
}
结果是:
[14-05-19 10:28:10:334 EEST] Get Thread Collection Time: 0.513
[14-05-19 10:28:10:591 EEST] ta 14608be50b3e2972: 0.256
[14-05-19 10:28:10:924 EEST] ma 14608be50b3e2972: 0.333
[14-05-19 10:28:11:088 EEST] ta 14605c88a55e1d5f: 0.164
[14-05-19 10:28:15:238 EEST] ma 14605c88a55e1d5f: 4.150
[14-05-19 10:28:15:398 EEST] ta 14604dcdc318938f: 0.159
[14-05-19 10:28:15:539 EEST] ma 14604da59c36f292: 0.141
[14-05-19 10:28:15:671 EEST] ma 14604da60bd7f36b: 0.132
[14-05-19 10:28:19:804 EEST] ma 14604da7e8364ae5: 4.133
[14-05-19 10:28:19:954 EEST] ma 14604db33c6f5b07: 0.150
[14-05-19 10:28:20:173 EEST] ma 14604db667e2501b: 0.219
[14-05-19 10:28:20:305 EEST] ma 14604dba5e63713d: 0.132
[14-05-19 10:28:24:436 EEST] ma 14604dc033d71d9f: 4.131
[14-05-19 10:28:24:567 EEST] ma 14604dc6750e659a: 0.130
[14-05-19 10:28:24:698 EEST] ma 14604dc8722693e9: 0.131
[14-05-19 10:28:24:831 EEST] ma 14604dcdc318938f: 0.132
[14-05-19 10:28:30:984 EEST] ta 146004ab2a9c64ca: 6.152
[14-05-19 10:28:31:139 EEST] ma 145ff996cf2f4c13: 0.155
[14-05-19 10:28:31:273 EEST] ma 145ff9f561109a80: 0.134
[14-05-19 10:28:31:423 EEST] ma 146004ab2a9c64ca: 0.150
[14-05-19 10:28:35:723 EEST] ta 1460040acea62d23: 4.300
[14-05-19 10:28:35:861 EEST] ma 1460034634bfe0bc: 0.137
[14-05-19 10:28:35:998 EEST] ma 14600387e9ce077a: 0.137
[14-05-19 10:28:36:134 EEST] ma 146003af12370540: 0.136
[14-05-19 10:28:40:279 EEST] ma 146003c3050f55c4: 4.144
[14-05-19 10:28:40:500 EEST] ma 146003ce0072e0cf: 0.221
[14-05-19 10:28:40:649 EEST] ma 146003d45600fb6c: 0.149
[14-05-19 10:28:40:804 EEST] ma 146003da4c9592ee: 0.155
[14-05-19 10:28:45:031 EEST] ma 14600405e2339b2b: 4.227
[14-05-19 10:28:45:158 EEST] ta 145ff1ec0e9093bc: 0.125
[14-05-19 10:28:45:292 EEST] ma 145ff1ec0e9093bc: 0.134
[14-05-19 10:28:45:420 EEST] ta 145faac214352a74: 0.128
[14-05-19 10:28:49:556 EEST] ma 145faa9301567f73: 4.136
[14-05-19 10:28:49:681 EEST] ma 145faac214352a74: 0.124
[14-05-19 10:28:49:806 EEST] ta 145faa6f9dd8fc04: 0.125
[14-05-19 10:28:49:938 EEST] ma 145faa6f9dd8fc04: 0.132
[14-05-19 10:28:56:131 EEST] ta 145fa9bbe29f0a9f: 6.192
[14-05-19 10:28:56:282 EEST] ma 145fa9bbe29f0a9f: 0.151
[14-05-19 10:28:56:405 EEST] ta 145fa98ea19a70d1: 0.123
[14-05-19 10:28:56:541 EEST] ma 145fa98ea19a70d1: 0.135
[14-05-19 10:29:00:674 EEST] ta 145fa940bac7a200: 4.133
[14-05-19 10:29:00:802 EEST] ma 145fa940bac7a200: 0.128
[14-05-19 10:29:00:930 EEST] ta 145fa940941fa6b7: 0.127
[14-05-19 10:29:01:100 EEST] ma 145fa940941fa6b7: 0.169
[14-05-19 10:29:05:227 EEST] ta 145fa9406e0239b3: 4.127
[14-05-19 10:29:05:368 EEST] ma 145fa9406e0239b3: 0.141
[14-05-19 10:29:05:369 EEST] Get Inbox Time: 55.547
可以看到,数据访问非常稳定,但是每第四次操作(第一次不包括在内)就非常慢。就像慢了4秒(有时是6秒,但我想这也是可以预测的行为)。
谁能告诉我为什么?我怎样才能避免呢?我也有同样的问题。似乎getPlainBody比getBody慢10倍。我有一个脚本需要90秒才能执行。将getPlainBody切换为getBody,现在需要~10秒