Skip to content

Commit e9ace87

Browse files
committed
Compute memory usage via MajorGC log entries
1 parent 97d55f1 commit e9ace87

File tree

4 files changed

+95
-58
lines changed

4 files changed

+95
-58
lines changed

webdriver-java/src/main/java/net/stefankrause/AbstractCPUBench.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
import java.io.IOException;
44
import java.util.*;
55

6-
import com.google.common.collect.HashBasedTable;
7-
import com.google.common.collect.Table;
86
import org.json.*;
97

108
import org.openqa.selenium.*;
119
import org.openqa.selenium.chrome.*;
1210
import org.openqa.selenium.logging.*;
13-
import org.openqa.selenium.remote.*;
1411
import org.openqa.selenium.support.ui.ExpectedConditions;
1512
import org.openqa.selenium.support.ui.WebDriverWait;
1613

17-
public abstract class AbstractCPUBench implements Bench {
14+
public abstract class AbstractCPUBench extends Bench {
1815
abstract void init(WebDriver driver, String url);
1916
abstract void test(WebDriver driver);
2017

@@ -82,46 +79,6 @@ public Double run(ChromeDriver driver, Framework framework, double lastWait) thr
8279

8380
return printLog(driver, true, "aurelia".equals(framework.framework));
8481
}
85-
86-
public String getAsString(JSONObject root, String path) {
87-
return getAsStringRec(root, Arrays.asList(path.split("\\.")));
88-
}
89-
90-
public double getAsLong(JSONObject root, String path) {
91-
Double r = getAsLongRec(root, Arrays.asList(path.split("\\.")));
92-
if (r==null) {
93-
return 0;
94-
} else {
95-
return r.doubleValue();
96-
}
97-
}
98-
99-
100-
public String getAsStringRec(JSONObject root, List<String> path) {
101-
JSONObject obj = root;
102-
if (!root.has(path.get(0)))
103-
return null;
104-
105-
if (path.size()==1) {
106-
return root.getString(path.get(0));
107-
} else {
108-
return getAsStringRec(root.getJSONObject(path.get(0)), path.subList(1, path.size()));
109-
}
110-
}
111-
112-
public Double getAsLongRec(JSONObject root, List<String> path) {
113-
JSONObject obj = root;
114-
if (!root.has(path.get(0)))
115-
return null;
116-
117-
if (path.size()==1) {
118-
return Double.valueOf(root.getDouble(path.get(0)));
119-
} else {
120-
return getAsLongRec(root.getJSONObject(path.get(0)), path.subList(1, path.size()));
121-
}
122-
}
123-
124-
12582

12683
List<PLogEntry> submitPerformanceResult(List<LogEntry> perfLogEntries, boolean print)
12784
throws IOException, JSONException {
@@ -137,8 +94,8 @@ List<PLogEntry> submitPerformanceResult(List<LogEntry> perfLogEntries, boolean p
13794
|| "Paint".equals(name)
13895
|| "TimerFire".equals(name)) {
13996
filtered.add(new PLogEntry(name,
140-
(long)getAsLong(obj, "message.params.ts"),
141-
(long)getAsLong(obj, "message.params.dur"),
97+
(long) getAsDouble(obj, "message.params.ts"),
98+
(long) getAsDouble(obj, "message.params.dur"),
14299
entry.getMessage()));
143100
}
144101
}

webdriver-java/src/main/java/net/stefankrause/AbstractMemoryBench.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package net.stefankrause;
22

3+
import java.io.IOException;
34
import java.util.*;
45

6+
import org.json.JSONException;
7+
import org.json.JSONObject;
58
import org.openqa.selenium.*;
69
import org.openqa.selenium.chrome.*;
710
import org.openqa.selenium.logging.*;
8-
import org.openqa.selenium.remote.*;
911
import org.openqa.selenium.support.ui.ExpectedConditions;
1012
import org.openqa.selenium.support.ui.WebDriverWait;
1113

12-
public abstract class AbstractMemoryBench implements Bench {
14+
public abstract class AbstractMemoryBench extends Bench {
1315
abstract void init(WebDriver driver, String url);
1416
abstract void test(WebDriver driver);
1517

@@ -24,28 +26,32 @@ public Double run(ChromeDriver driver, Framework framework, double lastWait) thr
2426
WebDriverWait wait = new WebDriverWait(driver, 10);
2527
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("run")));
2628

29+
driver.executeScript("window.gc();");
2730
Thread.sleep(2000);
2831

2932
System.out.println(framework.framework + " " + this.getName() + " => run");
3033
this.test(driver);
3134
System.out.println("run " + this.getName());
3235

33-
Thread.sleep(20000);
36+
Thread.sleep(1000 + (int) lastWait);
37+
driver.executeScript("window.gc();");
38+
Double mem = extractMem(driver);
39+
System.out.println("Memory from GC: "+mem);
3440

35-
return snapMemorySize(driver);
41+
return mem; //snapMemorySize(driver);
3642
}
3743

