Skip to content

Commit 019d571

Browse files
Add 601.pdf-generator benchmark and its data
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 6d7b456 commit 019d571

File tree

6 files changed

+139
-1
lines changed

6 files changed

+139
-1
lines changed

benchmarks-data

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"timeout": 60,
3+
"memory": 256,
4+
"languages": ["nodejs"]
5+
}
6+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import glob
3+
4+
def buckets_count():
5+
return (1, 1) # One input bucket, one output bucket
6+
7+
def generate_input(data_dir, size, benchmarks_bucket, input_paths, output_paths, upload_func):
8+
# The HTML file and the images directory
9+
input_file_path = os.path.join(data_dir, 'template', 'demo.html')
10+
images_dir = os.path.join(data_dir, 'template', 'images') # Directory path
11+
12+
# Initialize input_config with 'object' and 'bucket' fields
13+
input_config = {'object': {}, 'bucket': {}}
14+
15+
# Upload the HTML file to the input bucket
16+
upload_func(0, "demo.html", input_file_path)
17+
18+
# Prepare the bucket configuration
19+
input_config['bucket']['bucket'] = benchmarks_bucket
20+
input_config['bucket']['input'] = input_paths[0]
21+
input_config['bucket']['output'] = output_paths[0]
22+
23+
# Upload each image in the images directory to the input bucket
24+
for file in glob.glob(os.path.join(images_dir, '*.png')):
25+
img = os.path.relpath(file, data_dir)
26+
upload_func(0, img, file)
27+
28+
# Store the list of image file configurations in 'object'
29+
input_config['object']['key'] = "images/"
30+
input_config['object']['input_file'] = 'demo.html'
31+
32+
return input_config
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const puppeteer = require('puppeteer-core');
2+
const path = require('path');
3+
const fs = require('fs');
4+
const { PassThrough } = require('stream');
5+
const storage = require('./storage');
6+
7+
let storage_handler = new storage.storage();
8+
9+
const browserPath = path.join(__dirname, 'chromium/chrome-linux64/chrome');
10+
11+
12+
exports.handler = async function(event) {
13+
const bucket = event.bucket.bucket;
14+
const input_prefix = event.bucket.input;
15+
const output_prefix = event.bucket.output;
16+
const input_file = event.object.input_file;
17+
18+
// Create a read stream for the input HTML file
19+
let readStreamPromise = storage_handler.downloadStream(bucket, path.join(input_prefix, input_file));
20+
21+
// Create a PassThrough stream to pipe the HTML content into Puppeteer
22+
const htmlStream = new PassThrough();
23+
24+
// Create a write stream for the output PDF file
25+
let [writeStream, promise, uploadName] = storage_handler.uploadStream(bucket, path.join(output_prefix, 'output.pdf'));
26+
27+
try {
28+
// Download the HTML file from storage
29+
const inputStream = await readStreamPromise;
30+
inputStream.pipe(htmlStream);
31+
32+
// Launch Puppeteer and generate the PDF
33+
const browser = await puppeteer.launch({ executablePath: browserPath });
34+
const page = await browser.newPage();
35+
await page.setContent(await streamToString(htmlStream), { waitUntil: 'networkidle0' });
36+
const pdfBuffer = await page.pdf({ format: 'A4' });
37+
38+
// Close Puppeteer
39+
await browser.close();
40+
41+
// Pipe the PDF buffer into the write stream
42+
writeStream.write(pdfBuffer);
43+
writeStream.end();
44+
45+
// Wait for upload to complete
46+
await promise;
47+
48+
return { bucket: output_prefix, key: uploadName };
49+
} catch (error) {
50+
console.error('Error generating PDF:', error);
51+
throw error;
52+
}
53+
};
54+
55+
// Utility function to convert a stream to a string
56+
function streamToString(stream) {
57+
return new Promise((resolve, reject) => {
58+
const chunks = [];
59+
stream.on('data', chunk => chunks.push(chunk));
60+
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
61+
stream.on('error', reject);
62+
});
63+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
DIR=$1
4+
VERBOSE=$2
5+
6+
CHROMIUM_URL="https://storage.googleapis.com/chrome-for-testing-public/127.0.6533.88/linux64/chrome-linux64.zip"
7+
8+
# Define the script directory and the download path
9+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
10+
DOWNLOAD_DIR="${DIR}/chromium"
11+
12+
# Create the target directory if it doesn't exist
13+
mkdir -p "$DOWNLOAD_DIR"
14+
15+
# Download Chromium
16+
curl -o "${DOWNLOAD_DIR}/chrome-linux.zip" "$CHROMIUM_URL"
17+
18+
# Extract the downloaded zip file
19+
unzip -q "${DOWNLOAD_DIR}/chrome-linux.zip" -d "$DOWNLOAD_DIR"
20+
21+
# Clean up the downloaded zip file
22+
rm "${DOWNLOAD_DIR}/chrome-linux.zip"
23+
24+
# Move the extracted files to the final directory
25+
mv "${DOWNLOAD_DIR}/chrome-linux"/* "${DOWNLOAD_DIR}/"
26+
27+
# Remove the empty directory
28+
rmdir "${DOWNLOAD_DIR}/chrome-linux"
29+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "pdf-generator",
3+
"version": "1.0.0",
4+
"description": "PDF Generator Benchmark using Puppeteer",
5+
"dependencies": {
6+
"puppeteer-core": "^22.15.0"
7+
}
8+
}

0 commit comments

Comments
 (0)