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

Dev tool profiler #7249

Merged
merged 6 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
163 changes: 163 additions & 0 deletions java/client/src/org/openqa/selenium/devtools/profiler/Profiler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.devtools.profiler;

import static org.openqa.selenium.devtools.ConverterFunctions.map;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.reflect.TypeToken;
import java.util.List;
import java.util.Optional;
import org.openqa.selenium.Beta;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.profiler.model.ConsoleProfileFinished;
import org.openqa.selenium.devtools.profiler.model.ConsoleProfileStarted;
import org.openqa.selenium.devtools.profiler.model.Profile;
import org.openqa.selenium.devtools.profiler.model.ScriptCoverage;
import org.openqa.selenium.devtools.profiler.model.ScriptTypeProfile;

public class Profiler {

/**
* Disable Profiling
*/
public static Command<Void> disable() {
return new Command<>("Profiler.disable", ImmutableMap.of());
}

/**
* Enable Profiling
*/
public static Command<Void> enable() {
return new Command<>("Profiler.enable", ImmutableMap.of());
}

/**
* start Profiling process
*/
public static Command<Void> start() {
return new Command<>("Profiler.start", ImmutableMap.of());
}

/**
* stop Profiling process
*
* @return {@link Profile} with sampled values
*/
public static Command<Profile> stop() {
return new Command<>("Profiler.stop", ImmutableMap.of(), map("profile", Profile.class));
}

/**
* Collect coverage data for the current isolate. The coverage data may be incomplete due to garbage collection.
*
* @return Array {@link ScriptCoverage}
dratler marked this conversation as resolved.
Show resolved Hide resolved
*/
public static Command<List<ScriptCoverage>> getBestEffortCoverage() {
return new Command<>(
"Profiler.getBestEffortCoverage", ImmutableMap.of(), map("result", new TypeToken<List<ScriptCoverage>>() {
}.getType()));
}

/**
* Changes CPU profiler sampling interval. Must be called before CPU profiles recording started.
*
* @param interval - New sampling interval in microseconds.
dratler marked this conversation as resolved.
Show resolved Hide resolved
*/
public static Command<Void> setSamplingInterval(int interval) {
return new Command<>("Profiler.setSamplingInterval", ImmutableMap.of("interval", interval));
}

/**
* Enable precise code coverage. Coverage data for JavaScript executed before enabling precise code coverage may be
* incomplete. Enabling prevents running optimized code and resets execution counters.
*
* @param callCount - Collect accurate call counts beyond simple 'covered' or 'not covered'.
* @param detailed - Collect block-based coverage.
*/
public static Command<Void> startPreciseCoverage(
Optional<Boolean> callCount, Optional<Boolean> detailed) {
Builder<String, Object> mapBuilder = ImmutableMap.builder();
callCount.ifPresent(value -> mapBuilder.put("callCount", value));
detailed.ifPresent(value -> mapBuilder.put("detailed", value));
return new Command<>("Profiler.startPreciseCoverage", mapBuilder.build());
}

/**
* Enable type profile
*/
@Beta
public static Command<Void> startTypeProfile() {
return new Command<>("Profiler.startTypeProfile", ImmutableMap.of());
}

/**
* Disable precise code coverage. Disabling releases unnecessary execution count records and allows executing
* optimized code.
*/
public static Command<Void> stopPreciseCoverage() {
return new Command<Void>("Profiler.stopPreciseCoverage", ImmutableMap.of());
}

/**
* Disable type profile. Disabling releases type profile data collected so far.EXPERIMENTAL
*/
@Beta
public static Command<Void> stopTypeProfile() {
return new Command<>("Profiler.stopTypeProfile", ImmutableMap.of());
}

/**
* Collect coverage data for the current isolate, and resets execution counters. Precise code coverage needs to have
* started.
*/
public static Command<List<ScriptCoverage>> takePreciseCoverage() {
return new Command<>(
"Profiler.takePreciseCoverage",
ImmutableMap.of(),
map("result", new TypeToken<List<ScriptCoverage>>() {
}.getType()));
}

/**
* Collect type profile.EXPERIMENTAL
*
* @return - Type profile for all scripts since startTypeProfile() was turned on.
*/
@Beta
public static Command<List<ScriptCoverage>> takeTypeProfile() {
return new Command<>(
"Profiler.takeTypeProfile",
ImmutableMap.of(),
map("result", new TypeToken<List<ScriptTypeProfile>>() {
}.getType()));
}

public static Event<ConsoleProfileFinished> consoleProfileFinished() {
return new Event<>("Profiler.consoleProfileFinished", map("id", ConsoleProfileFinished.class));
}

/**
* Sent when new profile recording is started using console.profile() call.
*/
public static Event<ConsoleProfileStarted> consoleProfileStarted() {
return new Event<>("Profiler.consoleProfileStarted", map("id", ConsoleProfileStarted.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.devtools.profiler.model;

import java.util.Objects;
import org.openqa.selenium.json.JsonInput;

public class ConsoleProfileFinished {

private String id;
dratler marked this conversation as resolved.
Show resolved Hide resolved
/**
* Location of console.profileEnd().
*/
private Location location;

private Profile profile;

/**
* Profile title passed as an argument to console.profile().
*
* <p>Optional
*/
private String title;

public ConsoleProfileFinished(String id, Location location, Profile profile, String title) {
dratler marked this conversation as resolved.
Show resolved Hide resolved
this.setId(id);
this.setLocation(location);
this.setProfile(profile);
this.setTitle(title);
}

public static ConsoleProfileFinished fromJson(JsonInput input) {
dratler marked this conversation as resolved.
Show resolved Hide resolved
String id = input.nextString();
Location location = null;
Profile profile = null;
String title = null;
while (input.hasNext()) {
switch (input.nextName()) {
case "location":
location = Location.fronJson(input);
break;
case "profile":
profile = Profile.fromJson(input);
break;
case "title":
title = input.nextString();
break;
default:
input.skipValue();
break;
}
}
return new ConsoleProfileFinished(id, location, profile, title);
}

public String getId() {
return id;
}

public void setId(String id) {
dratler marked this conversation as resolved.
Show resolved Hide resolved
Objects.requireNonNull(id, "id is require");
this.id = id;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
Objects.requireNonNull(location, "location is require");
this.location = location;
}

public Profile getProfile() {
return profile;
}

public void setProfile(Profile profile) {
Objects.requireNonNull(profile, "profile is require");
this.profile = profile;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.devtools.profiler.model;

import java.util.Objects;
import org.openqa.selenium.json.JsonInput;

public class ConsoleProfileStarted {

private String id;
/**
* Location of console.profileEnd().
*/
private Location location;
/**
* Profile title passed as an argument to console.profile().
*
* <p>Optional
*/
private String title;

public ConsoleProfileStarted(String id, Location location, String title) {
this.setId(id);
this.setLocation(location);
this.setTitle(title);
}

public static ConsoleProfileStarted fromJson(JsonInput input) {
String id = input.nextString();
Location location = null;
String title = null;
while (input.hasNext()) {
switch (input.nextName()) {
case "location":
location = Location.fronJson(input);
break;
case "title":
title = input.nextString();
break;
default:
input.skipValue();
break;
}
}
return new ConsoleProfileStarted(id, location, title);
}

public String getId() {
return id;
}

public void setId(String id) {
Objects.requireNonNull(id, "id is require");
this.id = id;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
Objects.requireNonNull(location, "location is require");
this.location = location;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Loading