comparison json/VampJson.h @ 46:6fe9f5010505

Some timing calculations. I don't actually want to keep these, they're just for reference -- will back out in a mo
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 12 Sep 2016 15:17:35 +0100
parents 7668fbdcfb15
children fa2edfabf829
comparison
equal deleted inserted replaced
45:7668fbdcfb15 46:6fe9f5010505
46 #include <vamp-hostsdk/Plugin.h> 46 #include <vamp-hostsdk/Plugin.h>
47 #include <vamp-hostsdk/PluginLoader.h> 47 #include <vamp-hostsdk/PluginLoader.h>
48 48
49 #include "bits/PluginHandleMapper.h" 49 #include "bits/PluginHandleMapper.h"
50 #include "bits/RequestResponseType.h" 50 #include "bits/RequestResponseType.h"
51
52 #include <chrono>
51 53
52 namespace vampipe { 54 namespace vampipe {
53 55
54 /** 56 /**
55 * Convert the structures laid out in the Vamp SDK classes into JSON 57 * Convert the structures laid out in the Vamp SDK classes into JSON
315 return pd; 317 return pd;
316 } 318 }
317 319
318 static std::string 320 static std::string
319 fromFloatBuffer(const float *buffer, size_t nfloats) { 321 fromFloatBuffer(const float *buffer, size_t nfloats) {
322 static std::chrono::nanoseconds total;
323 auto t0 = std::chrono::high_resolution_clock::now();
320 // must use char pointers, otherwise the converter will only 324 // must use char pointers, otherwise the converter will only
321 // encode every 4th byte (as it will count up in float* steps) 325 // encode every 4th byte (as it will count up in float* steps)
322 const char *start = reinterpret_cast<const char *>(buffer); 326 const char *start = reinterpret_cast<const char *>(buffer);
323 const char *end = reinterpret_cast<const char *>(buffer + nfloats); 327 const char *end = reinterpret_cast<const char *>(buffer + nfloats);
324 std::string encoded; 328 std::string encoded;
329 encoded.reserve(16 * nfloats / 3);
325 bn::encode_b64(start, end, back_inserter(encoded)); 330 bn::encode_b64(start, end, back_inserter(encoded));
331 auto t1 = std::chrono::high_resolution_clock::now();
332 total += std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0);
333 // std::cout << "encode: " << total.count()/1000000 << std::endl;
326 return encoded; 334 return encoded;
327 } 335 }
328 336
329 static std::vector<float> 337 static std::vector<float>
330 toFloatBuffer(std::string encoded) { 338 toFloatBuffer(std::string encoded) {
339 static std::chrono::nanoseconds total;
340 auto t0 = std::chrono::high_resolution_clock::now();
331 std::string decoded; 341 std::string decoded;
342 decoded.reserve(3 * encoded.size() / 4);
332 bn::decode_b64(encoded.begin(), encoded.end(), back_inserter(decoded)); 343 bn::decode_b64(encoded.begin(), encoded.end(), back_inserter(decoded));
333 const float *buffer = reinterpret_cast<const float *>(decoded.c_str()); 344 const float *buffer = reinterpret_cast<const float *>(decoded.c_str());
334 size_t n = decoded.size() / sizeof(float); 345 size_t n = decoded.size() / sizeof(float);
335 return std::vector<float>(buffer, buffer + n); 346 std::vector<float> result(buffer, buffer + n);
347 auto t1 = std::chrono::high_resolution_clock::now();
348 total += std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0);
349 // std::cout << "decode: " << total.count()/1000000 << std::endl;
350 return result;
336 } 351 }
337 352
338 static json11::Json 353 static json11::Json
339 fromFeature(const Vamp::Plugin::Feature &f, 354 fromFeature(const Vamp::Plugin::Feature &f,
340 BufferSerialisation serialisation) { 355 BufferSerialisation serialisation) {
866 static Vamp::HostExt::ProcessRequest 881 static Vamp::HostExt::ProcessRequest
867 toProcessRequest(json11::Json j, 882 toProcessRequest(json11::Json j,
868 const PluginHandleMapper &mapper, 883 const PluginHandleMapper &mapper,
869 BufferSerialisation &serialisation) { 884 BufferSerialisation &serialisation) {
870 885
886 static std::chrono::nanoseconds total;
887 auto t0 = std::chrono::high_resolution_clock::now();
888
871 std::string err; 889 std::string err;
872 890
873 if (!j.has_shape({ 891 if (!j.has_shape({
874 { "pluginHandle", json11::Json::NUMBER }, 892 { "pluginHandle", json11::Json::NUMBER },
875 { "processInput", json11::Json::OBJECT } }, err)) { 893 { "processInput", json11::Json::OBJECT } }, err)) {
907 } else { 925 } else {
908 throw Failure("expected values or b64values in inputBuffers object"); 926 throw Failure("expected values or b64values in inputBuffers object");
909 } 927 }
910 } 928 }
911 929
930 auto t1 = std::chrono::high_resolution_clock::now();
931 total += std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0);
932 // std::cout << "parse: " << total.count()/1000000 << std::endl;
933
912 return r; 934 return r;
913 } 935 }
914 936
915 static json11::Json 937 static json11::Json
916 fromVampRequest_List() { 938 fromVampRequest_List() {