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

added benchmarks synthesis #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions benchmark_generator/benchmark_generator/code_composer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import os
import uuid

def load_benchmark_code(benchmark_name, language="python"):
current_dir = os.getcwd()
path_to_code = os.path.join(current_dir, benchmark_name, language, "function.py" if language == "python" else "function.js")

if os.path.exists(path_to_code):
try:
with open(path_to_code, "r") as source_file:
source_code = source_file.read()
[_, after_test] = source_code.split("#test")
[_, after_import] = after_test.split("#import")
[import_part, after_function] = after_import.split("#function")
[function_part, run_part] = after_function.split("#run")

return {
"import": import_part,
"function": function_part,
"run": run_part
}
except Exception as e:
print(f"Error loading code from {path_to_code}: {e}")

print(f"Path {path_to_code} does not exist")
return {
"import": "",
"function": "",
"run": ""
}

def intend(body):
return "\n".join(["\t" + line for line in body.splitlines()])

def generate_huge_dict(number_of_elements):
return {
str(uuid.uuid1()) + "-" + str(i): str(uuid.uuid1()) for i in range(number_of_elements)
}

def generate_python_handler(config, code_maps):
code = "\ndef handler(event):\n"
handler_function = "result = {}\n"

for number, (benchmark_name, benchmark_config) in enumerate(config):
handler_function += f"\nnumber = {number}\n"
handler_function += f"config = {benchmark_config}\n"
handler_function += code_maps[benchmark_name]["run"]
if benchmark_name == "artificial_code":
number_of_elements = benchmark_config.get("number_of_elements", 0)
handler_function += f"artificial_dict{number} = {generate_huge_dict(number_of_elements)}\n"

handler_function += """\nreturn {'result': result }\n"""

code += intend(handler_function)
return code

def generate_async_nodejs_handler(config, code_maps):
code = "\nexports.handler = async function(event) {\n"
handler_function = """var result = {};\nawait (async () => { return [result, 0] })()"""

for number, (benchmark_name, benchmark_config) in enumerate(config):
handler_function += ".then(async ([result, number]) => {\n"
inner_function = f"var config = {benchmark_config};\n"
inner_function += code_maps[benchmark_name]["run"] + "\n"
inner_function += "return [result, number + 1]\n"
handler_function += intend(inner_function)
handler_function += "\n})\n"
if benchmark_name == "artificial_code":
number_of_elements = benchmark_config.get("number_of_elements", 0)
handler_function += f"var artificial_dict{number} = {generate_huge_dict(number_of_elements)};\n"

handler_function += """\nreturn {'result': result }\n}"""

code += intend(handler_function)
return code

def compose(config, language):
benchmarks_list = {benchmark for (benchmark, _) in config.items()}

code_maps = {
benchmark_name: load_benchmark_code(benchmark_name, language) for benchmark_name in benchmarks_list
}

code = "\n".join(code_map["import"] for code_map in code_maps.values())
code += "\n".join(code_map["function"] for code_map in code_maps.values())

if language == "python":
return code + generate_python_handler(config.items(), code_maps)
elif language == "async_nodejs":
return code + generate_async_nodejs_handler(config.items(), code_maps)
else:
return ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//#test
var result = {};
var config = {
"block_size": 1024 * 1024 * 128
};
var number = 0;
//#import
var fs = require('fs');
var uuid = require('uuid');
var uuidv1 = uuid.v1;
//#function
function generate_data_disc(block_size) {
return Array(block_size + 1).fill('x').join(''); // Generate string of specified block size
}

async function testDisc(block_size) {
try {
var data = generate_data_disc(block_size); // Generate data based on block size
var path = "/tmp/serverless-benchmark-test-file.json";
var t0 = new Date().getTime(); // Get current time in milliseconds
fs.writeFileSync(path, data); // Write data to file
var t1 = new Date().getTime(); // Get current time in milliseconds
await fs.promises.readFile(path); // Read file asynchronously
var t2 = new Date().getTime(); // Get current time in milliseconds

return {
"write_time": t1 - t0,
"read_time": t2 - t1,
"bytes": block_size
};
} catch (error) {
return { "error": error.toString() };
}
}

