1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <ppapi/cpp/input_event.h>
6 #include <ppapi/cpp/instance.h>
7 #include <ppapi/cpp/module.h>
8 #include <ppapi/cpp/point.h>
9 #include <ppapi/cpp/var.h>
10 #include <ppapi/cpp/var_array.h>
11 #include <ppapi/cpp/var_dictionary.h>
12 #include <ppapi/utility/completion_callback_factory.h>
21 #include "framework.h"
23 void BenchmarkPost(Benchmark
* b
, double median
, double range
, void* data
) {
24 pp::VarDictionary benchmark
;
25 benchmark
.Set("name", b
->Name());
26 std::ostringstream result
;
27 result
<< std::fixed
<< std::setw(8) << std::setprecision(5) <<
29 benchmark
.Set("result", result
.str());
30 pp::VarDictionary message
;
31 message
.Set("message", "benchmark_result");
32 message
.Set("benchmark", benchmark
);
33 pp::Instance
* instance
= static_cast<pp::Instance
*>(data
);
34 instance
->PostMessage(message
);
39 class BenchmarksInstance
: public pp::Instance
{
41 explicit BenchmarksInstance(PP_Instance instance
)
42 : pp::Instance(instance
),
43 callback_factory_(this) {}
45 ~BenchmarksInstance() {}
47 virtual bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) {
48 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE
);
52 virtual void HandleMessage(const pp::Var
& var
) {
53 if (var
.is_dictionary()) {
54 pp::VarDictionary
dictionary(var
);
55 std::string message
= dictionary
.Get("message").AsString();
56 if (message
== "run_benchmark") {
57 BenchmarkSuite::Run("", BenchmarkPost
, this);
58 pp::VarDictionary message
;
59 message
.Set("message", "benchmark_finish");
66 pp::CompletionCallbackFactory
<BenchmarksInstance
> callback_factory_
;
69 class BenchmarksModule
: public pp::Module
{
71 BenchmarksModule() : pp::Module() {}
72 virtual ~BenchmarksModule() {}
74 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
75 return new BenchmarksInstance(instance
);
81 Module
* CreateModule() { return new BenchmarksModule(); }