-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: The BatchProcessor provides functions to enable batch sending Conversions API event requests. Reviewed By: josejia Differential Revision: D23771748 fbshipit-source-id: 4164f3fd
- Loading branch information
1 parent
c2a0d7e
commit 8bf5037
Showing
8 changed files
with
272 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* @flow | ||
*/ | ||
|
||
import ServerEvent from './server-event'; | ||
import EventRequest from './event-request'; | ||
import EventResponse from './event-response'; | ||
|
||
export default class BatchProcessor { | ||
_batch_size: number | ||
_concurrent_requests: number | ||
|
||
constructor(batch_size: number, concurrent_requests: number) { | ||
this._batch_size = batch_size; | ||
this._concurrent_requests = concurrent_requests; | ||
} | ||
|
||
*processEventRequestsGenerator(event_requests: Array<EventRequest>): Generator<Array<Promise<EventResponse>>,void,EventRequest> { | ||
let start = 0; | ||
let end = this._concurrent_requests; | ||
let requests = event_requests.slice(start, end); | ||
while (requests.length > 0) { | ||
yield requests.map(request => request.execute()); | ||
start = end; | ||
end += this._concurrent_requests; | ||
requests = event_requests.slice(start, end); | ||
} | ||
return; | ||
} | ||
|
||
async processEventRequests(event_requests: Array<EventRequest>) { | ||
const generator = this.processEventRequestsGenerator(event_requests); | ||
while (true) { | ||
const batch = generator.next().value; | ||
if (!batch || batch.length === 0) { | ||
generator.return(); | ||
return; | ||
} | ||
await Promise.all(batch); | ||
} | ||
} | ||
|
||
*processEventsGenerator(event_request_to_clone: EventRequest, all_events: Array<ServerEvent>): Generator<Array<Promise<EventResponse>>,void,EventRequest> { | ||
let index = 0; | ||
while (index < all_events.length) { | ||
let event_requests = []; | ||
while (index < all_events.length && event_requests.length < this._concurrent_requests) { | ||
const event_request = event_request_to_clone.cloneWithoutEvents(); | ||
const events = all_events.slice(index, index + this._batch_size); | ||
event_request.setEvents(events); | ||
event_requests.push(event_request); | ||
index += this._batch_size; | ||
} | ||
yield event_requests.map(request => request.execute()); | ||
} | ||
return; | ||
} | ||
|
||
async processEvents(event_request_to_clone: EventRequest, all_events: Array<ServerEvent>) { | ||
const generator = this.processEventsGenerator(event_request_to_clone, all_events); | ||
while (true) { | ||
const batch = generator.next().value; | ||
if (!batch || batch.length === 0) { | ||
generator.return(); | ||
return; | ||
} | ||
await Promise.all(batch); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
const {BatchProcessor, Content, CustomData, DeliveryCategory} = require('facebook-nodejs-business-sdk'); | ||
const {describe} = require('mocha'); | ||
const {expect} = require('chai'); | ||
|
||
|
||
describe('BatchProcessor', function() { | ||
it('processEventRequests processes all event requests', async function() { | ||
const event_requests = getEventRequests(5); | ||
const batchProcessor = new BatchProcessor(2, 2); | ||
await batchProcessor.processEventRequests(event_requests); | ||
|
||
event_requests.forEach(event_request => expect(event_request.called_execute_count).to.equal(1)); | ||
}); | ||
|
||
it('processEventRequestsGenerator returns event request promises in batches', async function() { | ||
const event_requests = getEventRequests(5); | ||
const batchProcessor = new BatchProcessor(2, 2); | ||
let iterations = 0; | ||
let promises = []; | ||
const generator = batchProcessor.processEventRequestsGenerator(event_requests); | ||
let batch; | ||
while(true) { | ||
batch = generator.next().value; | ||
if (!batch || batch.length === 0) { | ||
generator.return(); | ||
break; | ||
} | ||
promises.push(batch); | ||
iterations += 1; | ||
} | ||
await Promise.all(promises); | ||
|
||
event_requests.forEach(event_request => expect(event_request.called_execute_count).to.equal(1)); | ||
expect(iterations).to.equal(3); | ||
}); | ||
|
||
it('processEventsGenerator returns event request promises in batches', async function() { | ||
const event_request = new EventRequestMock(); | ||
const events = getEvents(19); | ||
const batchProcessor = new BatchProcessor(2, 2); | ||
let iterations = 0; | ||
let promises = []; | ||
const generator = batchProcessor.processEventsGenerator(event_request, events); | ||
let batch; | ||
while(true) { | ||
batch = generator.next().value; | ||
if (!batch || batch.length === 0) { | ||
generator.return(); | ||
break; | ||
} | ||
promises.push(batch); | ||
iterations += 1; | ||
} | ||
await Promise.all(promises); | ||
|
||
expect(iterations).to.equal(5); | ||
}); | ||
|
||
it('processEvents processes all events', async function() { | ||
const event_request = new EventRequestMock(); | ||
const events = getEvents(11); | ||
const batchProcessor = new BatchProcessor(2, 3); | ||
await batchProcessor.processEvents(event_request, events); | ||
|
||
expect(event_request.cloned_event_requests.length).to.equal(6); | ||
event_request.cloned_event_requests.forEach(event_request => { | ||
expect(event_request.called_execute_count).to.equal(1) | ||
}); | ||
const event_batches = event_request.cloned_event_requests.map(request => request.set_events); | ||
expect(event_batches).to.deep.equal([ | ||
[events.slice(0, 2)], | ||
[events.slice(2, 4)], | ||
[events.slice(4, 6)], | ||
[events.slice(6, 8)], | ||
[events.slice(8, 10)], | ||
[events.slice(10, 12)], | ||
]); | ||
}); | ||
}); | ||
|
||
|
||
// Test helpers | ||
class EventRequestMock { | ||
constructor() { | ||
this.called_execute_count = 0; | ||
this.set_events = [] | ||
this.cloned_event_requests = [] | ||
} | ||
|
||
execute() { | ||
return new Promise((resolve, reject) => { | ||
this.called_execute_count += 1; | ||
return resolve(); | ||
}); | ||
} | ||
|
||
cloneWithoutEvents() { | ||
const cloned_event_request = new EventRequestMock(); | ||
this.cloned_event_requests.push(cloned_event_request); | ||
return cloned_event_request; | ||
} | ||
|
||
setEvents(events) { | ||
this.set_events.push(events); | ||
} | ||
} | ||
|
||
class EventMock { | ||
constructor(num) { | ||
this.num = num; | ||
} | ||
} | ||
|
||
function getEventRequests(num) { | ||
let events = []; | ||
for (let i = 0 ; i < num; i++) { | ||
events.push(new EventRequestMock()); | ||
} | ||
return events; | ||
} | ||
|
||
function getEvents(num) { | ||
let events = []; | ||
for (let i = 0 ; i < num; i++) { | ||
events.push(new EventMock(i)); | ||
} | ||
return events; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters