~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to src/cobweb/Browser/Core/BrowserTimings.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
 
2
 *******************************************************************************
 
3
 *
 
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
5
 *
 
6
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
 
7
 *
 
8
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
 
9
 *
 
10
 * The copyright notice above does not evidence any actual of intended
 
11
 * publication of such source code, and is an unpublished work by create3000.
 
12
 * This material contains CONFIDENTIAL INFORMATION that is the property of
 
13
 * create3000.
 
14
 *
 
15
 * No permission is granted to copy, distribute, or create derivative works from
 
16
 * the contents of this software, in whole or in part, without the prior written
 
17
 * permission of create3000.
 
18
 *
 
19
 * NON-MILITARY USE ONLY
 
20
 *
 
21
 * All create3000 software are effectively free software with a non-military use
 
22
 * restriction. It is free. Well commented source is provided. You may reuse the
 
23
 * source in any way you please with the exception anything that uses it must be
 
24
 * marked to indicate is contains 'non-military use only' components.
 
25
 *
 
26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
27
 *
 
28
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
 
29
 *
 
30
 * This file is part of the Cobweb Project.
 
31
 *
 
32
 * Cobweb is free software: you can redistribute it and/or modify it under the
 
33
 * terms of the GNU General Public License version 3 only, as published by the
 
34
 * Free Software Foundation.
 
35
 *
 
36
 * Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
 
37
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 
38
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
 
39
 * details (a copy is included in the LICENSE file that accompanied this code).
 
40
 *
 
41
 * You should have received a copy of the GNU General Public License version 3
 
42
 * along with Cobweb.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
 
43
 * copy of the GPLv3 License.
 
44
 *
 
45
 * For Silvio, Joy and Adi.
 
46
 *
 
47
 ******************************************************************************/
 
