this should do it...

I'm linking to chromium release 35622

monitor the process memory usage in the task manager.

it appears that the slowdown occurs in Context::New.

#include <v8.h>

#include <string>
#include <ctime>

using namespace std;
using namespace v8;

static long limit;
static v8::Persistent<v8::Script> script1, script2;
static v8::Persistent<ObjectTemplate> global;

v8::Handle<v8::Value> exec(v8::Handle<v8::Script>& compiled_script)
{
        v8::TryCatch try_catch;
    try_catch.SetVerbose(true);
        if (!compiled_script.IsEmpty())
        {
                v8::Handle<v8::Value> result = compiled_script->Run();
                if (try_catch.HasCaught()) {
                        v8::String::Utf8Value error(try_catch.Exception());
                        printf("%s\n", *error);
                } else if (!result.IsEmpty())
                        return result;
        }
        return try_catch.Exception();
}

static Handle<Value> LogCallback(const Arguments& args) {
if (limit > 0) {
        HandleScope scope;
        if (--limit%4 == 0) {
                Handle<Context> context2 = Context::New(NULL, global);
                context2->Enter();
                exec(script2);
                context2->Exit();
        } else
                exec(limit%3 ? script1 : script2);
}
        return v8::Undefined();
}

v8::Persistent<v8::Script> makeScript(char *stringScript) {
        v8::Persistent<v8::Script> script = v8::Persistent<v8::Script>::New(
        v8::Script::New(v8::String::New(stringScript)));
        if (script.IsEmpty())
        {
                printf("Error compiling script %s", stringScript);
        }
        return script;
}

int main(int argc, char* argv[]) {

// Each processor gets its own context so different processors
// do not affect each other.

clock_t start, end;
HeapStatistics heap;
int n=0;
do {
        {
        v8::HandleScope scope;

start = clock();
global = v8::Persistent<v8::ObjectTemplate>::New(ObjectTemplate::New
());
global->Set(String::New("log"), FunctionTemplate::New(LogCallback));
Persistent<Context> context1 = Context::New(NULL, global);
context1->Enter();
if (n==0) {
        script2.Dispose();
        script2.Clear();
        script1.Dispose();
        script1.Clear();
V8::LowMemoryNotification(); // invoke CollectAllGarbage to no avail

        script1 = makeScript("log(1)");
script2 = makeScript("log(2)");
}
end = clock();
printf("\ncontext creation time %ld milliseconds\n",(end-start));
printf("NumberOfHandles %d\n",v8::HandleScope::NumberOfHandles());

limit = 600;
start = clock();
exec(script1);
end = clock();
printf("script execution time %ld milliseconds\n\n",(end-start));
printf("NumberOfHandles now %d\n",v8::HandleScope::NumberOfHandles());
if (context1->HasOutOfMemoryException())
        printf("Context experienced HasOutOfMemoryException\n");
V8::GetHeapStatistics(&heap);
printf("total_heap_size %ld used_heap_size %ld\n",
heap.total_heap_size(),heap.used_heap_size());
printf("check memory usage...\nenter 0 to dispose and recreate scripts
\nenter 0 or 1 to continue> ");
scanf_s ("%d",&n);

context1->Exit();
context1.Dispose();
context1.Clear();
        }

} while (n<=1);


}

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to