How to calculate the time taken to load module in gwt? - gwt

I am using a custom gridview widget and large amount of data(say 1000 rows) assigned to it.I know that it will take more time.And I want to know how much time it exactly taking to load the grid.
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="Custom.css">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>CustomWidget</title>
<script type="text/javascript" language="javascript" src="mywidget/mywidget.nocache.js">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<div id="txt"></div>
</body>
</html>
But not able display time..and its working when executing as single html page.

Another two ways along with #Braj answer
1.Using Speed Tracer you are able to get a better picture of where time is being spent in your application. This includes problems caused by:
Javascript parsing and execution
Layout
CSS style recalculation and selector matching
DOM Event handling
Network resource loading
Timer fires
XMLHttpRequest callbacks
Painting
Fallow this link Speed Tracer and to Crome(click on free) ,works with Crome only
2.You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page
With this we can get how much time each function taken , how many times is called and % of total time
but its works with fire fox only.
Fallow this link FireBug.Click on Console and enable Profile to see the performance of each function.
Sometimes you may get obfuscated function names.To avoid this project>Google>GWT Compile>Select output style as Pretty

Try this one
Steps to follow:
create a hidden div that have a information about current time before downloading nocache.js
Once nocache.js is loaded it will call EntryPoint#onModuleLoad() method of your entry point class.
Compare the time to get the time taken
HTML/JSP:
<body>
<div id="timeinfo" style="visibility: hidden;"></div>
<script type="text/javascript">
var today = new Date();
document.getElementById("timeinfo").innerHTML = today.getTime();
</script>
<script type="text/javascript" src="mywidget/mywidget.nocache.js"></script>
</body>
Entry point class:
public class MyWidget implements EntryPoint {
public void onModuleLoad() {
RootPanel timeinfo = RootPanel.get("timeinfo");
long startTime = Long.valueOf(timeinfo.getElement().getInnerHTML());
long endTime = System.currentTimeMillis();
System.out.println(new Date(startTime));
System.out.println(new Date(endTime));
System.out.println("Total time taken=" + (endTime - startTime) + " ms.");
RootPanel.getBodyElement().removeChild(timeinfo.getElement());
...
}
}
output:
Tue Apr 22 16:55:49 IST 2014
Tue Apr 22 16:56:03 IST 2014
Total time taken=14479 ms.

Open console in your browser. Click on the Network tab. Reload the page. See how long it takes to load each resource.

Related

After a user is redirected to a page in my quick app from a card, how can my app directly take them back to the card?

In my Huawei quick app, when I have opened page A in a quick app, and then taps a card or other media to proceed to page B, tapping the back button in the upper left corner redirects them to page A first. But it is expected to be redirected directly to the card after tapping the back button. How does it occur?
The problem is caused by the default startup mode standard, which the page adopts. In this mode, every new page started by the user will be cached in the page stack, so a page that is opened for multiple times will be cached repeatedly. As a result, the user can only close one page at a time. To solve this problem, you are advised to set the startup mode of page B to clearTask using the dynamic declaration when the user taps a card to go to a quick app. In this case, page A closes when page B opens, meaning only page B exists in the page stack. When the user taps the back button, they will directly exit the quick app.
You can use the following sample code for redirecting a user from a card to a quick app using deep links:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Quick app test</title>
</head>
<body>
Open through hwfastapp.
<br>
<br>
Open through hap.
</body>
</html>
Target page of the quick app to which the card is redirected (There is always only one page based on the number of current page stacks.)
<template>
<div class="container">
<text>___PARAM_LAUNCH_FLAG___=</text>
<text>{{taskflag}}</text>
<text>Number of current page stacks.</text>
<text>{{length}}</text>
</div>
</template>
<style>
.container {
flex-direction: column;
align-content: center;
align-items: center;
justify-content: center;
}
text {
margin-bottom: 50px;
}
</style>
<script>
import router from '#system.router';
export default {
data: {
// The default is the local app internal image
photoUri: '/Common/logo.png',
taskflag:'',
PARAM_LAUNCH_FLAG:'',
length:''
},
onInit() {
this.$page.setTitleBar({ text: 'deepLink' })
var that=this;
that.taskflag=this.PARAM_LAUNCH_FLAG;
// Call the getPages method.
let pages = router.getPages()
// The obtained value is a JSON array. Therefore, the value cannot be displayed directly. You can use the following method to display the value:
console.log("tag", this.printJSONArray(router.getPages()));
that.length= router.getLength();
console.log("pages' length = "+that.length);
},
printJSONArray(array) {
let result = ""
const suffix = ", "
Array.isArray(array) && array.forEach((element, index) => {
result = result + JSON.stringify(element) + (index === array.length-1 ? "" : ", ")
})
return result
}
}
</script>
The page launch mode can be configured in two modes: static declaration in the manifest file and dynamic parameter pass declaration, of which the latter is recommended. The static declaration mode applies to scenarios with fixed settings which cannot be flexibly adjusted.
For more details,you can refer to this Docs and deep link documentation.