3844
private double snapMemorySize(ChromeDriver driver) {
3945
JavascriptExecutor executor = ((JavascriptExecutor) driver);
40-
46+
4147
Map<String, ?> heapSnapshot = (Map<String, ?>) executor.executeScript(":takeHeapSnapshot");
42-
48+
4349
Map<String, ?> snapshot = (Map<String, ?>) heapSnapshot.get("snapshot");
4450
Map<String, ?> meta = (Map<String, ?>) snapshot.get("meta");
4551
ArrayList<String> node_fields = (ArrayList<String>) meta.get("node_fields");
4652

4753
ArrayList<Long> nodes = (ArrayList<Long>) heapSnapshot.get("nodes");
48-
54+
4955
long self_size = 0;
5056
for(int k = node_fields.indexOf("self_size"), l = nodes.size(), d = node_fields.size(); k < l; k += d) {
5157
self_size += nodes.get(k);
@@ -57,4 +63,35 @@ private double snapMemorySize(ChromeDriver driver) {
5763

5864
return memory;
5965
}
66+
67+
List<Double> submitPerformanceResult(List<LogEntry> perfLogEntries, boolean print)
68+
throws IOException, JSONException {
69+
ArrayList<Double> filtered = new ArrayList<>();
70+
71+
if (print) System.out.println(perfLogEntries.size() + " performance log entries found");
72+
for (LogEntry entry : perfLogEntries) {
73+
JSONObject obj = new JSONObject(entry.getMessage());
74+
String name = getAsString(obj, "message.params.name");
75+
if (print) System.out.println(entry.getMessage());
76+
if ("MajorGC".equals(name) && getAsString(obj, "message.params.args.type")==null) {
77+
filtered.add(Double.valueOf(getAsDouble(obj, "message.params.args.usedHeapSizeAfter")/1024.0/1024.0));
78+
}
79+
}
80+
81+
return filtered;
82+
}
83+
84+
Double extractMem(WebDriver driver) throws Exception {
85+
Logs logs = driver.manage().logs();
86+
ArrayList<LogEntry> perfEntries = new ArrayList<>();
87+
for (String lt : logs.getAvailableLogTypes()) {
88+
List<LogEntry> entries = logs.get(lt).getAll();
89+
if (LogType.PERFORMANCE.equals(lt)) perfEntries.addAll(entries);
90+
System.out.println(entries.size() + " " + lt + " log entries found");
91+
}
92+
List<Double> filtered = submitPerformanceResult(perfEntries, false);
93+
// System.out.println(filtered);
94+
if (filtered.size()==0) return -1.0;
95+
return filtered.get(filtered.size()-1);
96+
}
6097
}

webdriver-java/src/main/java/net/stefankrause/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ public DesiredCapabilities setUp() throws Exception {
549549
ChromeOptions options = new ChromeOptions();
550550
options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
551551
options.setBinary(BINARY);
552+
options.addArguments("--js-flags=--expose-gc");
552553
cap.setCapability(ChromeOptions.CAPABILITY, options);
553554

554555
return cap;
Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
11
package net.stefankrause;
22

3+
import org.json.JSONObject;
34
import org.openqa.selenium.chrome.ChromeDriver;
45

5-
public interface Bench {
6-
Double run(ChromeDriver driver, Framework framework, double lastWait) throws Exception;
7-
String getName();
8-
String getPath();
9-
String getType();
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public abstract class Bench {
10+
public abstract Double run(ChromeDriver driver, Framework framework, double lastWait) throws Exception;
11+
public abstract String getName();
12+
public abstract String getPath();
13+
public abstract String getType();
14+
15+
public String getAsString(JSONObject root, String path) {
16+
return getAsStringRec(root, Arrays.asList(path.split("\\.")));
17+
}
18+
19+
public double getAsDouble(JSONObject root, String path) {
20+
Double r = getAsDoubleRec(root, Arrays.asList(path.split("\\.")));
21+
if (r==null) {
22+
return 0;
23+
} else {
24+
return r.doubleValue();
25+
}
26+
}
27+
28+
29+
public String getAsStringRec(JSONObject root, List<String> path) {
30+
JSONObject obj = root;
31+
if (!root.has(path.get(0)))
32+
return null;
33+
34+
if (path.size()==1) {
35+
return root.getString(path.get(0));
36+
} else {
37+
return getAsStringRec(root.getJSONObject(path.get(0)), path.subList(1, path.size()));
38+
}
39+
}
40+
41+
public Double getAsDoubleRec(JSONObject root, List<String> path) {
42+
JSONObject obj = root;
43+
if (!root.has(path.get(0)))
44+
return null;
45+
46+
if (path.size()==1) {
47+
return Double.valueOf(root.getDouble(path.get(0)));
48+
} else {
49+
return getAsDoubleRec(root.getJSONObject(path.get(0)), path.subList(1, path.size()));
50+
}
51+
}
1052
}

0 commit comments

Comments
 (0)