comparison OrthancFramework/UnitTestsSources/RestApiTests.cpp @ 4150:b56f3a37a4a1

optimization of ChunkedBuffer if many small chunks are added
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 19 Aug 2020 11:18:55 +0200
parents 732ad6c618ba
children 8c559dd5034b
comparison
equal deleted inserted replaced
4149:72047b61570f 4150:b56f3a37a4a1
141 } 141 }
142 142
143 #endif 143 #endif
144 144
145 145
146 TEST(RestApi, ChunkedBuffer) 146 TEST(ChunkedBuffer, Basic)
147 { 147 {
148 ChunkedBuffer b; 148 for (unsigned int i = 0; i < 2; i++)
149 //b.SetTrailingBufferSize(0); // TODO 149 {
150 150 ChunkedBuffer b;
151 ASSERT_EQ(0u, b.GetNumBytes()); 151
152 152 if (i == 0)
153 b.AddChunk("hello", 5); 153 {
154 ASSERT_EQ(5u, b.GetNumBytes()); 154 b.SetPendingBufferSize(0);
155 155 ASSERT_EQ(0u, b.GetPendingBufferSize());
156 b.AddChunk("world", 5); 156 }
157 ASSERT_EQ(10u, b.GetNumBytes()); 157 else
158 158 {
159 std::string s; 159 ASSERT_EQ(16u * 1024u, b.GetPendingBufferSize());
160 b.Flatten(s); 160 }
161 ASSERT_EQ("helloworld", s); 161
162 ASSERT_EQ(0u, b.GetNumBytes());
163
164 b.AddChunk("hello", 5);
165 ASSERT_EQ(5u, b.GetNumBytes());
166
167 b.AddChunk("world", 5);
168 ASSERT_EQ(10u, b.GetNumBytes());
169
170 std::string s;
171 b.Flatten(s);
172 ASSERT_EQ("helloworld", s);
173 }
162 } 174 }
163 175
164 176
165 TEST(RestApi, ParseCookies) 177 TEST(RestApi, ParseCookies)
166 { 178 {
881 w.SetUrl("coucou"); 893 w.SetUrl("coucou");
882 w.SetUrl("/coucou"); 894 w.SetUrl("/coucou");
883 } 895 }
884 896
885 897
898 TEST(ChunkedBuffer, DISABLED_Large)
899 {
900 const size_t LARGE = 60 * 1024 * 1024;
901
902 ChunkedBuffer b;
903 for (size_t i = 0; i < LARGE; i++)
904 {
905 b.AddChunk(boost::lexical_cast<std::string>(i % 10));
906 }
907
908 std::string s;
909 b.Flatten(s);
910 ASSERT_EQ(LARGE, s.size());
911 ASSERT_EQ(0, b.GetNumBytes());
912
913 for (size_t i = 0; i < LARGE; i++)
914 {
915 ASSERT_EQ('0' + (i % 10), s[i]);
916 }
917
918 b.Flatten(s);
919 ASSERT_EQ(0u, s.size());
920 }
921
922
923 TEST(ChunkedBuffer, Pending)
924 {
925 ChunkedBuffer b;
926
927 for (size_t pendingSize = 0; pendingSize < 16; pendingSize++)
928 {
929 b.SetPendingBufferSize(pendingSize);
930 ASSERT_EQ(pendingSize, b.GetPendingBufferSize());
931
932 unsigned int pos = 0;
933 unsigned int iteration = 0;
934
935 while (pos < 1024)
936 {
937 size_t chunkSize = (iteration % 17);
938
939 std::string chunk;
940 chunk.resize(chunkSize);
941 for (size_t i = 0; i < chunkSize; i++)
942 {
943 chunk[i] = '0' + (pos % 10);
944 pos++;
945 }
946
947 b.AddChunk(chunk);
948
949 iteration ++;
950 }
951
952 std::string s;
953 b.Flatten(s);
954 ASSERT_EQ(0u, b.GetNumBytes());
955 ASSERT_EQ(pos, s.size());
956
957 for (size_t i = 0; i < s.size(); i++)
958 {
959 ASSERT_EQ('0' + (i % 10), s[i]);
960 }
961 }
962 }
963
886 964
887 965
888 namespace 966 namespace
889 { 967 {
890 class TotoBody : public HttpClient::IRequestBody 968 class TotoBody : public HttpClient::IRequestBody
913 chunk.clear(); 991 chunk.clear();
914 chunk.resize(chunkSize_); 992 chunk.resize(chunkSize_);
915 993
916 size_t i = 0; 994 size_t i = 0;
917 while (pos_ < size_ && 995 while (pos_ < size_ &&
918 i < chunkSize_) 996 i < chunk.size())
919 { 997 {
920 chunk[i] = '0' + (pos_ % 7); 998 chunk[i] = '0' + (pos_ % 7);
921 pos_++; 999 pos_++;
922 i++; 1000 i++;
923 } 1001 }
985 server.Register(handler); 1063 server.Register(handler);
986 server.Start(); 1064 server.Start();
987 1065
988 WebServiceParameters w; 1066 WebServiceParameters w;
989 w.SetUrl("http://localhost:5000"); 1067 w.SetUrl("http://localhost:5000");
990 1068
991 TotoBody body(600 * 1024 * 1024, 6 * 1024 * 1024 - 17); 1069 //TotoBody body(600 * 1024 * 1024, 6 * 1024 * 1024 - 17);
992 //TotoBody body(600 * 1024 * 1024, 1); 1070 TotoBody body(32 * 1024, 1); // This crashes Orthanc 1.6.0 to 1.7.2
993 1071
994 HttpClient c(w, "toto"); 1072 HttpClient c(w, "toto");
995 c.SetMethod(HttpMethod_Post); 1073 c.SetMethod(HttpMethod_Post);
996 c.AddHeader("Expect", ""); 1074 c.AddHeader("Expect", "");
997 c.AddHeader("Transfer-Encoding", "chunked"); 1075 c.AddHeader("Transfer-Encoding", "chunked");
1002 1080
1003 printf(">> [%s]\n", s.c_str()); 1081 printf(">> [%s]\n", s.c_str());
1004 1082
1005 server.Stop(); 1083 server.Stop();
1006 } 1084 }
1007
1008
1009
1010 TEST(Toto, DISABLED_Tata)
1011 {
1012 boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
1013
1014 ChunkedBuffer b;
1015 for (unsigned int i = 0; i < 600 * 1024 * 1024; i++)
1016 {
1017 b.AddChunk("a", 1);
1018 }
1019
1020 boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
1021
1022 printf("time: %d\n", (end-start).total_microseconds());
1023 }