Simple play/scala benchmark, rendering a view versus a raw text output comparison

I was just benchmarking a new play/scala application.
When performing a simple text output in my action, I get 60K requests per second.
If I render a view (see below), it drops to 13K per second.
Since views are just functions in scala, I would have expected that the extra overhead of calling a function wouldn't drop the requests per second down so dramatically.
I only ran the benchmark for 10-30 seconds, would it take longer for the jvm to optimize maybe or this is just expected behavour?
def index() = Action { implicit request: Request[AnyContent] =>
Ok("hello")
}
If I actually render a view, the requests per second drops to about 13K.
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}
/app/views/index.scala.html
#()
#main("Welcome to Play") {
<h1>Welcome to Play!</h1>
}
/app/views/main.scala.html
#*
* This template is called from the `index` template. This template
* handles the rendering of the page header and body tags. It takes
* two arguments, a `String` for the title of the page and an `Html`
* object to insert into the body of the page.
*#
#(title: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
<head>
#* Here's where we render the page title `String`. *#
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="#routes.Assets.versioned("images/favicon.png")">
</head>
<body>
#* And here's where we render the `Html` object containing
* the page content. *#
#content
<script src="#routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
</body>
</html>
As I already said in my comment, the view is not a trivial function. It performs string concatenation and it also calls routes.Assets.versioned three times. Profiling session shows, that the view basically only waits on this function:
Drilling down, we learn, that the versioned function always re-reads the file from classpath:
Maybe you can open an issue and ask Play framework creators, whether serving of assets could be optimized better ?
Edit: I profiled two setups. First I modified HomeControllerSpec test:
"render the index page from a new instance of controller" in {
val controller = new HomeController(stubControllerComponents())
val indexAction = controller.index()
val fakeRequest = FakeRequest(GET, "/")
var i = 100000
while (i > 0) {
indexAction.apply(fakeRequest)
i -= 1
}
But this does not rule out, that some components can behave differently in production mode. So I ran sbt stage and started generated application, attached profiler to running JVM and executed 10000 requests to the profiled JVM. Result was identical though.

karma-runner: load scripts (and CSS) via DOM

I want to inject some CSS and JavaScript files via a preprocessor.
In my preprocessor I inject the html template to the body element.
I printed the result out via console.log(document.body) - you can see the result at the bottom. It looks good, but the script is not evaluated.
If I run console.log(window.foobar) in my test, it's undefined.
Actually I don't want to to inject simple scripts, I want to load some files via
<script src="build/app.js"></script>
I need it in every test, so I don't want to refactor every single test for the same code injection, that's the reason why I tried to put it into the html generated by karma.
<body><script> window.foobar = 'miau!';</script>
<!-- The scripts need to be at the end of body, so that some test running frameworks
(Angular Scenario, for example) need the body to be loaded so that it can insert its magic
into it. If it is before body, then it fails to find the body and crashes and burns in an epic
manner. -->
<script type="text/javascript">
// sets window.__karma__ and overrides console and error handling
// Use window.opener if this was opened by someone else - in a new window
if (window.opener) {
window.opener.karma.setupContext(window);
} else {
window.parent.karma.setupContext(window);
}
// All served files with the latest timestamps
window.__karma__.files = {
'/base/node_modules/mocha/mocha.js': '253e2fdce43a4b2eed46eb25139b784adbb5c47f',
'/base/node_modules/karma-mocha/lib/adapter.js': '3664759c75e6f4e496fef20ad115ce8233a0f7b5',
'/base/test/custom-test.js': 'abf5b0b3f4dbb62653c816b264a251c7fc264fb9',
'/base/test/build/build.css': 'df7e943e50164a1fc4b66e0a0c46fc86efdef656',
'/base/test/build/build.js': '9f0a39709e073846c73481453cdee8d37e528856',
'/base/test/build/test.js': '0ccd4711b9c887458f81cf1dedc04c6ed59abe43'
};
</script>
<!-- Dynamically replaced with <script> tags -->
<script type="text/javascript" src="/base/node_modules/mocha/mocha.js?253e2fdce43a4b2eed46eb25139b784adbb5c47f"></script>
<script type="text/javascript" src="/base/node_modules/karma-mocha/lib/adapter.js?3664759c75e6f4e496fef20ad115ce8233a0f7b5"></script>
<script type="text/javascript" src="/base/test/custom-test.js?abf5b0b3f4dbb62653c816b264a251c7fc264fb9"></script></body>
Karma introduces the page scripts/html just like ajax, so it wont execute once the append has finished.
You will need to append the files for each spec. I have a helper for this job:
function appendCSS(path){
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href='base/' + path;
document.body.appendChild(link)
}
function appendScript(path){
var link = document.createElement('script');
link.type = 'javascript';
link.src='base/' + path;
document.body.appendChild(link)
}
function loadAssets(page){
document.body.innerHTML = __html__['_site/' + (page || 'index') + '.html'];
appendCSS('_site/styles/demo.css');
appendCSS('_site/styles/' + page + '.css');
appendScript('_site/scripts/vendor.js');
appendScript('_site/scripts/' + page + '.js');
}
module.exports = {
loadAssets: loadAssets
};
In my spec i then simply call the helper, passing the name of the html page to be tested.
require('../helper').loadAssets('tested-page-name');
As you can see, i use the borwserify plugin, but i hope this helps.

Zero clipboard is not working in gwt

I have been trying to integrate zero clipboard library with gwt code as follows.
test.html
<script type="text/javascript" language="javascript" src="test/test.nocache.js"></script>
<script type="text/javascript" src="ZeroClipboard.js"></script>
<script language="text/javascript">
function initZeroClipboard() {
ZeroClipboard.setMoviePath('ZeroClipboard.swf');
}
</script>
.....................................
//Intial body load instantiating 'Moviepath'
<body onload="initZeroClipboard();">
TestWidget.java
Anchor copy = new Anchor("Copy");
......................
//setting id to refer the movie
copy.getElement().setId("copyId");
glueCopy("Hello World");
...........
//Native method
public static native void glueCopy(String text) /*-{
var clip = new $wnd.ZeroClipboard.Client();
clip.setText(text);
clip.glue('copyId');
}-*/;
But on intial load itself i am getting the following error in IE and FF.
Jetty Server IE error
Jetty server FF error
I have donloaded 'ZeroClipboard.swf' and 'ZeroClipboard.js' files from the following
https://github.com/jonrohan/ZeroClipboard
Can anyone come across this issue, if so suggest me how to get rid of this.

Embed google-plus in GWT

I am trying to embed Google-Plus into my GWT Application. I would like it to be embedded into a HorizontalPanel. I did read +1button developers google. I didn't find any post about this particular problem in stackoverflow. My problem might be that I don't understand how to include the js into a GUI component. I would appreciate an Example of how to add the Google+ code into a Panel.
Here is how to do it:
Documentation:
<!-- Place this tag in your head or just before your close body tag -->
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<!-- Place this tag where you want the +1 button to render -->
<g:plusone></g:plusone>
in GWT:
private void drawPlusOne() {
String s = "<g:plusone href=\"http://urltoplusone.com\"></g:plusone>";
HTML h = new HTML(s);
somePanel.add(h);
// You can insert a script tag this way or via your .gwt.xml
Document doc = Document.get();
ScriptElement script = doc.createScriptElement();
script.setSrc("https://apis.google.com/js/plusone.js");
script.setType("text/javascript");
script.setLang("javascript");
doc.getBody().appendChild(script);
}
I've personally never embedded the +1 button in GWT, but the linked article seems pretty self explanatory.
In the section "A Simple Button", it indicates that the simplest way of implementing GooglePlus integration is to add this:
<script src="https://apis.google.com/js/plusone.js" />
<g:plusone></g:plusone>
First, the <script> tag should be included in your .gwt.xml file.
Then I'd implement the <g:plusone></g:plusone> like this:
public class GPlusOne extends SimplePanel {
public GPlusOne () {
super((Element)Document.get().createElement("g:plusone").cast());
}
}
(Note that this code is untested, but it's based on the simple concept that a SimplePanel can be extended to compile as any HTML element.)
Then you'd use the new GPlusOne element wherever you'd want the button to show.
I found a better way to do it:
Follow this example to have the button work on invocation on a normal html page (you can try one here http://jsfiddle.net/JQAdc/)
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="https://apis.google.com/js/plusone.js">
{"parsetags": "explicit"}
</script>
<script type="text/javascript">
function gPlusBtn(id, params) {
/* window.alert("searching for "+ id +" with params: "+ params) */
paramsObj = eval( '('+params+')' );
gapi.plusone.render(id, paramsObj );
}
// params is here just for a reference to simulate what will come from gwt
params = '{href:"http://1vu.fr", size:"tall"}';
</script>
</head>
<body>
taken from http://jsfiddle.net/JQAdc/
<div id="gplus" />
<button onclick="gPlusBtn('gplus', params)">show!</button>
</body>
</html>
Then you can call a native method to trigger the button display on Activity start (if you're using MVP).
protected native void plusOneButton(String id, String params) /*-{
$wnd.gPlusBtn(id, params);
}-*/;
You can have multiple buttons with different urls, that's why id is left as a parameter.
NOTE: for me the raw HTML works on localhost, but the GWT version. I have to deploy to the server to be able to see the results