//#run
var block_size = config.block_size;
testDisc(block_size).then(returnJson => {
result[number] = returnJson;
console.log(result); // Output the result
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "disc",
"version": "1.0.0",
"description": "",
"author": "",
"license": "",
"dependencies": {
"uuid": "8.2.0"
},
"devDependencies": {
}
}

36 changes: 36 additions & 0 deletions benchmark_generator/benchmark_generator/disc/python/function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import numpy as np
import time
import uuid
import os

def test_disc(block_size, file_name):
a = np.ones(int(block_size / 4), dtype=np.dtype("int32")) * 2 # Create array of specified block size
t0 = time.perf_counter() # Start time for write operation
np.save(file_name, a) # Save array to disk
t1 = time.perf_counter() # End time for write operation

t2 = time.perf_counter() # Start time for read operation
loaded_array = np.load(file_name) # Load array from disk
t3 = time.perf_counter() # End time for read operation

write_time = t1 - t0
read_time = t3 - t2
return {
"block_size": block_size,
"write_time": write_time,
"read_time": read_time
}

# Test configuration
config = {
"block_size": 1024 * 1024 * 128 # 128 MB
}

result = {}
number = 0

block_size = config.get("block_size", 1024 * 100) # Default 100 KB
file_name = "/tmp/sebs.npy"

result[str(number)] = test_disc(block_size, file_name)
print(result)
59 changes: 59 additions & 0 deletions benchmark_generator/benchmark_generator/generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import sys
import json
import code_composer
import requirements_composer
import input_composer
import os

def read_config_file(file_path):
try:
with open(file_path, 'r') as config_file:
return json.load(config_file)
except FileNotFoundError:
print(f"Error: File {file_path} not found.")
sys.exit(1)
except json.JSONDecodeError:
print(f"Error: Failed to parse JSON from {file_path}.")
sys.exit(1)

def write_to_file(file_path, content):
try:
with open(file_path, 'w+') as file:
file.write(content)
except Exception as e:
print(f"Error writing to {file_path}: {e}")
sys.exit(1)

def generate_and_write_code(config, language, path_to_benchmark):
if language == "python":
write_to_file(os.path.join(path_to_benchmark, "function.py"), code_composer.compose(config, "python"))
write_to_file(os.path.join(path_to_benchmark, "requirements.txt"), requirements_composer.compose(config, "python"))
elif language == "async_nodejs":
write_to_file(os.path.join(path_to_benchmark, "function.js"), code_composer.compose(config, "async_nodejs"))
write_to_file(os.path.join(path_to_benchmark, "package.json"), requirements_composer.compose(config, "async_nodejs"))

if len(sys.argv) < 2:
print("Missing argument: path to config")
sys.exit(1)

total_config = read_config_file(sys.argv[1])

config = total_config["config"]
language = total_config["language"]

path_to_benchmark_base = "./../benchmarks/600.generated/620.generated"

if language == "python":
path_to_benchmark = os.path.join(path_to_benchmark_base, "python")
if not os.path.exists(path_to_benchmark):
os.makedirs(path_to_benchmark)
elif language == "async_nodejs":
path_to_benchmark = os.path.join(path_to_benchmark_base, "nodejs")
if not os.path.exists(path_to_benchmark):
os.makedirs(path_to_benchmark)

generate_and_write_code(config, language, path_to_benchmark)

# Ensure path_to_benchmark is initialized before calling input_composer.compose
if "path_to_benchmark" in locals():
write_to_file(os.path.join(path_to_benchmark, "../input.py"), input_composer.compose(config))
45 changes: 45 additions & 0 deletions benchmark_generator/benchmark_generator/input_composer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import uuid

def generate_input_dict(benchmark_config):
"""Generate input dictionary based on input_size in benchmark_config."""
input_dict = {}
if "input_size" in benchmark_config:
for _ in range(int(benchmark_config["input_size"])):
input_dict[str(uuid.uuid1())] = 100
return input_dict

def generate_buckets_count_code(benchmarks_list):
"""Generate buckets_count function code."""
if "storage" in benchmarks_list:
return """def buckets_count():
return (0, 1)\n"""
else:
return """def buckets_count():
return (0, 0)\n"""

def generate_generate_input_code(benchmarks_list):
"""Generate generate_input function code."""
if "storage" in benchmarks_list:
return """def generate_input(data_dir, size, input_buckets, output_buckets, upload_func):
input_dict = {'bucket': {}}
input_dict['bucket']['output'] = output_buckets[0]
return input_dict """
else:
return """def generate_input(data_dir, size, input_buckets, output_buckets, upload_func):
return input_dict """

def generate_code(config):
benchmarks_list = {benchmark for (benchmark, _) in config.items()}

input_dict = {}
for benchmark, benchmark_config in config.items():
if benchmark == "function_input":
input_dict = generate_input_dict(benchmark_config)
break

code = ""
code += "input_dict = " + str(input_dict) + "\n"
code += generate_buckets_count_code(benchmarks_list)
code += generate_generate_input_code(benchmarks_list)

return code
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//#test
var config = {
"size_in_bytes": 10485760
};
var result = {};
var number = 0;
//#import
var math = require('mathjs');
//#function
const testMemory = async (size) => {
var t0 = new Date().getTime(); // Get current time in milliseconds
var a = math.ones([Math.floor(size / 8)]); // Ensure size is an integer
var t1 = new Date().getTime(); // Get current time in milliseconds

return {
"time": t1 - t0,
"size_in_bytes": size
};
};
//#run
var array_size_in_bytes = config["size_in_bytes"];
testMemory(array_size_in_bytes).then(returnJson => {
result[number] = returnJson;
console.log(result); // Output the result
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "sleep",
"version": "1.0.0",
"description": "",
"author": "",
"license": "",
"dependencies": {
"mathjs": "7.0.2"
},
"devDependencies": {
}
}
24 changes: 24 additions & 0 deletions benchmark_generator/benchmark_generator/memory/python/function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np
import time

def allocate(size_in_bytes):
t0 = time.perf_counter()
arr = np.ones(int(size_in_bytes/4), dtype=np.dtype("int32"))
t1 = time.perf_counter()

return {
"time": t1 - t0,
"size_in_bytes": size_in_bytes
}

config = {
"size_in_bytes": 1024 * 1024
}

result = {}
number = 0

size_of_allocated_memory = config.get("size_in_bytes", 1024 * 1024) # Default 1 MB
result[str(number)] = allocate(size_of_allocated_memory)

print(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy==1.18.5
Loading