-
Notifications
You must be signed in to change notification settings - Fork 100
Java benchmarks support #223
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
Changes from all commits
7776701
13aa9ec
b026577
d478dc1
d36481c
a7055e1
cdd1763
e9b72b2
8d2e015
8dcddef
b6c8bb7
3032978
32235f5
7f8eb2f
67bb80c
19c572d
ab2bc2d
37c37ae
601903f
2c59db1
726e07a
4f88a8d
139cf29
b1a0a0f
5625faf
f784af3
e7fad85
f1796c1
82d04fa
a66fda7
8ed60e1
528a1d9
ebca340
2974d2d
21849d3
7843b6d
14ad548
106a016
ca127b1
650a767
c27c80e
9f6dbcf
f5e2ee0
e616406
5fc5c86
4d9e184
6e5ca67
b10510d
24c6bf3
4114db1
cbd5fec
a879d3a
ca27dec
b757773
da68363
ca4dc29
1f6462d
d696934
0ddb6c2
255f452
17ba563
40962a2
cfd7749
51d24f1
b588358
b5d62bb
52287f7
4c9a83f
eb9c88d
87a680c
8f6fea4
e4f7c50
3ae88eb
a580eae
a25d079
6cdb19d
499153f
20bb209
d41785e
658d4ea
b72edb3
611a11e
87fc63b
c071f75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,5 +188,7 @@ cache | |
| .idea | ||
| *.iml | ||
|
|
||
| # Visual Studio Code files | ||
| .vscode/ | ||
| # MacOS Finder | ||
| **/.DS_Store | ||
| **/.DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "timeout": 120, | ||
| "memory": 128, | ||
| "languages": ["python", "nodejs", "cpp"], | ||
| "languages": ["python", "nodejs", "java", "cpp"], | ||
| "modules": [] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>function</groupId> | ||
| <artifactId>benchmark</artifactId> | ||
| <version>1.0</version> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <java.version>${env.JAVA_VERSION}</java.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <!-- PLATFORM_DEPENDENCIES --> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <finalName>function</finalName> | ||
| <plugins> | ||
| <!-- Compiler Plugin using environment-sourced version --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.8.1</version> | ||
| <configuration> | ||
| <source>${java.version}</source> | ||
| <target>${java.version}</target> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| <!-- Shade plugin to build fat JAR --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-shade-plugin</artifactId> | ||
| <version>3.2.4</version> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals><goal>shade</goal></goals> | ||
| <configuration> | ||
| <filters> | ||
| <filter> | ||
| <artifact>*:*</artifact> | ||
| <excludes> | ||
| <exclude>module-info.class</exclude> | ||
| <exclude>META-INF/*.SF</exclude> | ||
| <exclude>META-INF/*.DSA</exclude> | ||
| <exclude>META-INF/*.RSA</exclude> | ||
| </excludes> | ||
| </filter> | ||
| </filters> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package function; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class Function { | ||
|
|
||
| public Map<String, Object> handler(Map<String, Object> event) { | ||
| double sleepSeconds = parseSeconds(event.get("sleep")); | ||
| try { | ||
| Thread.sleep((long) (sleepSeconds * 1000)); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| Map<String, Object> result = new HashMap<>(); | ||
| result.put("result", sleepSeconds); | ||
| return result; | ||
| } | ||
|
|
||
| private double parseSeconds(Object value) { | ||
| if (value instanceof Number) { | ||
| return ((Number) value).doubleValue(); | ||
| } | ||
| if (value instanceof String) { | ||
| try { | ||
| return Double.parseDouble((String) value); | ||
| } catch (NumberFormatException ignored) { | ||
| return 0; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "timeout": 10, | ||
| "memory": 128, | ||
| "languages": ["python", "nodejs"], | ||
| "languages": ["python", "nodejs", "java"], | ||
| "modules": [] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/bin/bash | ||
|
|
||
| OUTPUT_DIR=$1 | ||
|
|
||
| # Copy templates directory to the output directory | ||
| cp -r templates "$OUTPUT_DIR/" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>function</groupId> | ||
| <artifactId>benchmark</artifactId> | ||
| <version>1.0</version> | ||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <java.version>${env.JAVA_VERSION}</java.version> | ||
| </properties> | ||
| <dependencies> | ||
| <!-- Mustache templating engine --> | ||
| <dependency> | ||
| <groupId>com.github.spullara.mustache.java</groupId> | ||
| <artifactId>compiler</artifactId> | ||
| <version>0.9.10</version> | ||
| </dependency> | ||
| <!-- PLATFORM_DEPENDENCIES --> | ||
| </dependencies> | ||
| <build> | ||
| <!-- Force rebuild for Architecture fix --> | ||
| <finalName>function</finalName> | ||
| <resources> | ||
| <resource> | ||
| <directory>${project.basedir}/templates</directory> | ||
| <targetPath>templates</targetPath> | ||
| <includes> | ||
| <include>**/*.html</include> | ||
| </includes> | ||
| </resource> | ||
| </resources> | ||
| <plugins> | ||
| <!-- Compiler Plugin using environment-sourced version --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.8.1</version> | ||
| <configuration> | ||
| <source>${java.version}</source> | ||
| <target>${java.version}</target> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| <!-- Shade plugin to build fat JAR --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-shade-plugin</artifactId> | ||
| <version>3.2.4</version> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>shade</goal> | ||
| </goals> | ||
| <configuration> | ||
| <createDependencyReducedPom>false</createDependencyReducedPom> | ||
| <filters> | ||
| <filter> | ||
| <artifact>*:*</artifact> | ||
| <excludes> | ||
| <exclude>module-info.class</exclude> | ||
| <exclude>META-INF/*.SF</exclude> | ||
| <exclude>META-INF/*.RSA</exclude> | ||
| <exclude>META-INF/*.DSA</exclude> | ||
| <exclude>module-info.class</exclude> | ||
| <exclude>META-INF/versions/*/module-info.class</exclude> | ||
| <exclude>META-INF/versions/**/module-info.class</exclude> | ||
| </excludes> | ||
| </filter> | ||
| </filters> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package function; | ||
|
|
||
| import com.github.mustachejava.DefaultMustacheFactory; | ||
| import com.github.mustachejava.Mustache; | ||
| import com.github.mustachejava.MustacheFactory; | ||
|
|
||
| import java.io.*; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.*; | ||
|
|
||
| public class Function { | ||
|
|
||
| private static final DateTimeFormatter DATE_FORMATTER = | ||
| DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
|
|
||
| public Map<String, Object> handler(Map<String, Object> event) { | ||
| try { | ||
| // Get input parameters | ||
| String username = (String) event.getOrDefault("username", "Guest"); | ||
| int randomLen = parseRandomLen(event.get("random_len")); | ||
|
|
||
| // Generate random numbers | ||
| List<Integer> randomNumbers = generateRandomNumbers(randomLen); | ||
|
|
||
| // Get current time | ||
| String currentTime = LocalDateTime.now().format(DATE_FORMATTER); | ||
|
|
||
| // Prepare template data | ||
| Map<String, Object> templateData = new HashMap<>(); | ||
| templateData.put("username", username); | ||
| templateData.put("cur_time", currentTime); | ||
| templateData.put("random_numbers", randomNumbers); | ||
|
|
||
| // Render HTML | ||
| String html = renderTemplate(templateData); | ||
|
|
||
| // Return result | ||
| Map<String, Object> result = new HashMap<>(); | ||
| result.put("result", html); | ||
| return result; | ||
|
|
||
| } catch (Exception e) { | ||
| // Return error as result to avoid crashing | ||
| Map<String, Object> result = new HashMap<>(); | ||
| StringWriter sw = new StringWriter(); | ||
| PrintWriter pw = new PrintWriter(sw); | ||
| e.printStackTrace(pw); | ||
| result.put("result", "<html><body><h1>Error</h1><pre>" + | ||
| sw.toString() + "</pre></body></html>"); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| private int parseRandomLen(Object value) { | ||
| if (value instanceof Number) { | ||
| return ((Number) value).intValue(); | ||
| } | ||
| if (value instanceof String) { | ||
| try { | ||
| return Integer.parseInt((String) value); | ||
| } catch (NumberFormatException e) { | ||
| return 10; // default | ||
| } | ||
| } | ||
| return 10; // default | ||
| } | ||
|
|
||
| private List<Integer> generateRandomNumbers(int count) { | ||
| Random random = new Random(); | ||
| List<Integer> numbers = new ArrayList<>(count); | ||
| for (int i = 0; i < count; i++) { | ||
| numbers.add(random.nextInt(1000000)); | ||
|
Comment on lines
+55
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bound Line 57/Line 61 can return negative or very large values, and Line 71 allocates directly from that count. Add min/max bounds before list allocation. Proposed fix+ private static final int DEFAULT_RANDOM_LEN = 10;
+ private static final int MAX_RANDOM_LEN = 10_000;
@@
private int parseRandomLen(Object value) {
+ int parsed = DEFAULT_RANDOM_LEN;
if (value instanceof Number) {
- return ((Number) value).intValue();
+ parsed = ((Number) value).intValue();
+ } else if (value instanceof String) {
+ try {
+ parsed = Integer.parseInt((String) value);
+ } catch (NumberFormatException e) {
+ parsed = DEFAULT_RANDOM_LEN;
+ }
}
- if (value instanceof String) {
- try {
- return Integer.parseInt((String) value);
- } catch (NumberFormatException e) {
- return 10; // default
- }
- }
- return 10; // default
+ if (parsed < 0) return 0;
+ return Math.min(parsed, MAX_RANDOM_LEN);
}🤖 Prompt for AI Agents |
||
| } | ||
| return numbers; | ||
| } | ||
|
|
||
| private String renderTemplate(Map<String, Object> data) throws Exception { | ||
| // Try to load template from classpath | ||
| InputStream templateStream = getClass().getClassLoader() | ||
| .getResourceAsStream("templates/template.html"); | ||
|
|
||
| if (templateStream == null) { | ||
| throw new IOException("Template not found in classpath"); | ||
| } | ||
|
|
||
| // Create Mustache factory and compile template | ||
| MustacheFactory mf = new DefaultMustacheFactory(); | ||
| Mustache mustache; | ||
|
|
||
| try (InputStreamReader reader = new InputStreamReader(templateStream)) { | ||
| mustache = mf.compile(reader, "template"); | ||
| } | ||
|
|
||
| // Render template | ||
| StringWriter writer = new StringWriter(); | ||
| mustache.execute(writer, data).flush(); | ||
| return writer.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||
| <!DOCTYPE html> | ||||||
| <html> | ||||||
| <head> | ||||||
| <title>Randomly generated data.</title> | ||||||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||
| <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" media="screen"> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use HTTPS for external CDN resources. The Bootstrap CSS is loaded over HTTP, which can cause mixed-content warnings and is vulnerable to man-in-the-middle attacks. Use HTTPS instead. 🔒 Suggested fix- <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" media="screen">
+ <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" media="screen">📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| <style type="text/css"> | ||||||
| .container { | ||||||
| max-width: 500px; | ||||||
| padding-top: 100px; | ||||||
| } | ||||||
| </style> | ||||||
| </head> | ||||||
| <body> | ||||||
| <div class="container"> | ||||||
| <p>Welcome {{username}}!</p> | ||||||
| <p>Data generated at: {{cur_time}}!</p> | ||||||
| <p>Requested random numbers:</p> | ||||||
| <ul> | ||||||
| {{#random_numbers}} | ||||||
| <li>{{.}}</li> | ||||||
| {{/random_numbers}} | ||||||
| </ul> | ||||||
| </div> | ||||||
| </body> | ||||||
| </html> | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not return stack traces to clients.
Line 48-Line 50 exposes internal exception details in the HTTP payload. Return a generic error message and log details server-side instead.
Proposed fix
} catch (Exception e) { - // Return error as result to avoid crashing Map<String, Object> result = new HashMap<>(); - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - result.put("result", "<html><body><h1>Error</h1><pre>" + - sw.toString() + "</pre></body></html>"); + result.put("result", "<html><body><h1>Error</h1><p>Internal error</p></body></html>"); return result; }📝 Committable suggestion
🤖 Prompt for AI Agents