PXR_NAMESPACE_USING_DIRECTIVE

void
WriteStats(FILE *file, const std::string& name, const TfStopwatch &timer)
{
    fprintf(
        file,
        "{'profile':'%s','metric':'time','value':%f,'samples':%zu}\n",
        name.c_str(),
        timer.GetSeconds(),
        timer.GetSampleCount());
}
Example #2
0
static void
Pause(double seconds)
{
    // Create a shared stopwatch named "Pause" and then
    // sleep for some number of seconds accumulating the
    // time in "Pause"
    //
    static TfStopwatch pauseWatch("pwatch", true);

    struct timespec delay;

    int64_t nanoseconds = (int64_t)(seconds * 1000000000);
    delay.tv_sec  = nanoseconds / 1000000000;
    delay.tv_nsec = nanoseconds % 1000000000;

    pauseWatch.Start();
    nanosleep(&delay, 0);
    pauseWatch.Stop();
}
// Returns the number of seconds it took to complete this operation.
double
_DoTBBTest(bool verify, const size_t arraySize, const size_t numIterations)
{
    std::vector<int> v;
    _PopulateVector(arraySize, &v);
    std::vector<int> save = v;

    TfStopwatch sw;
    sw.Start();
    std::vector<int> filterv;
    for (size_t i = 0; i < numIterations; i++) {
        v = save;
         WorkParallelSort(&v);
    }

    if (verify) {
        TF_AXIOM(numIterations == 1);
        for(unsigned int i = 1; i < v.size(); ++i){
            TF_AXIOM(v[i-1] <= v[i]);
        }
    }
    sw.Stop();
    return sw.GetSeconds();
}
// Returns the number of seconds it took to complete this operation.
double
_DoTBBTest(bool verify, const int arraySize, const size_t numIterations)
{
    std::vector<int> v;
    _PopulateVector(arraySize, v);

    TfStopwatch sw;
    sw.Start();
    int res = 0;
    for (size_t i = 0; i < numIterations; i++) {
         res = WorkParallelReduceN(0,
            arraySize,
            std::bind(&sum, _1, _2, _3, v),
            std::bind(&plus, _1, _2));
    }

    if (verify) {
        TF_AXIOM(numIterations == 1);
        TF_AXIOM(res = arraySize*(arraySize-1)/2);
    }

    sw.Stop();
    return sw.GetSeconds();
}
int main(int argc, char* argv[])
{
    FILE *statsFile = fopen("perfstats.raw", "w");
    TfStopwatch watch;

    int recrusionSizes[] = {1, 2, 10};//, 100};
    int testSizes[] = {1000000, 10000000, 100000000};
    for (int R : recrusionSizes) {
        std::cout << "Recursion depth: " << R << std::endl;  
        for (int size : testSizes) {

            watch.Reset();
            watch.Start();
            auto collection = CreateTrace(size, R);
            watch.Stop();
            std::cout << "Create Trace    N: " << size << " time: " 
                << watch.GetSeconds() << " scopes/msec: " 
                << float(size)/watch.GetMilliseconds()
                << std::endl;

            auto reporter = TraceReporter::New(
                "Test", TraceReporterDataSourceCollection::New(collection));

            watch.Reset();
            watch.Start();
            reporter->UpdateAggregateTree();
            watch.Stop();
            WriteStats( statsFile,
                        TfStringPrintf("aggregate tree R %d N %d", R, size),
                        watch);
            std::cout << "Aggregate Tree N: " << size << " time: " 
                << watch.GetSeconds()
                << " scopes/msec: " << float(size)/watch.GetMilliseconds()
                << std::endl;

            watch.Reset();
            watch.Start();
            auto tree = TraceEventTree::New(*collection);
            watch.Stop();
            WriteStats( statsFile,
                        TfStringPrintf("event tree R %d N %d", R, size),
                        watch);
            std::cout << "Event Tree     N: " << size << " time: " 
                << watch.GetSeconds()
                << " scopes/msec: " << float(size)/watch.GetMilliseconds()
                << std::endl;
        }
    }
    fclose(statsFile);
    return 0;
}
Example #6
0
static bool
Test_TfStopwatch()
{
    bool ok = true;

    // Test constructor
    TfStopwatch watch1("watch1");
    if (watch1.GetName() != "watch1") {
        cout << "GetName: expected \"watch1\", got "
             << watch1.GetName()
             << endl;
        ok = false;
    }

    // Test copy constructor.
    TfStopwatch watchCopy(watch1);
    if (watchCopy.GetSeconds() != watch1.GetSeconds() ||
        watchCopy.GetName()    != watch1.GetName()) {
        cout << "expected watchCopy to contain (\"watch1\", 0.0) but got (\""
             << watchCopy.GetName()
             << "\", " << watchCopy.GetSeconds()
             << ")" << endl;
        ok = false;
    }

    // Test the timer
    struct timespec delay;

    // Delay .5 seconds (500 million nanoseconds)
    //
    delay.tv_sec  = 0;
    delay.tv_nsec = 500000000;

    watch1.Start();
    nanosleep(&delay, 0);
    watch1.Stop();

    // The value of watch1 should be "near" 0.5 seconds
    if (fabs(watch1.GetSeconds() - 0.5) > 0.05) {
        cout << "nanosleep for .5 seconds but measured time was "
             << watch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Delay another .5 seconds and see if watch is near 1
    //
    watch1.Start();
    nanosleep(&delay, 0);
    watch1.Stop();

    // The value of watch1 should be "near" 1.0 seconds
    if (fabs(watch1.GetSeconds() - 1.0) > 0.1) {
        cout << "nanosleep for 1.0 seconds but measured time was "
             << watch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // The value of watchCopy should be zero
    //
    if (watchCopy.GetSeconds() != 0.0) {
        cout << "watchCopy has non-zero initial time of "
             << watchCopy.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test AddFrom
    //
    watchCopy.AddFrom(watch1);
    if (watchCopy.GetSeconds() != watch1.GetSeconds()) {
        cout << "AddFrom: watchCopy has time of "
             << watchCopy.GetSeconds()
             << " instead of "
             << watch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test AddFrom
    //
    watchCopy.AddFrom(watch1);
    if (fabs(watchCopy.GetSeconds()/watch1.GetSeconds() - 2.0) > 0.00001) {
        cout << "AddFrom: watchCopy has time of "
             << watchCopy.GetSeconds()
             << " instead of "
             << 2 * watch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test Reset
    watchCopy.Reset();
    if (watchCopy.GetSeconds() != 0.0) {
        cout << "Reset: watchCopy has time of "
             << watchCopy.GetSeconds()
             << " instead of "
             << 0.0
             << " seconds."
             << endl;
        ok = false;
    }


    //////////////// Shared Stopwatches ////////////////

    // Test constructor
    TfStopwatch swatch1("swatch1", true);
    if (swatch1.GetName()  != "swatch1" ||
        swatch1.IsShared() != true)
    {
        cout << "GetName: expected \"swatch1\", got "
             << swatch1.GetName()
             << endl;
        ok = false;
    }

    // Test copy constructor.
    TfStopwatch swatchCopy(swatch1);
    if (swatchCopy.GetSeconds() != swatch1.GetSeconds() ||
        swatchCopy.GetName()    != swatch1.GetName() ||
        swatchCopy.IsShared()   != false)
    {
        cout << "expected watchCopy to contain (\"swatch1\", 0.0, false) but got (\""
             << swatchCopy.GetName()
             << "\", "
             << swatchCopy.GetSeconds()
             << ", "
             << swatchCopy.IsShared()
             << ")" << endl;
        ok = false;
    }

    // Test the timer
    // Delay .5 seconds (500 million nanoseconds)
    //
    delay.tv_sec  = 0;
    delay.tv_nsec = 500000000;

    swatch1.Start();
    nanosleep(&delay, 0);
    swatch1.Stop();

    // The value of swatch1 should be "near" 0.5 seconds
    if (fabs(swatch1.GetSeconds() - 0.5) > 0.05) {
        cout << "nanosleep for .5 seconds but measured time was "
             << swatch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Delay another .5 seconds and see if swatch is near 1
    //
    swatch1.Start();
    nanosleep(&delay, 0);
    swatch1.Stop();

    // The value of swatch1 should be "near" 1.0 seconds
    if (fabs(swatch1.GetSeconds() - 1.0) > 0.1) {
        cout << "nanosleep for 1.0 seconds but measured time was "
             << swatch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test the assignment operator
    //
    watch1 = swatch1;
    if (watch1.GetSeconds() != swatch1.GetSeconds() ||
        watch1.GetName()    != swatch1.GetName() ||
        watch1.IsShared()   != false)
    {
        cout << "expected watch1 to contain (\"swatch1\", 0.0, false) but got (\""
             << watch1.GetName()
             << "\", "
             << watch1.GetSeconds()
             << ", "
             << watch1.IsShared()
             << ")" << endl;
        ok = false;
    }

    TfStopwatch swatch2("swatch2", true);

    // The value of swatch2 should be zero
    //
    if (swatch2.GetSeconds() != 0.0) {
        cout << "swatch2 has non-zero initial time of "
             << swatch2.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test AddFrom
    //
    swatch2.AddFrom(swatch1);
    if (swatch2.GetSeconds() != swatch1.GetSeconds()) {
        cout << "AddFrom: swatch2 has time of "
             << swatch2.GetSeconds()
             << " instead of "
             << swatch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test AddFrom
    //
    swatch2.AddFrom(swatch1);
    if (fabs(swatch2.GetSeconds()/swatch1.GetSeconds() - 2.0) > 0.00001) {
        cout << "AddFrom: swatch2 has time of "
             << swatch2.GetSeconds()
             << " instead of "
             << 2 * swatch1.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test Reset
    swatch2.Reset();
    if (swatch2.GetSeconds() != 0.0) {
        cout << "Reset: swatch2 has time of "
             << swatch2.GetSeconds()
             << " instead of "
             << 0.0
             << " seconds."
             << endl;
        ok = false;
    }

    // Test GetStopwatchNames
    vector<string> names = TfStopwatch::GetStopwatchNames();
    sort(names.begin(), names.end());

    if (TfStringJoin(names) != "swatch1 swatch2") {
        cout << "GetStopwatchNames returned: ("
             << TfStringJoin(names, ", ")
             << ") instead of (swatch1, swatch2)."
             << endl;
        ok = false;
    }

    // Call Pause, this should create a 3rd name
    //
    Pause(0.5);

    names = TfStopwatch::GetStopwatchNames();
    sort(names.begin(), names.end());

    if (TfStringJoin(names) != "pwatch swatch1 swatch2") {
        cout << "GetStopwatchNames returned: ("
             << TfStringJoin(names, ", ")
             << ") instead of (pwatch, swatch1, swatch2)."
             << endl;
        ok = false;
    }

    
    TfStopwatch pauseWatch = TfStopwatch::GetNamedStopwatch("pwatch");

    if (fabs(pauseWatch.GetSeconds() - 0.5) > 0.05) {
        cout << "pause for .5 seconds but measured time was "
             << pauseWatch.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Now pause for another half second and then get the result time.
    //
    Pause(0.5);

    pauseWatch = TfStopwatch::GetNamedStopwatch("pwatch");
    if (fabs(pauseWatch.GetSeconds() - 1.0) > 0.1) {
        cout << "pause for 1.0 seconds but measured time was "
             << pauseWatch.GetSeconds()
             << " seconds."
             << endl;
        ok = false;
    }

    // Test removing from the set of named watches.  Copying over a shared
    // stopwatch should make it unshared and remove it from the list.
    //
    swatch2 = swatch1;

    names = TfStopwatch::GetStopwatchNames();
    sort(names.begin(), names.end());

    if (TfStringJoin(names) != "pwatch swatch1" ||
        swatch2.GetSeconds() != swatch1.GetSeconds() ||
        swatch2.GetName()    != swatch1.GetName() ||
        swatch2.IsShared()   != false)
    {
        cout << "Assignment to remove a shared stopwatch failed.\n"
             << "  GetStopwatchNames returned: ("
             << TfStringJoin(names, ", ")
             << "), expected (pwatch, swatch1).\n"
             << "  GetSeconds returned: " << swatch2.GetSeconds()
             << ", expected " << swatch1.GetSeconds() << "\n"
             << "  GetName returned: " << swatch2.GetName()
             << ", expected " << swatch1.GetName() << "\n"
             << "  IsShared returned: " << swatch2.IsShared()
             << ", expected false"
             << endl;
        ok = false;
    }

    // Test removing names in the destructor
    //
    TfStopwatch* swatchTemp = new TfStopwatch("swatchTemp", true);

    string names1 = TfStringJoin(TfStopwatch::GetStopwatchNames());

    delete swatchTemp;

    string names2 = TfStringJoin(TfStopwatch::GetStopwatchNames());

    if (names1 != "pwatch swatch1 swatchTemp" ||
        names2 != "pwatch swatch1")
    {
        cout << "Allocating and deleting swatchTemp failed to add or remove it.\n"
             << "  After allocting, name list was (" << names1
             << "), expected (pwatch swatch1 swatchTemp)\n"
             << "  After deleting, name list was (" << names2
             << "), expected (pwatch swatch1)"
             << endl;
        ok = false;
    }

    return ok;
}
Example #7
0
HdTextureResourceSharedPtr
UsdImagingGL_GetTextureResource(UsdPrim const& usdPrim,
                                SdfPath const& usdPath,
                                UsdTimeCode time)
{
    if (!TF_VERIFY(usdPrim))
        return HdTextureResourceSharedPtr();
    if (!TF_VERIFY(usdPath != SdfPath()))
        return HdTextureResourceSharedPtr();

    UsdAttribute attr = _GetTextureResourceAttr(usdPrim, usdPath);
    SdfAssetPath asset;
    if (!TF_VERIFY(attr) || !TF_VERIFY(attr.Get(&asset, time))) {
        return HdTextureResourceSharedPtr();
    }

    HdTextureType textureType = HdTextureType::Uv;

    TfToken filePath = TfToken(asset.GetResolvedPath());
    // If the path can't be resolved, it's either an UDIM texture
    // or the texture doesn't exists and we can to exit early.
    if (filePath.IsEmpty()) {
        filePath = TfToken(asset.GetAssetPath());
        if (GlfIsSupportedUdimTexture(filePath)) {
            textureType = HdTextureType::Udim;
        } else {
            TF_DEBUG(USDIMAGING_TEXTURES).Msg(
                "File does not exist, returning nullptr");
            TF_WARN("Unable to find Texture '%s' with path '%s'.",
                    filePath.GetText(), usdPath.GetText());
            return {};
        }
    } else {
        if (GlfIsSupportedPtexTexture(filePath)) {
            textureType = HdTextureType::Ptex;
        }
    }

    GlfImage::ImageOriginLocation origin =
            UsdImagingGL_ComputeTextureOrigin(usdPrim);

    HdWrap wrapS = _GetWrapS(usdPrim, textureType);
    HdWrap wrapT = _GetWrapT(usdPrim, textureType);
    HdMinFilter minFilter = _GetMinFilter(usdPrim);
    HdMagFilter magFilter = _GetMagFilter(usdPrim);
    float memoryLimit = _GetMemoryLimit(usdPrim);

    TF_DEBUG(USDIMAGING_TEXTURES).Msg(
            "Loading texture: id(%s), type(%s)\n",
            usdPath.GetText(),
            textureType == HdTextureType::Uv ? "Uv" :
            textureType == HdTextureType::Ptex ? "Ptex" : "Udim");
 
    HdTextureResourceSharedPtr texResource;
    TfStopwatch timer;
    timer.Start();
    // Udim's can't be loaded through like other textures, because
    // we can't select the right factory based on the file type.
    // We also need to pass the layer context to the factory,
    // so each file gets resolved properly.
    GlfTextureHandleRefPtr texture;
    if (textureType == HdTextureType::Udim) {
        UdimTextureFactory factory(_FindLayerHandle(attr, time));
        texture = GlfTextureRegistry::GetInstance().GetTextureHandle(
            filePath, origin, &factory);
    } else {
        texture = GlfTextureRegistry::GetInstance().GetTextureHandle(
            filePath, origin);
    }

    texResource = HdTextureResourceSharedPtr(
        new HdStSimpleTextureResource(texture, textureType, wrapS, wrapT,
                                      minFilter, magFilter, memoryLimit));
    timer.Stop();

    TF_DEBUG(USDIMAGING_TEXTURES).Msg("    Load time: %.3f s\n", 
                                     timer.GetSeconds());

    return texResource;
}