rename.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. rename.c
  5. Abstract:
  6. This module implements the performance benchmark tests for the rename() C
  7. library call.
  8. Author:
  9. Chris Stevens 6-May-2015
  10. Environment:
  11. User
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. #include "perftest.h"
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #define PT_RENAME_TEST_FILE_NAME_LENGTH 48
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. //
  30. // ----------------------------------------------- Internal Function Prototypes
  31. //
  32. //
  33. // -------------------------------------------------------------------- Globals
  34. //
  35. //
  36. // ------------------------------------------------------------------ Functions
  37. //
  38. void
  39. RenameMain (
  40. PPT_TEST_INFORMATION Test,
  41. PPT_TEST_RESULT Result
  42. )
  43. /*++
  44. Routine Description:
  45. This routine performs the rename performance benchmark test.
  46. Arguments:
  47. Test - Supplies a pointer to the performance test being executed.
  48. Result - Supplies a pointer to a performance test result structure that
  49. receives the tests results.
  50. Return Value:
  51. None.
  52. --*/
  53. {
  54. char *DestinationFile;
  55. int FileCreated;
  56. int FileDescriptor;
  57. char FileNames[2][PT_RENAME_TEST_FILE_NAME_LENGTH];
  58. int Index;
  59. unsigned long long Iterations;
  60. pid_t ProcessId;
  61. char *SourceFile;
  62. int Status;
  63. char *TempFile;
  64. FileCreated = 0;
  65. Iterations = 0;
  66. Result->Type = PtResultIterations;
  67. Result->Status = 0;
  68. SourceFile = FileNames[0];
  69. DestinationFile = FileNames[1];
  70. //
  71. // Create two process safe file names. One will start as the source and the
  72. // other the destination.
  73. //
  74. ProcessId = getpid();
  75. for (Index = 0; Index < 2; Index += 1) {
  76. Status = snprintf(FileNames[Index],
  77. PT_RENAME_TEST_FILE_NAME_LENGTH,
  78. "rename%d_%d.txt",
  79. Index,
  80. ProcessId);
  81. if (Status < 0) {
  82. Result->Status = errno;
  83. goto MainEnd;
  84. }
  85. }
  86. SourceFile = FileNames[0];
  87. DestinationFile = FileNames[1];
  88. //
  89. // Create the source file.
  90. //
  91. FileDescriptor = creat(SourceFile, S_IRUSR | S_IWUSR);
  92. if (FileDescriptor < 0) {
  93. Result->Status = errno;
  94. goto MainEnd;
  95. }
  96. close(FileDescriptor);
  97. FileCreated = 1;
  98. //
  99. // Start the test. This snaps resource usage and starts the clock ticking.
  100. //
  101. Status = PtStartTimedTest(Test->Duration);
  102. if (Status != 0) {
  103. Result->Status = errno;
  104. goto MainEnd;
  105. }
  106. //
  107. // Measure the performance of the rename() C library routine by counting
  108. // the number of times a file can be renamed.
  109. //
  110. while (PtIsTimedTestRunning() != 0) {
  111. Status = rename(SourceFile, DestinationFile);
  112. if (Status != 0) {
  113. Result->Status = errno;
  114. break;
  115. }
  116. //
  117. // Swap the two pointers.
  118. //
  119. TempFile = SourceFile;
  120. SourceFile = DestinationFile;
  121. DestinationFile = TempFile;
  122. Iterations += 1;
  123. }
  124. Status = PtFinishTimedTest(Result);
  125. if ((Status != 0) && (Result->Status == 0)) {
  126. Result->Status = errno;
  127. }
  128. MainEnd:
  129. //
  130. // If the file was created, try to delete it at both possible file paths.
  131. //
  132. if (FileCreated != 0) {
  133. remove(FileNames[0]);
  134. remove(FileNames[1]);
  135. }
  136. Result->Data.Iterations = Iterations;
  137. return;
  138. }
  139. //
  140. // --------------------------------------------------------- Internal Functions
  141. //