main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "RakPeerInterface.h"
  11. #include "RakSleep.h"
  12. #include "RakThread.h"
  13. #include "RakNetworkFactory.h"
  14. #include <stdio.h>
  15. #include "../SQLite3Plugin/ServerOnly/sqlite3.h"
  16. #include "GetTime.h"
  17. #include "RakString.h"
  18. #include "jpeglib.h"
  19. using namespace RakNet;
  20. struct my_error_mgr {
  21. struct jpeg_error_mgr pub; /* "public" fields */
  22. };
  23. METHODDEF(void) my_error_exit (j_common_ptr cinfo);
  24. //
  25. // to handle fatal errors.
  26. // the original JPEG code will just exit(0). can't really
  27. // do that in Windows....
  28. //
  29. METHODDEF(void) my_error_exit (j_common_ptr cinfo)
  30. {
  31. }
  32. void main(void)
  33. {
  34. printf("Add images to SQLite database and read back to measure performance.\n");
  35. // Create a database, and tell the plugin about it
  36. sqlite3 *database;
  37. // Here :memory: means create the database in memory only.
  38. // Normally the first parameter refers to a path on the disk to the database file
  39. if (sqlite3_open_v2("C:\\EchoChamber\\sqliteDb", &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0)!=SQLITE_OK)
  40. return;
  41. char *errorMsg;
  42. sqlite3_exec(database, "CREATE TABLE Images (imageId_pk integer PRIMARY KEY, imageData bytea);", 0, 0, &errorMsg);
  43. if (errorMsg)
  44. {
  45. printf(errorMsg);
  46. sqlite3_free(errorMsg);
  47. return;
  48. }
  49. printf("Opening image\n");
  50. const char *imgPath1="c:\\temp\\img1.jpg";
  51. const char *imgPath2="c:\\temp\\img2.jpg";
  52. FILE *fp = fopen(imgPath1, "rb");
  53. unsigned fileLength1;
  54. fseek(fp, 0, SEEK_END);
  55. fileLength1 = ftell(fp);
  56. fseek(fp, 0, SEEK_SET);
  57. char *fileBuff1 = (char *) malloc(fileLength1);
  58. fread(fileBuff1, 1, fileLength1, fp);
  59. fclose(fp);
  60. fp = fopen(imgPath2, "rb");
  61. unsigned fileLength2;
  62. fseek(fp, 0, SEEK_END);
  63. fileLength2 = ftell(fp);
  64. fseek(fp, 0, SEEK_SET);
  65. char *fileBuff2 = (char *) malloc(fileLength2);
  66. fread(fileBuff2, 1, fileLength2, fp);
  67. fclose(fp);
  68. static const int numInsertions=100;
  69. printf("Adding image to database %i times\n", numInsertions);
  70. sqlite3_stmt *statement;
  71. if (sqlite3_prepare_v2(
  72. database,
  73. "INSERT INTO Images (imageData) VALUES (?);",
  74. -1,
  75. &statement,
  76. 0
  77. )!=SQLITE_OK)
  78. return;
  79. unsigned int i;
  80. int rc;
  81. for (i=0; i < numInsertions; i++)
  82. {
  83. if ((i&1)==0)
  84. {
  85. if (sqlite3_bind_blob(statement, 1, fileBuff1, fileLength1, 0)!=SQLITE_OK)
  86. return;
  87. }
  88. else
  89. {
  90. if (sqlite3_bind_blob(statement, 1, fileBuff2, fileLength2, 0)!=SQLITE_OK)
  91. return;
  92. }
  93. rc = sqlite3_step(statement);
  94. if (rc!=SQLITE_DONE && rc!=SQLITE_OK)
  95. return;
  96. sqlite3_reset(statement);
  97. }
  98. sqlite3_finalize(statement);
  99. // 4 animations 25 FPS = 100 images per second
  100. if (sqlite3_prepare_v2(
  101. database,
  102. "SELECT imageData FROM Images;",
  103. -1,
  104. &statement,
  105. 0
  106. )!=SQLITE_OK)
  107. return;
  108. // Execute first time
  109. printf("Reading 100 images\n");
  110. RakNetTimeMS startTime=RakNet::GetTimeMS();
  111. rc = sqlite3_step(statement);
  112. RakNetTimeMS endTime=RakNet::GetTimeMS();
  113. printf("Statement execution took %i milliseconds\n", endTime-startTime);
  114. printf("Processing row ");
  115. unsigned int rowCount=1;
  116. while (rc==SQLITE_ROW)
  117. {
  118. const void *readImageDataFromDb = sqlite3_column_blob(statement, 0);
  119. int lengthOfImageData = sqlite3_column_bytes(statement, 0);
  120. struct jpeg_decompress_struct cinfo;
  121. struct my_error_mgr jerr;
  122. cinfo.err = jpeg_std_error(&jerr.pub);
  123. jerr.pub.error_exit = my_error_exit;
  124. jpeg_create_decompress(&cinfo);
  125. jpeg_memory_src(&cinfo, (const JOCTET *) readImageDataFromDb, lengthOfImageData);
  126. (void) jpeg_read_header(&cinfo, TRUE);
  127. JDIMENSION width = cinfo.image_width;
  128. JDIMENSION height = cinfo.image_height;
  129. jpeg_destroy_decompress(&cinfo);
  130. printf("%i ", rowCount++);
  131. rc = sqlite3_step(statement);
  132. }
  133. if (rc==SQLITE_ERROR)
  134. return;
  135. RakNetTimeMS endTime2=RakNet::GetTimeMS();
  136. sqlite3_reset(statement);
  137. printf("\n");
  138. printf("Processing took %i milliseconds\n", endTime2-endTime);
  139. printf("Total time is %i milliseconds\n", endTime2-startTime);
  140. sqlite3_finalize(statement);
  141. free(fileBuff1);
  142. free(fileBuff2);
  143. sqlite3_close(database);
  144. printf("Press enter to quit\n");
  145. char str[256];
  146. gets(str);
  147. return;
  148. }
粤ICP备19079148号