static void
formatUnknownContentLength(char* buf, PRUint32 len, PRUint32 bytesReceived, PRUint32 elapsed)
{
    char rate[32];
    *rate = 0;

    // the transfer rate
    double bytes_per_sec = 0;
    if (elapsed > 0)
        bytes_per_sec = ((double) bytesReceived) / ((double) elapsed);

    formatRate(rate, sizeof(rate), bytes_per_sec);

    // the number of bytes received
    char bytes_received[32];

    if (bytesReceived < KILOBYTE)
        PR_snprintf(bytes_received, sizeof(bytes_received),
                    XP_GetString(XP_THERMO_UH),
                    bytesReceived, IS_PLURAL(bytesReceived));
    else
        PR_snprintf(bytes_received, sizeof(bytes_received),
                    XP_GetString(XP_THERMO_KBYTE_FORMAT),
                    bytesReceived / KILOBYTE);

    if (*rate) {
        /* "%s read (at %s)" */
        PR_snprintf(buf, len, XP_GetString(XP_THERMO_PERCENT_RATE_FORM), bytes_received, rate);
    }
    else {
        PR_snprintf(buf, len, XP_GetString(XP_THERMO_RAW_COUNT_FORM), bytes_received);
    }
}
Esempio n. 2
0
void benchmark(autoserial::ISerializable *object, int runs)
{
    autoserial::OpaqueObject o;
    o.set(object);
    Size size = o.getSize();
    std::cout << "Benching " << object->getTypeNameV() << " of " << formatSize(size) << std::endl;

    {
        // With alloaction + deallocation (closer to what happens in reality)
        double st = Timer::get();
        for (int i=0; i < runs; ++i)
        {
            autoserial::OpaqueObject obj;
            obj.set(object);
        }
        double t = Timer::get()-st;
        std::cout << "Serialization in " << t/runs << " ms" << std::endl;
        std::cout << "  Serialization including (de)allocation: " << formatRate(t, size, runs) << std::endl;
    }
    {
        // With alloaction + deallocation (closer to what happens in reality)
        double st = Timer::get();
        for (int i=0; i < runs; ++i)
        {
            FLUSH();
            autoserial::OpaqueObject obj;
            obj.set(object);
        }
        double t = Timer::get()-st - flushTime*runs;
        std::cout << "Serialization in " << t/runs << " ms" << std::endl;
        std::cout << "  Serialization including (de)allocation: " << formatRate(t, size, runs) << std::endl;
    }
    {
        // Deserialization+deallocation
        double st = Timer::get();
        for (int i=0; i < runs; ++i)
        {
            autoserial::ISerializable *obj = o.get();
            delete obj;
        }
        double t = Timer::get()-st;
        std::cout << "DESerialization in " << t/runs << " ms" << std::endl;
        std::cout << "  Deserialization including (de)allocation: " << formatRate(t, size, runs) << std::endl;
    }
    {
        // Deserialization+deallocation
        double st = Timer::get();
        for (int i=0; i < runs; ++i)
        {
            FLUSH();
            autoserial::ISerializable *obj = o.get();
            delete obj;
        }
        double t = Timer::get()-st - flushTime*runs;
        std::cout << "DESerialization in " << t/runs << " ms" << std::endl;
        std::cout << "  Deserialization including (de)allocation: " << formatRate(t, size, runs) << std::endl;
    }

    {
        // Comparison
        autoserial::ISerializable *obj = o.get();
        int count = 0;
        double st = Timer::get();
        for (int i=0; i < runs; ++i)
        {
            FLUSH();
            if (!obj->equals(object))
                count++;
        }
        double t = Timer::get()-st - flushTime*runs;
        std::cout << "  " << count << " Comparison time: " << formatRate(t, size, runs) << std::endl;
    }
}
static void
formatKnownContentLength(char* buf,
                         PRUint32 len,
                         PRUint32 bytesReceived,
                         PRUint32 contentLength,
                         PRUint32 elapsed)
{
    char rate[32];
    *rate = 0;

    // the transfer rate
    double bytes_per_sec = 0;
    if (elapsed > 0)
        bytes_per_sec = ((double) bytesReceived) / ((double) elapsed);

    formatRate(rate, sizeof(rate), bytes_per_sec);
    

    // format the content length
    char length[32];
    *length = 0;

    if (contentLength < KILOBYTE)
        PR_snprintf(length, sizeof(length), XP_GetString(XP_THERMO_BYTE_FORMAT), contentLength);
    else
        PR_snprintf(length, sizeof(length), XP_GetString(XP_THERMO_KBYTE_FORMAT), contentLength / KILOBYTE);
	

    // the percentage complete
    char percent[32];

    PRUint32 p = (bytesReceived * 100) / contentLength;
    if (p >= 100 && bytesReceived != contentLength)
        p = 99;

    PR_snprintf(percent, sizeof(percent), XP_GetString(XP_THERMO_PERCENTAGE_FORMAT), p);

    // the amount of time remaining
    char tleft[32];
    *tleft = 0;

    if (bytes_per_sec >= KILOBYTE && elapsed >= ENOUGH_TIME_TO_GUESS) {
        PRUint32 secs_left =
            (PRUint32) (((double) (contentLength - bytesReceived)) / bytes_per_sec);

        if (secs_left >= HOUR) {
            PR_snprintf(tleft, sizeof(tleft),
                        XP_GetString(XP_THERMO_HOURS_FORMAT),
                        secs_left / HOUR,
                        (secs_left / MINUTE) % MINUTE,
                        secs_left % MINUTE);
        }
        else if (secs_left >= MINUTE) {
            PR_snprintf(tleft, sizeof(tleft),
                        XP_GetString(XP_THERMO_MINUTES_FORMAT),
                        secs_left / MINUTE,
                        secs_left % MINUTE);
        }
        else if (secs_left > 0) {
            PR_snprintf(tleft, sizeof(tleft),
                        XP_GetString(XP_THERMO_SECONDS_FORMAT),
                        secs_left,
                        IS_PLURAL(secs_left));
        }
    }

    if (*tleft) {
        /* "%s of %s (at %s, %s remaining)" */
        PR_snprintf(buf, len,
                    XP_GetString(XP_THERMO_RATE_REMAINING_FORM),
                    percent, length, rate, tleft);
    }
    else if (*rate) {
        /* "%s of %s (at %s)" */
        PR_snprintf(buf, len,
                    XP_GetString(XP_THERMO_RATE_FORM),
                    percent, length, rate);
    }
    else {
        /* "%s of %s" */
        PR_snprintf(buf, len,
                    XP_GetString(XP_THERMO_PERCENT_FORM),
                    percent, length);
    }
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
    std::cout << "========= Benchmarking autoserial =========" << std::endl;
    std::cout << " All times are in seconds " << std::endl;

    // Compute rough memory bandwidth
    {
#define bufsize 100000000
        int iter = 100;
        char *buf1 = new char[bufsize];
        char *buf2 = new char[bufsize];
        // Touch the memory
        memcpy(buf1, buf2, bufsize*sizeof(char));
        double st = Timer::get();
        for (int i = 0; i < iter; ++i)
            memcpy(buf1, buf2, bufsize*sizeof(char));
        double t = Timer::get() - st;
        std::cout << "Memory bandwidth (large memcpy): " << formatRate(t, bufsize, iter) << std::endl;
        st = Timer::get();
        for (int i = 0; i < iter; ++i)
            memset(buf1, 0, bufsize*sizeof(char));
        t = Timer::get() - st;
        std::cout << "Memory bandwidth (large memset): " << formatRate(t, bufsize, iter) << std::endl;
        st = Timer::get();
        int count = 0;
        for (int i = 0; i < iter; ++i)
            count += memcmp(buf1, buf2, bufsize*sizeof(char));
        t = Timer::get() - st;
        std::cout << "Memory bandwidth (large memcmp): " << formatRate(t, bufsize, iter) << "   " << count << std::endl;
    }

    // Compute rough memory bandwidth
    {
#undef bufsize
#define bufsize 100
        int iter = 100000000;
        char *buf1 = new char[bufsize];
        char *buf2 = new char[bufsize];
        // Touch the memory
        memcpy(buf1, buf2, bufsize*sizeof(char));
        double st = Timer::get();
        for (int i = 0; i < iter; ++i)
            memcpy(buf1, buf2, bufsize*sizeof(char));
        double t = Timer::get() - st;
        std::cout << "Memory bandwidth (large memcpy): " << formatRate(t, bufsize, iter) << std::endl;
        st = Timer::get();
        for (int i = 0; i < iter; ++i)
            memset(buf1, 0, bufsize*sizeof(char));
        t = Timer::get() - st;
        std::cout << "Memory bandwidth (large memset): " << formatRate(t, bufsize, iter) << std::endl;
        st = Timer::get();
        int count = 0;
        for (int i = 0; i < iter; ++i)
            count += memcmp(buf1, buf2, bufsize*sizeof(char));
        t = Timer::get() - st;
        std::cout << "Memory bandwidth (large memcmp): " << formatRate(t, bufsize, iter) << "   " << count << std::endl;
    }

    // Initialize flushBuffer, to be read after every serialization to flush the cache
    flushBuffer = (int*)malloc(flushBufferSize);
    memset(flushBuffer, 0, flushBufferSize);
    // Get reference time, to be subtracted to each measurement
    flushTime = Timer::get();
    for (int i=0; i<100; ++i)
        FLUSH();
    flushTime = (Timer::get()-flushTime)/100;

    // Define various types of serializable objects
    {
        IntDouble d(0, 1.3);
        benchmark(&d, 1000);
    }

    {
        int a[] = {10, 100, 1000, 10000};
        for (size_t i = 0; i<sizeof(a)/sizeof(int); ++i)
        {
            IntBuffer b(a[i]);
            benchmark(&b, 1000);
        }
    }

    {
        Misc m;
        benchmark(&m, 10000);
    }

    /*	{
    		MiscBuffer mb(100);
    	}
    */
    {
        int a[] = {10, 100, 1000, 10000};
        for (size_t i = 0; i<sizeof(a)/sizeof(int); ++i)
        {
            autoserial::internal::Buffer<Misc> b;
            b.resize(a[i]);
            benchmark(&b, 100000/a[i]);
        }
    }

    {
        int a[] = {10, 100, 1000, 10000};
        for (size_t i = 0; i<sizeof(a)/sizeof(int); ++i)
        {
            StdVector s(a[i]);
            benchmark(&s, 1000);
        }
    }

    {
        int a[] = {10, 100, 1000, 10000};
        for (size_t i = 0; i<sizeof(a)/sizeof(int); ++i)
        {
            MapOfIntBuffers m(a[i]);
            benchmark(&m, 1000/a[i]);
        }
    }

    {
        int a[] = {10, 100, 1000, 10000};
        for (size_t i = 0; i<sizeof(a)/sizeof(int); ++i)
        {
            LinkedList l(a[i]);
            benchmark(&l, 1000000/a[i]);
        }
    }

    return 0;
}