48
 
 
49
 
 
50
define ([
 
51
        "jquery",
 
52
        "cobweb/Fields/SFBool",
 
53
        "cobweb/Basic/X3DBaseNode",
 
54
        "lib/gettext",
 
55
],
 
56
function ($,
 
57
          SFBool,
 
58
          X3DBaseNode,
 
59
          _)
 
60
{
 
61
"use strict";
 
62
        
 
63
   function f2 (n) { return Math .floor (n * 100) / 100; }
 
64
 
 
65
        function BrowserTimings (executionContext)
 
66
        {
 
67
                X3DBaseNode .call (this, executionContext);
 
68
 
 
69
                this .addChildObjects ("enabled", new SFBool ());
 
70
        }
 
71
 
 
72
        BrowserTimings .prototype = $.extend (Object .create (X3DBaseNode .prototype),
 
73
        {
 
74
                constructor: BrowserTimings,
 
75
                getTypeName: function ()
 
76
                {
 
77
                        return "BrowserTimings";
 
78
                },
 
79
                getComponentName: function ()
 
80
                {
 
81
                        return "Cobweb";
 
82
                },
 
83
                getContainerField: function ()
 
84
                {
 
85
                        return "browserTimings";
 
86
                },
 
87
                initialize: function ()
 
88
                {
 
89
                        X3DBaseNode .prototype .initialize .call (this);
 
90
 
 
91
                        this .enabled_ .addInterest ("set_enabled__", this);
 
92
 
 
93
                        this .localeOptions = { minimumFractionDigits: 2, maximumFractionDigits: 2 };
 
94
                        this .type          = this .getBrowser () .getDataStorage () ["BrowserTimings.type"] || "LESS";
 
95
                        this .startTime     = 0;
 
96
                        this .frames        = 0;
 
97
 
 
98
                        this .element = $("<div></div>") .addClass ("cobweb-browser-timings") .appendTo (this .getBrowser () .getElement () .find (".cobweb-surface"));
 
99
                        this .table   = $("<table></table>") .appendTo (this .element);
 
100
                        this .header  = $("<thead></thead>") .append ($("<tr></tr>") .append ($("<th colspan='2'></th>"))) .appendTo (this .table);
 
101
                        this .body    = $("<tbody></tbody>") .appendTo (this .table);
 
102
                        this .footer  = $("<tfoot></tfoot>") .append ($("<tr></tr>") .append ($("<td colspan='2'></td>"))) .appendTo (this .table);
 
103
                        this .button  = $("<button></button>") .click (this .set_type__ .bind (this)) .appendTo (this .footer .find ("td"));
 
104
                        this .rows    = [ ];
 
105
 
 
106
                        this .set_button__ ();
 
107
 
 
108
                        if (this .getBrowser () .getDataStorage () ["BrowserTimings.enabled"])
 
109
                                this .enabled_ = true;
 
110
                },
 
111
                set_enabled__: function (enabled)
 
112
                {
 
113
                        if (! this .getBrowser () .getBrowserOptions () .getTimings ())
 
114
                                return;
 
115
 
 
116
                        this .getBrowser () .getDataStorage () ["BrowserTimings.enabled"] = enabled .getValue ();
 
117
 
 
118
                        if (enabled .getValue ())
 
119
                        {
 
120
                                this .element .fadeIn ();
 
121
                                this .getBrowser () .prepareEvents () .addInterest ("update", this);
 
122
                                this .update ();
 
123
                        }
 
124
                        else
 
125
                        {
 
126
                                this .element .fadeOut ();
 
127
                                this .getBrowser () .prepareEvents () .removeInterest ("update", this);
 
128
                        }
 
129
                },
 
130
                set_type__: function ()
 
131
                {
 
132
                        if (this .type === "MORE")
 
133
                                this .type = "LESS";
 
134
                        else
 
135
                                this .type = "MORE";
 
136
 
 
137
                        this .getBrowser () .getDataStorage () ["BrowserTimings.type"] = this .type;
 
138
 
 
139
                        this .set_button__ ();
 
140
                        this .build ();
 
141
                },
 
142
                set_button__: function ()
 
143
                {
 
144
                        if (this .type === "MORE")
 
145
                                this .button .text (_("Less Properties"));
 
146
                        else
 
147
                                this .button .text (_("More Properties"));
 
148
                },
 
149
                update: function ()
 
150
                {
 
151
                        var currentTime = this .getBrowser () .getCurrentTime ();
 
152
                
 
153
                        if (currentTime - this .startTime > 1)
 
154
                        {
 
155
                           this .build ();
 
156
                                
 
157
                                this .frames    = 0;
 
158
                                this .startTime = currentTime;
 
159
                        }
 
160
                        else
 
161
                                ++ this .frames;
 
162
                },
 
163
                build: function ()
 
164
                {
 
165
                        var
 
166
                                browser     = this .getBrowser (),
 
167
                                currentTime = browser .getCurrentTime (),
 
168
                                language    = navigator .language || navigator .userLanguage,
 
169
                                fixed       = this .localeOptions,
 
170
                                rows        = this .rows,
 
171
                                r           = 0;
 
172
                        
 
173
                        rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Frame rate") + ":")) .append ($("<td></td>") .text (f2(this .frames / (currentTime - this .startTime)) .toLocaleString (language, fixed) + " " + _("fps")));
 
174
                        rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Speed")      + ":")) .append ($("<td></td>") .text (f2(this .getSpeed (browser .currentSpeed))         .toLocaleString (language, fixed) + " " + this .getSpeedUnit (browser .currentSpeed)));
 
175
 
 
176
                        if (this .type === "MORE")
 
177
                        {
 
178
                                var 
 
179
                                        layers            = browser .getWorld () .getLayerSet () .getLayers (),
 
180
                                        activeLayer       = browser .getActiveLayer (),
 
181
                                        systemTime        = browser .systemTime,
 
182
                                        navigationTime    = activeLayer && browser .getCollisionCount () ? activeLayer .collisionTime : 0,
 
183
                                        collisionTime     = browser .collisionTime + navigationTime,
 
184
                                        routingTime       = browser .browserTime - (browser .cameraTime + browser .collisionTime + browser .displayTime + navigationTime),
 
185
                                        prepareEvents     = Object .keys (browser .prepareEvents () .getInterests ()) .length - 1,
 
186
                                        sensors           = Object .keys (browser .sensors () .getInterests ()) .length,
 
187
                                        opaqueShapes      = 0,
 
188
                                        transparentShapes = 0;
 
189
 
 
190
                                for (var l = 0; l < layers .length; ++ l)
 
191
                                {
 
192
                                        var layer = layers [l];
 
193
                                        opaqueShapes      += layer .numOpaqueShapes;
 
194
                                        transparentShapes += layer .numTransparentShapes;
 
195
                                }
 
196
 
 
197
                           rows [1] .addClass ("cobweb-more");
 
198
 
 
199
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Browser")   + ":")) .append ($("<td></td>") .text (f2(systemTime)           .toLocaleString (language, fixed) + " " + _("ms")));
 
200
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("X3D")       + ":")) .append ($("<td></td>") .text (f2(browser .browserTime) .toLocaleString (language, fixed) + " " + _("ms")));
 
201
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Routing")   + ":")) .append ($("<td></td>") .text (f2(routingTime)          .toLocaleString (language, fixed) + " " + _("ms")));
 
202
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Picking")   + ":")) .append ($("<td></td>") .text (f2(browser .pickingTime) .toLocaleString (language, fixed) + " " + _("ms")));
 
203
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Camera")    + ":")) .append ($("<td></td>") .text (f2(browser .cameraTime)  .toLocaleString (language, fixed) + " " + _("ms")));
 
204
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Collision") + ":")) .append ($("<td></td>") .text (f2(collisionTime)        .toLocaleString (language, fixed) + " " + _("ms")));
 
205
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Display")   + ":")) .append ($("<td></td>") .text (f2(browser .displayTime) .toLocaleString (language, fixed) + " " + _("ms")));
 
206
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Shapes")    + ":")) .append ($("<td></td>") .text (opaqueShapes + " + " + transparentShapes));
 
207
                                rows [r++] = $("<tr></tr>") .append ($("<td></td>") .text (_("Sensors")   + ":")) .append ($("<td></td>") .text (prepareEvents + sensors));
 
208
                        }
 
209
 
 
210
                        rows .length = r;
 
211
 
 
212
                        this .header .find ("th") .text (_("Browser Timings"));
 
213
                        this .body .empty ();
 
214
                        this .body .append (rows);
 
215
                },
 
216
                getSpeed: function (speed)
 
217
                {
 
218
                        if (speed < 15)
 
219
                                return speed;
 
220
 
 
221
                        return speed * 3.6;
 
222
                },
 
223
                getSpeedUnit: function (speed)
 
224
                {
 
225
                        if (speed < 15)
 
226
                                return _("m/s");
 
227
 
 
228
                        return _("km/h");
 
229
                },
 
230
        });
 
231
 
 
232
        return BrowserTimings;
 
233
});